Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer
    
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 –  BaronVonKaneHoffen Sep 16 '13 at 19:11
3  
Nobody said anything about putting root in. It's not common to all your configuration blocks! –  Michael Hampton Sep 16 '13 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. –  BaronVonKaneHoffen Sep 17 '13 at 10:21
    
Well, you can always remove them... they shouldn't be there anyway. –  Michael Hampton Sep 17 '13 at 12:57
1  
God you're right as well. They can go in the main server {} block.... Sorry, I'm not normally this dumb honestly –  BaronVonKaneHoffen Sep 18 '13 at 15:22

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.

share|improve this answer
    
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.... –  BaronVonKaneHoffen Sep 17 '13 at 10:54

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
}
share|improve this answer
    
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 Oct 23 '13 at 4:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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