0

I am having a real problem force rewriting my HTTP to HTTPS in nginx.

This is the same issue as rewrite http to https with ngnix behind load balancer

However I would say that my problem is that solution not working.

My configuration:

server {
    listen 80;

    server_name myserver.com *.myserver.com;

    location /health-check {
      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_redirect off;
      proxy_next_upstream error;
      proxy_pass http://localhost:8080;
      break;
    }


    location / {

        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_redirect off;
        proxy_next_upstream error;

        # 2) Any request that did not originally come in to the ELB
        #    over HTTPS gets redirected.
        if ($http_x_forwarded_proto != 'https') {
            return 301 https://$server_name$request_uri;
        }

        proxy_pass http://localhost:8080;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;";
    }
}

With this configuration, a request to www.myserver.com should be redirected with a 301 to https://www.myserver.com, however it isn't.

Any help much appreciated.

  • Note if i add `return 301 https://www.google.com;` above the if check, it does redirect all requests to google. So it seems to be the conditional checking for HTTPS, and thus something to do with ELB – Andrew Plummer Apr 25 '17 at 07:26
  • So if you replace that 301 line, leaving the "if" there, it always forwards to Google when the viewer comes into the ELB on http? When you say "it isn't", can you please edit your question to include "curl -i example.com" and show us the return headers? Before you do the curl add this to the config (may need to be tweaked): add_header X_DEBUG_PROTO $http_x_forwarded_proto – Tim Apr 25 '17 at 17:41

0 Answers0