Nginx – File issue with NGinx & PHP-FPM on separate servers

network-sharenginxphp-fpm

I've done a bit of research on running NGinx and PHP with FastCGI on separate servers, and have successfully set it up on test virtual machines. I've ran into the problem that these articles describe…

…which is that in order for this setup to work, the PHP files need to be in the same directory on BOTH servers, even though the machines running the php-fpm worker is actually executing the scripts. I've tested this myself; if I remove a file 'test.php' in the document root on the NGinx server, it throws a 404 or 403 even though the file still exists on the PHP-FPM machine.

My question: is there no way around this? I know using a central storage running NFS is one solution, but I would like to avoid the performance issues associated with that.

If it helps, here are my config files:

/etc/nginx/sites-enabled/test-app.conf

upstream test-app {
    server <php-fpm-server-ip>:9001;
}

server {
    listen 80;
    listen [::]:80;

    server_name test.app.com;
    root /var/www/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri $uri/ =404;
        fastcgi_pass test-app;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

Best Answer

If you really want to do this, remove this from nginx under your \.php location:

try_files $uri $uri/ =404;

That is what causes it to check the filesystem before passing the request off to the fcgi upstream. Also verify that the fcgi process has the root directory set properly so it can't read random php files now that nginx can't do existence checking.