nginx alias 虚拟目录的灵异现象

2017-09-21 18:15:06 +08:00
 csbde

最新玩 nginx 配置,有一特殊需求,需要虚拟一个目录来运行根目录的程序。

nginx 配置如下,但出现了诡异现象,php 可以正常运行,各项参数也正确,但所有的静态页面不能正确读取。

	  root /home/wwwroot/wx.test.com/public/;
       location ~  ^/my/
          {
             alias /home/wwwroot/wx.test.com/public/;
             index index.html index.htm index.php default.html default.htm default.php;
             add_header X-My yes;
             autoindex off;
 
             location ~  ^/my/(.+\.php)$
                {
                  add_header X-subdir yes;
                  fastcgi_pass  unix:/tmp/php-cgi.sock;
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME  $document_root$1;
                  include fastcgi_params;

         }
    }


比如运行 www.test.com/my/index.php

是正确运行的。

任意静态文件放到 /home/wwwroot/wx.test.com/public/ 下面均无法读取到,报 404 错误 如静态文件的路径 /home/wwwroot/wx.test.com/public/index.html

访问路径:www.test.com/my/index.html 报 404 错误

请大神帮助解惑

1936 次点击
所在节点    问与答
11 条回复
rrfeng
2017-09-21 18:26:49 +08:00
不要嵌套 location
csbde
2017-09-21 18:29:38 +08:00
放外面一样的效果
csbde
2017-09-21 18:30:36 +08:00
也谢谢,铜币送上
paranoiagu
2017-09-21 18:37:12 +08:00
看看 access 日志,再找原因
csbde
2017-09-21 18:43:59 +08:00
日志就是 404
csbde
2017-09-21 18:45:08 +08:00
铜币送上
csbde
2017-09-21 18:46:55 +08:00
也送上感谢
gstqc
2017-09-21 18:56:09 +08:00
正则的 location 下用 alias 要非常注意
建议不使用正则
具体参考官方文档吧
csbde
2017-09-21 18:59:34 +08:00
@gstqc 已经翻看了很多次
qcloud
2017-09-21 19:20:19 +08:00
鬼故事
csbde
2017-09-21 21:37:26 +08:00
@qcloud 是啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/392616

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX