2016-08-16 92 views
0

如何覆盖变量的先前设置值?在下面的情况下,当$http_x_forwarded_proto = "https"$redirectToHttps未被设置为false但保持为真。我希望这会覆盖价值,但似乎并没有这样做。我怎样才能做到这一点?如何覆盖nginx变量

location/{ 

    set $redirectToHttps false; 
    set $environment "${APP_ENV}"; 

    # determine if we should force https based on the environment 
    if ($environment = "production") { 
     set $redirectToHttps true; 
    } 
    if ($environment = "staging") { 
     set $redirectToHttps true; 
    } 

    # determine if we should force https based on the protocol 
    if ($scheme = "https") { 
     set $redirectToHttps false; 
     return 200 "redirectToHttps-scheme: false"; 
    } 
    if ($http_x_forwarded_proto = "https") { 
     set $redirectToHttps false; 
    } 
+0

您确定'$ http_x_forwarded_proto'实际上设置为'https'吗? – VBart

+0

@VBart - 是的,我已确认。 – Webnet

+0

你如何检查'$ redirectToHttps'是否仍然是'true'? – VBart

回答

1

在nginx的所有变量都只是字符串,有没有这样的东西作为二进制标志像truefalse

按照if指令的documentation

条件可以是以下任意的:

  • 变量名;如果变量的值是空字符串或“0”,则为false;
  • ...

所以truefalse同样评价为真表达if ($var) { ... }