title: nginx proxied redirects use the port number of the proxy, not the host
body: <p>I'm setting up a web/app/db stack, and the nginx proxy configuration isn't working the way I thought it would.</p>
  
  <p>so here is an example of the stack...the url of the application is:
  <a href="https://testapp.com" rel="nofollow noreferrer">https://testapp.com</a></p>
  
  <p>here is the nginx config:</p>
  
  <pre><code>   server {
     listen       8886;
     server_name  _;
     root         /usr/share/nginx/html;
     include /etc/nginx/default.d/*.conf;
  
     #ELB
     if ($http_user_agent = 'ELB-HealthChecker/2.0') {
      return 200 working;
     }
  
     #HTTP to HTTPS
     if ($http_x_forwarded_proto != 'https') {
         return 301 https://$host$request_uri;
     }
     location / {
          set $proxy_upstream_name "testapp.com";
          port_in_redirect off;
          proxy_pass         http://internal-alb.amazonaws.com:8083/;
          proxy_redirect     off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
          proxy_set_header   Access-Control-Allow-Origin $http_origin;}
  </code></pre>
  
  <p>The app is proxied to an internal AWS alb, and it forwards it to a single (at this point) application server.</p>
  
  <p>I'm able to get the site to serve.  However, the application creates a redirect on login, and I get the following response.</p>
  
  <pre><code>Request URL:https://testapp.com/login
  Request Method:POST
  Status Code:302 
  Remote Address:34.192.444.29:443
  Referrer Policy:no-referrer-when-downgrade
  Response Headers
  content-language:en-US
  content-length:0
  date:Mon, 11 Sep 2017 18:35:34 GMT
  location:http://testapp.com:8083/testCode
  server:openresty/1.11.2.5
  status:302
  </code></pre>
  
  <p>The redirect fails because it's being served on 443, not 8083.</p>
  
  <p>For some reason the app or the proxy isn't updating the port as it doing it's reverse proxy thing, so that the redirect has the proxied port NOT the actual application port 443.</p>
  
  <p>What do I need to do with nginx config to get it to redirect correctly.</p>
  
  <p>thanks.
  myles.</p>