Set global handling for PHP scripts in NGINX + PHP-FPM

Radio asked:

I have to define fastcgi_pass for every virtual host. How do I define it global-wise?

server {
    listen       80;
    server_name  www.domain.tld;

    location / {
        root   /home/user/www.domain.tld;
        index  index.html index.php;
    }

    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/user/domain.tld$fastcgi_script_name;
        include        fastcgi_params;
    }

}

My answer:


Make a file that you can include wherever you need it. For instance:

cat > /etc/nginx/php-fpm <<EOF
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
EOF

Then modify your location to include php-fpm; and fix the root directive (it should appear under server, not under location /; this is a common nginx misconfiguration).


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.