V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
peesefoo
V2EX  ›  程序员

nginx 配置,使用 if 判断 add_header,其他 add_header 不见了

  •  
  •   peesefoo · Apr 25, 2017 · 7585 views
    This topic created in 3291 days ago, the information mentioned may be changed or developed.

    大体配置是这样,想实现带 mycookie 这个 cookie 的 php 页面,添加 Cache-Control 这个 http 头:

    server {
    
        ...
    
        set $skip_cache 0;
    
        if ($http_cookie ~* "mycookie") {
            set $skip_cache 1;
        }
        
        location ~ [^/]\.php(/|$) {
            
            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-Content-Type-Options "nosniff";
            
            if ($skip_cache = 1) {
                add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
            }
        }
    }
    

    发现加了下面这 3 行代码后, Cache-Control 头倒是出现了,但是 X-Frame-Options 和 X-Content-Type-Options 这两个 http 头不见了,删掉这 3 行代码后又出现:

    if ($skip_cache = 1) {
        add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
    }
    
    11 replies    2017-04-26 22:43:40 +08:00
    julyclyde
        1
    julyclyde  
       Apr 25, 2017
    有一些指令的特性是这样的,只能在同一层内生效
    所以你大概可以明白 fastcgi.conf 和 fastcgi_params 文件的用途了吧
    rrfeng
        2
    rrfeng  
       Apr 25, 2017
    if-is-evil
    peesefoo
        3
    peesefoo  
    OP
       Apr 25, 2017
    @rrfeng 测试了一下,如果不用 if ,仅仅添加:
    add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";

    X-Frame-Options 和 X-Content-Type-Options 这两个头也会消失。
    crystom
        4
    crystom  
       Apr 25, 2017
    add_header 是数组,子块里有就会覆盖所有
    rrfeng
        5
    rrfeng  
       Apr 25, 2017
    location ~ [^/]\.php(/|$) {

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
    add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0";
    }

    这样也会只有最后一个?
    peesefoo
        6
    peesefoo  
    OP
       Apr 25, 2017
    @rrfeng 再看了一下,有三个,抱歉,上面说法有误。
    Mitt
        7
    Mitt  
       Apr 25, 2017
    尝试加 always 参数
    ```
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    ```
    PS: 网上查的解决方案。 Nginx > 1.7.5
    msg7086
        8
    msg7086  
       Apr 25, 2017
    nginx 里使用 if 判断,出问题不稀奇。
    isCyan
        9
    isCyan  
       Apr 26, 2017 via Android
    crystom 的回答应该是正解
    ryd994
        10
    ryd994  
       Apr 26, 2017 via Android
    if is evil
    spacewander
        11
    spacewander  
       Apr 26, 2017 via Android
    这事还真不好怪在 if 头上。
    add_header 定义在下一级的会覆盖上一级的。你可以试试在 server 级别定义 add_header 看看。
    一般遇到纠结于 Nginx 配置的人,我都会安利 lua-nginx-module 。可以省下不少 corner case 的精力。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1193 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 23:19 · PVG 07:19 · LAX 16:19 · JFK 19:19
    ♥ Do have faith in what you're doing.