NGINX + PHP-FPM virtual hosting redirects php request to wrong directory

nginxPHPphp-fpmphp7web

My server has 2 sites setup using nginx. Below are the content in /etc/nginx/mysite1.conf

server {
    listen       80;
    server_name  test.mysite1.com;


    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/mysite1;
        try_files $uri $uri/ /index.php?$args;
        index  index.html index.htm index.php;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/mysite1;
    }

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php-fpm/php-fpm-mysite1.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }

}

The content of /etc/php-fpm.d/mysite1.conf is below

[mysite1]
user = nginx
group = nginx
listen = /run/php-fpm/php-fpm-mysite1.sock
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /status

The configuration for mysite2 is exactly the same (except for replacing mysite1 with mysite2). When both nginx my php-fpm is started, both sites work. But any time a php file in site2 is accessed, it goes to the same place of site 1. for example, when i access http://test.mysite2.com/tester.php it shows http://test.mysite1.com/tester.php .

Notes:

  • The server block in /etc/nginx.conf is commented out
  • All permissions have been set so nginx user can rwx to all /var/www directories and SELinux has been disabled.
  • OS : CentOS 7
  • ps -ef shows that processes with the mysite1 and mysite2 names are started when php-fpm is run
  • though I've added /status to the php-fpm config it doesn't work for either site
  • no error logs are shown (should there be? there is no exact error massage or anything for this)

Any help or suggestions are greatly appreciated.

Best Answer

It seems that php-fpm receives a wrong SCRIPT_FILENAME parameter from nginx.

include fastcgi_params; will probably override fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; in your config, because it is defined 1 line below. If the fastcgi_params file contains SCRIPT_FILENAME (look at /etc/nginx/fastcgi_params - maybe you changed SCRIPT_FILENAME here from $document_root$fastcgi_script_name to /var/www/mysite1/$fastcgi_script_name?), the previous parameter definition will be replaced.

It may also be possible that the wrong nginx server block is used (you can verify this by using a distinct access log for both, e.g. access_log logs/host1.access.log main; and access_log logs/host2.access.log main;), and therefore $document_root resolves to /var/www/mysite1 for both pages. Be sure that nginx receives the correct HTTP-Host-header (for example, this will not work when you access the webserver by IP).

If this does not work out, try a fixed SCRIPT_FILENAME below the include fastcgi_params, like this:

...
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/mysite1/$fastcgi_script_name;
...

...
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/mysite2/$fastcgi_script_name;
...