3

I want to automate the nginx vhost configuration for a website which have some if conditional statements. I was trying to do it with the jfryman-nginx puppet forge module but I am not able to configure the file. The configuration I want is given below:

 server {
  listen   8080;

  server_name abcqwe.com;

  root /data/vvv/abcqwe.com;
  index index.php index.html index.htm;

  access_log /data/vvv/abcqwe.com/logs/access.log;
  error_log  /data/vvv/abcqwe.com/logs/error.log;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    #try_files $uri $uri/ /index.html;

    if (!-f $request_filename){
      set $rule_0 1$rule_0;
    }

    try_files $uri $uri/ /index.html;
  }

So how do i put this if condition inside my vhost configuration:

 if (!-f $request_filename){
       set $rule_4 1$rule_4;
     }

Is it possible to do it though? I have tried with location_cfg_prepend but that didn't help..

5
  • I' not clear what you are asking, but try to read "If is Evil" article.
    – Alexey Ten
    Apr 22, 2014 at 3:09
  • 1
    I have the same problem and am still looking for a solution. The problem seems to be with location_cfg_prepend adding a ; after the closing if } in the generated config file. nginx does not seem to like that. location_cfg_append does the same thing.
    – zuniga
    May 5, 2014 at 7:26
  • @zuniga I posted an answer. There's a fix in the latest source on github, but it's not in puppet forge. May 27, 2014 at 1:21
  • Thanks @GuyHughes I will give the latest version a try but that is not a good enough solution for me in production and will have to wait till the fix is released
    – zuniga
    Jun 12, 2014 at 16:26
  • @zuniga, the github master branch should be production quality. The easy option is to take the v0.0.9 module as in puppet forge, and edit the .erb templates to remove the semicolon where appropriate. You could use a patch to apply the changes across your production machines. Jun 12, 2014 at 18:34

3 Answers 3

3

I had the same problem with v0.0.9 of jfryman-nginx.

In the latest source templates on github, it looks like the ; is not supposed to be added if you use location_custom_cfg_{pre,ap}pend.

But in v0.0.9, the latest bundled puppet forge release, the template adds the ; after even if the line only has a } causing nginx to fail.

Solutions:

  1. Use the location_**custom**_cfg_{pre,ap}pend options

  2. Either A or B:

    A. Edit out the ; in the templates

    B. Pull the latest version of the source from github:

sudo rm -rf /etc/puppet/modules/nginx && cd /etc/puppet/modules && sudo git clone https://github.com/jfryman/puppet-nginx nginx

Example

  nginx::resource::location { 'sub.dom.com_root':
    ensure                                                                       => present,
    vhost                                                                        => 'sub.dom.com',
    location                                                                     => '~ \.php$',
    fastcgi                                                                      => 'unix:/var/run/php5-fpm.sock',                                                                                                                                                              
    www_root                                                                     => '/var/www/sub.dom.com',
    location_custom_cfg_append                                                   => {
      'if (-f $request_filename)'                                                => '{ break; }',
      'if (-d $request_filename)'                                              => '{ break; }',
      'if ($request_filename !~ (js|css|jpg|png|gif|robots\.txt|index\.php.*) )' => '{ rewrite ^/(.*) /index.php?$1 last; }',
    },
  }
1
  • i solved it by adding # as the last char and forcing the extra ; to become a comment: 'if (-f $request_filename)' => '{ break; }#'
    – milan
    Jun 11, 2014 at 13:21
0

You can now use the raw_prepend and raw_append parameters for this.

See the location.pp page for more detail.

0
-1

Please try to use LuaModule in nginx,Lua Module having lot of configuration.

in Lua syntax very simple to use.

Please read this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.