title: How does if condition work inside location block in nginx conf?
body: <p>I have read 
  <a href="https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/" rel="noreferrer">https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/</a></p>
  
  <p>I want to check if my rails application has already added a header (<code>Access-Control-Allow-Origin</code>) and if it hasn't then add the header.</p>
  
  <p>The examples here have tried to explain the behaviour of <code>if</code> condition in nginx.conf <a href="http://agentzh.blogspot.in/2011/03/how-nginx-location-if-works.html" rel="noreferrer">http://agentzh.blogspot.in/2011/03/how-nginx-location-if-works.html</a></p>
  
  <p>But I have not understood it. One of the question I have is what is meant when they say a directive or phase directive?</p>
  
  <p>I have tried some things myself. Like:</p>
  
  <pre><code>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 '**';
    }
  }
  </code></pre>
  
  <p>Here when the if condition is met, only <code>My-inner-header</code> is set and not the <code>My-outer-header</code>.</p>
  
  <p>But when I do:</p>
  
  <pre><code>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 '**';
    }
  }
  </code></pre>
  
  <p>Here $a variable is set to 1 but only the <code>My-inner-header</code> is set again and not the <code>My-outer-header</code>.</p>
  
  <p>I want to check and ensure if the instructions like <code>proxy_set_header</code> are getting executed or not.</p>