title: Nginx Multilevel reverse Proxy
body: <p>Nginx Setup</p>
  <pre><code>AKAMAI
  |                                                                    +---&gt; Nginx port 82
  |                                                                    |
   -&gt; AWS ELB/ALB -&gt; Nginx Server(Port 80) -&gt; Docker (Nginx Port 81) --|
                                                                       |
                                                                       +----&gt; Nginx port 83
  
  
  </code></pre>
  <p>Browser sents request on HTTPS but Nginx server running on port 82/83 receiving x-forwarded-proto/scheme  as <code>http</code> instead of <code>https</code></p>
  <p>Nginx Port 80 config</p>
  <hr />
  <pre><code>http {
      ...
      server {
          ...
          listen 80;
          location / {
             proxy_set_header   X-Forwarded-For $remote_addr;
             proxy_set_header   Host $http_host;
             proxy_pass         &quot;http://127.0.0.1:81&quot;;
          }
          proxy_set_header HOST $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Scheme $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_redirect off;
      }
  }
  
  
  </code></pre>
  <h2>Nginx Port 81 config</h2>
  <pre><code>server {
      listen       81;
      location ~ ^/v1/test {
          proxy_pass http://127.0.0.1:82;
          proxy_set_header HOST $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Scheme $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_redirect off;
          break;
      }
  
      location / {
          proxy_set_header HOST $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Scheme $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_redirect off;
          proxy_pass http://127.0.0.1:83;
      }
  }
  </code></pre>
  <h2>Nginx Port 82 Config</h2>
  <pre><code>server {
      ...
      listen       82;
      proxy_set_header HOST $host;
      proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
  }
  
  </code></pre>
  <h2>Nginx Port 83 Config</h2>
  <pre><code>server {
      ...
      listen       83;
      proxy_set_header HOST $host;
      proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
  }
  
  </code></pre>
  <p>Applications running on port 82/83 are rails applications and use Passenger to serve the traffic.</p>
  <p>Even we tried setting as below in 82/83 Nginx server configs</p>
  <pre><code>proxy_set_header X-Forwarded-Proto https;
  proxy_set_header X-Forwarded-Scheme https;
  </code></pre>