如何在Nginx配置中有条件地添加标题? [英] How can I add header conditionally in nginx configuration?

查看:109
本文介绍了如何在Nginx配置中有条件地添加标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在nginx(1.4.6 ubuntu)配置中有条件地添加标头?

How can I add header conditionally in nginx(1.4.6 ubuntu) configuration?

起初我是这样尝试的.

 location / {
      add_header X-TEST-0 always-0;

      set $true 1;
      if ($true) {
          add_header X-TEST-1 only-true;
      }

      add_header X-TEST-2 always-2;
  }    

尽管我期望我的nginx设置所有标头(X-TEST-0,1,2),但它仅添加标头X-TEST-1.

Although I expected my nginx set all header(X-TEST-0,1,2), it added header only X-TEST-1.

接下来我尝试这样.

 location / {
      add_header X-TEST-0 always-0;

      ##set $true 1;
      ##if ($true) {
      ##    add_header X-TEST-1 only-true;
      ##}

      add_header X-TEST-2 always-2;
  }    

我的nginx按预期添加了标题X-TEST-1和X-TEST-2.

My nginx added header X-TEST-1 and X-TEST-2 as I expected.

我的追求是...

  1. 如何有条件地添加标题?

  1. How can I add header conditionally?

在上面的第一个示例中,为什么nginx只添加X-TEST-1?

Why does nginx add only X-TEST-1 with my first example above?

提前谢谢!

推荐答案

(我会为将来会有同样问题的人们写这个结论.感谢@Alexey Ten提供的信息.)

(I would write this conclusion for the people who will have the same question in the future. Thanks to @Alexey Ten for the information.)

if指令内不要使用任何非重写指令.因为if指令本质上是重写模块的一部分.

Don't use any non-rewrite directives inside if directives. Because if directive is essentially part of rewrite module.

我们可以在名为 IfIsEvil 的官方文档中获得有关它的更多详细信息.

We could get more details about it in official document named IfIsEvil.

这篇关于如何在Nginx配置中有条件地添加标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆