• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Had
V2EX  ›  程序员

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

  •  1
     
  •   Had · Mar 18, 2016 · 3743 views
    This topic created in 3710 days ago, the information mentioned may be changed or developed.

    一个 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 replies    2016-03-19 11:32:09 +08:00
    Had
        1
    Had  
    OP
       Mar 19, 2016
    然而并没有人可以回答?
    唔。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 1437ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    ♥ Do have faith in what you're doing.