How can I re-use PHP configuration directives in multiple Nginx server blocks?

BaronVonKaneHoffen asked:

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?

My answer:


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


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.