AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 895829
Accepted
Sudh33ra
Sudh33ra
Asked: 2018-02-06 23:19:35 +0800 CST2018-02-06 23:19:35 +0800 CST 2018-02-06 23:19:35 +0800 CST

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

  • 772

我的服务器使用 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 中的 server 块被注释掉
  • 已设置所有权限,因此 nginx 用户可以 rwx 到所有 /var/www 目录,并且 SELinux 已被禁用。
  • 操作系统:CentOS 7
  • ps -ef 显示 mysite1 和 mysite2 名称的进程在运行 php-fpm 时启动
  • 尽管我已将 /status 添加到 php-fpm 配置中,但它对任何一个站点都不起作用
  • 没有显示错误日志(应该有吗?没有确切的错误消息或任何东西)

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

nginx
  • 2 2 个回答
  • 1658 Views

2 个回答

  • Voted
  1. mlist
    2018-02-06T23:56:42+08:002018-02-06T23:56:42+08:00

    似乎 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;),因此 $document_root 解析/var/www/mysite1为两个页面。确保 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;
    ...
    
    • 1
  2. Best Answer
    Sudh33ra
    2018-02-07T01:51:32+08:002018-02-07T01:51:32+08:00

    问题在于位置块。

    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;
    }
    
    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve