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

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

  •  
  •   peesefoo · 2017-04-25 22:10:26 +08:00 · 6965 次点击
    这是一个创建于 2554 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大体配置是这样,想实现带 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 条回复    2017-04-26 22:43:40 +08:00
    julyclyde
        1
    julyclyde  
       2017-04-25 22:15:37 +08:00
    有一些指令的特性是这样的,只能在同一层内生效
    所以你大概可以明白 fastcgi.conf 和 fastcgi_params 文件的用途了吧
    rrfeng
        2
    rrfeng  
       2017-04-25 22:17:21 +08:00
    if-is-evil
    peesefoo
        3
    peesefoo  
    OP
       2017-04-25 22:20:21 +08:00
    @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  
       2017-04-25 22:24:53 +08:00
    add_header 是数组,子块里有就会覆盖所有
    rrfeng
        5
    rrfeng  
       2017-04-25 22:27:15 +08:00
    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
       2017-04-25 22:29:40 +08:00
    @rrfeng 再看了一下,有三个,抱歉,上面说法有误。
    Mitt
        7
    Mitt  
       2017-04-25 22:40:16 +08:00
    尝试加 always 参数
    ```
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    ```
    PS: 网上查的解决方案。 Nginx > 1.7.5
    msg7086
        8
    msg7086  
       2017-04-25 23:51:56 +08:00
    nginx 里使用 if 判断,出问题不稀奇。
    isCyan
        9
    isCyan  
       2017-04-26 00:12:11 +08:00 via Android
    crystom 的回答应该是正解
    ryd994
        10
    ryd994  
       2017-04-26 00:30:43 +08:00 via Android
    if is evil
    spacewander
        11
    spacewander  
       2017-04-26 22:43:40 +08:00 via Android
    这事还真不好怪在 if 头上。
    add_header 定义在下一级的会覆盖上一级的。你可以试试在 server 级别定义 add_header 看看。
    一般遇到纠结于 Nginx 配置的人,我都会安利 lua-nginx-module 。可以省下不少 corner case 的精力。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5773 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.