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

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

问题描述

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

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

一开始我是这样尝试的.

At first I tried like this.

 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天全站免登陆