设为首页收藏本站

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 14|回复: 0

nginx 反代与根目录 PHP 共存

[复制链接]

60

主题

346

回帖

976

积分

高级会员

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

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

配置文件示例:
[ol]
  • 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;
  •     }
  • }[/ol]复制代码

    尝试解决:
    将[ol]
  • location ~ \.php$ {[/ol]复制代码修改为强制匹配[ol]
  • location = / {[/ol]复制代码在 location 模块中使用 alias 指向绝对目录
    [ol]
  •     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;
  •     }[/ol]复制代码

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

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

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

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Discuz! X

    GMT+8, 2026-4-12 05:19 , Processed in 0.020399 second(s), 3 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2025 Discuz! Team.

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