3

So I have Nginx & php-fpm serving multiple sites on different domains. The configuration file gets very large due to the repetition of location ~ \.php$ { } blocks in each server { } block:

server {
    listen 80;
    server_name firstsite.com;
    root /var/www/firstsite;

    location ~ \.php$ {
        root           /var/www/firstsite;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
server {
    listen 80;
    server_name secondsite.com;
    root /var/www/secondsite;

    location ~ \.php$ {
        root           /var/www/secondsite;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

I can't move the blocks off to another file and include it, as root changes in each instance. How can I avoid all that repetition?

3 Answers 3

6

You move the common stuff off to another file and include it.

5
  • Yes, but the point here is the "root" directive is different for each block. If I could pass variables with each include statement, that'd be great.... but you can't afaik Commented Sep 16, 2013 at 19:11
  • 3
    Nobody said anything about putting root in. It's not common to all your configuration blocks! Commented Sep 16, 2013 at 19:17
  • Soerry, perhaps I wasn't explaining myself very well. Each location { } block has a root directive in. It's just it's value changes for each instance. ....so it's how I'd get round that without having to repeat each block. Commented Sep 17, 2013 at 10:21
  • Well, you can always remove them... they shouldn't be there anyway. Commented Sep 17, 2013 at 12:57
  • 2
    God you're right as well. They can go in the main server {} block.... Sorry, I'm not normally this dumb honestly Commented Sep 18, 2013 at 15:22
3

Create a directory such as /etc/nginx/package-configs/ and put the common php stuff into file /etc/nginx/package-configs/php.conf.

To use the file, in your php locations put include /etc/nginx/package-configs/php.conf.

1
  • So I'd still have to start that location ~ \.php$ { } block in the main server block, then include another file inside it that has all parameters other than the root one? That gets the amount of lines down a bit I suppose, even if it isn't ideal.... Commented Sep 17, 2013 at 10:54
3

For a more generic answer, you can also use a template:

# conf.d/sites.conf
server { 
  set $sitename brazzers.com
  include conf.d/site.template;
}
server { 
  set $sitename playboy.com
  include conf.d/site.template;
}

 

# site.template
location / {
        root /var/www/$sitename;
        # etc etc etc
}
1
  • Thanks for this! I didn't realize the set directive could be so useful. Is it possible to set $variable to be nothing at all? I'm trying to further reduce the number of template files by using a $subdomain variable, e.g. server_name $subdomain$sitename;, if I understand this correctly.
    – Paul
    Commented Oct 23, 2013 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .