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

nginx 配置静态目录路由失效,哪里写错了吗?

  •  
  •   LeeReamond · 2021-12-08 20:45:05 +08:00 · 600 次点击
    这是一个创建于 869 天前的主题,其中的信息可能已经有所发展或是发生改变。

    配置如下:

    server {
    	...
        location ~* \.(css|gif|ico|jpg|js|png|ttf|woff)$ {
          root /www/wwwroot/example.com/static;
          try_files /www/wwwroot/example/static/$uri $uri/ /404.html;
        }
    }
    

    想要寻找的文件是 favicon.ico ,也确保 /www/wwwroot/example.com/static 这个文件夹里有 favicon.ico ,这个路由也是排在首位的,访问 example.com/favicon.ico 的时候后端检查了一下实际请求的路由是 /404.html ,似乎是 try_files 失败了,这咋回事?

    2 条回复    2021-12-08 21:44:54 +08:00
    maichael
        1
    maichael  
       2021-12-08 20:58:22 +08:00
    你都配置 root 了,try_files 就直接 $uri 就好了。
    LeeReamond
        2
    LeeReamond  
    OP
       2021-12-08 21:44:54 +08:00
    @maichael 试了试,改成 tryfiles $uri $uri/ /404.html 这样就行了。之前那个好像也能跑,是 cdn 的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3213 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 14:00 · PVG 22:00 · LAX 07:00 · JFK 10:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.