NGINX + PHP-FPM 虚拟主机将 php 请求重定向到错误目录

NGINX + PHP-FPM 虚拟主机将 php 请求重定向到错误目录

我的服务器使用 nginx 设置了 2 个站点。以下是 /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;
    }

}

/etc/php-fpm.d/mysite1.conf 的内容如下

[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

mysite2 的配置完全相同(除了将 mysite1 替换为 mysite2)。当两个 nginx 和 php-fpm 都启动时,两个站点都可以工作。但每次访问 site2 中的 php 文件时,它都会转到与站点 1 相同的位置。例如,当我访问http://test.mysite2.com/tester.php表明http://test.mysite1.com/tester.php

笔记:

  • /etc/nginx.conf 中的服务器块被注释掉
  • 所有权限均已设置,因此 nginx 用户可以对所有 /var/www 目录进行 rwx,并且 SELinux 已被禁用。
  • 操作系统:CentOS 7
  • ps -ef 显示在运行 php-fpm 时启动了名为 mysite1 和 mysite2 的进程
  • 尽管我已将 /status 添加到 php-fpm 配置中,但它对两个网站都不起作用
  • 没有显示错误日志(应该有吗?没有确切的错误信息或任何内容)

非常感谢任何帮助或建议。

答案1

看来 php-fpm 从 nginx 接收到了错误的 SCRIPT_FILENAME 参数。

include fastcgi_params;将可能覆盖 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;在您的配置中,因为它在下面 1 行中定义。如果 fastcgi_params 文件包含 SCRIPT_FILENAME(查看 /etc/nginx/fastcgi_params - 也许您将此处的 SCRIPT_FILENAME 从 更改为$document_root$fastcgi_script_name/var/www/mysite1/$fastcgi_script_name),则先前的参数定义将被替换。

也可能使用了错误的 nginx 服务器块(您可以通过对两个页面使用不同的访问日志来验证这一点,例如access_log logs/host1.access.log main;access_log logs/host2.access.log main;),因此/var/www/mysite1两个页面的 $document_root 都解析为。确保 nginx 收到正确的 HTTP-Host-header(例如,当您通过 IP 访问 Web 服务器时,这将不起作用)。

如果这不起作用,请尝试固定的 SCRIPT_FILENAME以下,像include fastcgi_params这样:

...
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;
...

答案2

问题出在位置块上。

location ~ \.php$ {
root /var/www/mysite1;
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;
}

相关内容