if条件如何在nginx conf中的位置块内工作? [英] How does if condition work inside location block in nginx conf?

查看:48
本文介绍了if条件如何在nginx conf中的位置块内工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读https://www.nginx.com/resources/wiki/start/主题/深度/ifisevil/

我想检查我的 rails 应用程序是否已经添加了标题 (Access-Control-Allow-Origin),如果还没有添加标题.

I want to check if my rails application has already added a header (Access-Control-Allow-Origin) and if it hasn't then add the header.

这里的例子试图解释nginx.conf中if条件的行为http://agentzh.blogspot.in/2011/03/how-nginx-location-if-works.html

The examples here have tried to explain the behaviour of if condition in nginx.conf http://agentzh.blogspot.in/2011/03/how-nginx-location-if-works.html

但是我没看懂.我的一个问题是,当他们说指令或阶段指令时是什么意思?

But I have not understood it. One of the question I have is what is meant when they say a directive or phase directive?

我自己尝试了一些东西.喜欢:

I have tried some things myself. Like:

location / {
  add_header My-outer-header '*';
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true; 

  if($sent_http_access_control_allow_origin != /*){
    add_header My-inner-header '**';
  }
}

这里当满足 if 条件时,只设置了 My-inner-header 而不是 My-outer-header.

Here when the if condition is met, only My-inner-header is set and not the My-outer-header.

但是当我这样做时:

location / {
  add_header My-outer-header '*';
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true; 
  set $a 1;
  if($a == 1){
    add_header My-inner-header '**';
  }
}

此处 $a 变量设置为 1,但仅再次设置了 My-inner-header 而不是 My-outer-header.

Here $a variable is set to 1 but only the My-inner-header is set again and not the My-outer-header.

我想检查并确保像 proxy_set_header 这样的指令是否被执行.

I want to check and ensure if the instructions like proxy_set_header are getting executed or not.

推荐答案

我还没有测试你的代码.我只讲经验!!!

I haven't tested your code. I only speak from experience!!!

首先,请允许我指定 Nginx 配置与任何 OOP 行为(面向对象编程)完全不同.在一个词中,它是不可预测的声明性".

To begin with, allow me to specify that Nginx configuration is NOT at all similar to any OOP behaviour (Object Oriented Programming). It is in a single word unpredictably "Declarative".

含义...如果您在同一块中有 2 个 IF 语句都满足条件,则只有第二个将占优势并被执行.

Meaning... If you have 2 IF statements in the same block that both meet the criteria, ONLY the second will prevail and be executed.

同样的情况发生在 1 if 以及它在同一块中的所有先前声明.由于IF的存在,一些变量(不是全部)不会被执行,nginx希望它们在里面重新声明.这发生在您的示例中.为了清楚起见,让我们看第二个.在您的位置块中,您声明了几个标头值.但是,当满足 IF 语句时,它们不会被执行(IF 在 nginx 中确实是邪恶的).为了完全执行,您需要在 if 语句内部重新声明大部分头文件变量和 if 外部的任何其他内容,以便在满足 if 语句的情况下执行它们.注意,在 nginx 中没有列出所有受 IF 影响的变量的映射,您应该测试我们的 conf 是否有任何错误.有些变量不需要重新声明,在这种情况下,nginx 将在重启时报告错误.唯一的办法是找出哪个,是全力以赴,排除报告的错误.

The same happens with 1 if and all it's previous declarations in the same block. Some variables (Not all) will not be executed due to the presence of IF, and nginx expects them to be re-declared within. This happens in your example. Let's take the second one for clarity. Inside your location block, you declare several header values. BUT, when the IF statement is met, they are NOT executed (IF is evil in nginx indeed). In order to have a full execution you need to re-declare most of your header variables inside and anything else outside the if, again inside the if statement, so they will be executed in case the if statement is met. NOTE, there is no map that lists all the variable affected by IF in nginx and you should test our conf for any errors. Some variables will NOT need to be re-declared and in this case nginx will report an error on restart. Only way to figure out which, is go for all and rule out the reported erroneous ones.

建议

a) 如果可能,请避免在 nginx 中使用 IF.改用位置块.例如如果您只是想为某些文件添加像 Access-Control-Allow-Origin 这样的标头变量,请不要使用 if,而是使用第二个位置块(见下文)

a) Avoid IF in nginx if possible. Use location blocks instead. e.g. If you simply want to add a header variable like Access-Control-Allow-Origin for certain files, do NOT use if, but instead a second location block (See below)

location / {
  // some code here
}
location ~* \.(eot|ttf|woff|woff2)$ {
    add_header Access-Control-Allow-Origin *;
}

b) 如果 IF 是强制性的,请谨慎使用并测试测试.如上所述,在 if 语句中重新声明所有内容是一种很好的做法(如上所述,测试并排除任何重新声明破坏配置的情况).

b) In cases where IF is mandatory, use with caution and test test test. As above, re-declaring everything in the if statement is a good practice (As above suggested, test and rule out whichever re-declaration breaks configuration).

祝你好运.

这篇关于if条件如何在nginx conf中的位置块内工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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