全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 893|回复: 1

nginx 反代与根目录 PHP 共存

[复制链接]
发表于 2020-10-3 22:23:17 | 显示全部楼层 |阅读模式
错误信息:
在 nginx 的一个 vhost 配置中,在根目录放置 index.php 文件,并为子目录设置反代,直接访问域名时返回 nginx403,需手动添加 index.php 方可正常访问 php 。

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

配置文件示例:
  1. server {
  2.     listen 443 ssl http2;
  3.     server_name domain_name;
  4.     root /home/www;
  5.     index index.php;
  6.    
  7.     #ssl 配置略去
  8.    
  9.     location ~ \.php$ {
  10.         include fastcgi_params;
  11.         fastcgi_intercept_errors on;
  12.         fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
  13.         fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
  14.     }

  15.     location /deluge/ {
  16.         proxy_pass http://localhost:8112/;
  17.         proxy_set_header X-Deluge-Base "/deluge/";
  18.         add_header X-Frame-Options SAMEORIGIN;
  19.     }
  20. }
复制代码


尝试解决:
  1. location ~ \.php$ {
复制代码
修改为强制匹配
  1. location = / {
复制代码
在 location 模块中使用 alias 指向绝对目录
  1.     location = / {
  2.         include fastcgi_params;
  3.         fastcgi_intercept_errors on;
  4.         fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
  5.         fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
  6.         index index.php
  7.         alias /home/www;
  8.     }
复制代码


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

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

请问是否有办法解决这个问题?
发表于 2020-10-3 22:23:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-29 13:53 , Processed in 0.059377 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表