2016-08-16 118 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) { ... }