V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Backlitz
V2EX  ›  NGINX

nginx 反代与根目录 PHP 共存

  •  
  •   Backlitz · 2020-10-03 22:07:03 +08:00 · 2155 次点击
    这是一个创建于 1294 天前的主题,其中的信息可能已经有所发展或是发生改变。

    错误信息: 在 nginx 的一个 vhost 配置中,在根目录放置 index.php 文件,并为子目录设置反代,直接访问域名时返回 nginx403,需手动添加 index.php 方可正常访问 php 。

    猜测原因: 在使用 nginx 反代时,将反代设置为子目录,location 使用直接匹配。 而引入 php 配置时使用正则匹配。 直接匹配优先级高于正则匹配。

    配置文件示例:

    server {
        listen 443 ssl http2;
        server_name domain_name;
        root /home/www;
        index index.php;
        
        #ssl 配置略去
        
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }
    
        location /deluge/ {
            proxy_pass http://localhost:8112/;
            proxy_set_header X-Deluge-Base "/deluge/";
            add_header X-Frame-Options SAMEORIGIN;
        }
    }
    

    尝试解决: 将 location ~ \.php$ { 修改为强制匹配 location = / { 在 location 模块中使用 alias 指向绝对目录

        location = / {
            include fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
            index index.php
            alias /home/www;
        }
    

    访问网页,返回纯文本内容 File not found.

    已知在不设置子目录反代时,该配置 php 可正常访问。

    请问是否有办法解决这个问题?

    1 条回复    2020-10-04 10:27:09 +08:00
    masker
        1
    masker  
       2020-10-04 10:27:09 +08:00 via Android
    建议学会自己动手找 谷歌 and 看相应日志
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1020 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:08 · PVG 03:08 · LAX 12:08 · JFK 15:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.