V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
csbde
V2EX  ›  问与答

nginx alias 虚拟目录的灵异现象

  •  
  •   csbde · 2017-09-21 18:15:06 +08:00 · 1917 次点击
    这是一个创建于 2381 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最新玩 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 错误

    请大神帮助解惑

    11 条回复    2017-09-21 21:37:26 +08:00
    rrfeng
        1
    rrfeng  
       2017-09-21 18:26:49 +08:00   ❤️ 1
    不要嵌套 location
    csbde
        2
    csbde  
    OP
       2017-09-21 18:29:38 +08:00
    放外面一样的效果
    csbde
        3
    csbde  
    OP
       2017-09-21 18:30:36 +08:00
    也谢谢,铜币送上
    paranoiagu
        4
    paranoiagu  
       2017-09-21 18:37:12 +08:00 via Android   ❤️ 1
    看看 access 日志,再找原因
    csbde
        5
    csbde  
    OP
       2017-09-21 18:43:59 +08:00
    日志就是 404
    csbde
        6
    csbde  
    OP
       2017-09-21 18:45:08 +08:00
    铜币送上
    csbde
        7
    csbde  
    OP
       2017-09-21 18:46:55 +08:00
    也送上感谢
    gstqc
        8
    gstqc  
       2017-09-21 18:56:09 +08:00 via iPhone
    正则的 location 下用 alias 要非常注意
    建议不使用正则
    具体参考官方文档吧
    csbde
        9
    csbde  
    OP
       2017-09-21 18:59:34 +08:00
    @gstqc 已经翻看了很多次
    qcloud
        10
    qcloud  
       2017-09-21 19:20:19 +08:00 via iPhone
    鬼故事
    csbde
        11
    csbde  
    OP
       2017-09-21 21:37:26 +08:00 via Android
    @qcloud 是啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2691 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:09 · PVG 23:09 · LAX 08:09 · JFK 11:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.