V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Had
V2EX  ›  程序员

Nginx 配置, try_files 和 add_header 在同一个 location 中, add_header 不生效的问题

  •  1
     
  •   Had · 2016-03-18 04:39:06 +08:00 · 3054 次点击
    这是一个创建于 2953 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 Nginx 配置文件如下:

    server {
        listen 80;
        server_name ab.cd;
        index index.html;
        root /path/public;
    
        location @xx {
            proxy_pass http://10.0.0.1:8080;
        }
    
        location / {
            try_files $uri @xx;
        }
    
        location /special {
            add_header XX HEADER1;
            try_files $uri @xx;
        }
    }
    

    但是如果你去请求 /special 的话,会发现其实 add_header 并没有生效,所以 /special 的部分又不得不改写为:

        location /special {
            add_header XX HEADER1;
            if (!-f $request_filename) {
                proxy_pass http://10.0.0.1:8080;
                break;
            }
        }
    

    虽然这样 add_header 可以工作了,但是老实说,不好看。 大家有什么好的解决方案吗?

    谢谢。

    1 条回复    2016-03-19 11:32:09 +08:00
    Had
        1
    Had  
    OP
       2016-03-19 11:32:09 +08:00
    然而并没有人可以回答?
    唔。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5676 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:30 · PVG 11:30 · LAX 20:30 · JFK 23:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.