nginx http redirect wrongfully doubles the parameters (via AWS ELB)

by Casper   Last Updated January 05, 2018 00:00 AM

For example, when I browse

nginx redirects me to

Now if I continue to redirect, I get

It keeps doubling the parameters, not sure what did I do wrong.

My nginx config:

server {
  listen 80;
  listen 443 ssl;

  server_name {{ .SERVER_NAME }} www.{{ .SERVER_NAME }};

  ssl_certificate     /etc/ssl/nginx.crt;
  ssl_certificate_key /etc/ssl/nginx.key;

  if ($http_x_forwarded_proto != "https") {
      rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;

  # Nginx will reject anything not matching /
  location / {
    # Reject requests with unsupported HTTP method
    if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE|PATCH)$) {
      return 405;

    # Only requests matching the whitelist expectations will
    # get sent to the application server
    proxy_pass http://site_container:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_redirect     off;
    proxy_set_header   Host                 $http_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-Proto    $http_x_forwarded_proto;
    proxy_set_header   X-Forwarded-Port     $http_x_forwarded_port;
    proxy_cache_bypass $http_upgrade;

This is an ECS architecture with both nginx and app containers on the same EC2 instance.

Related Questions

How to make AWS containers communicate

Updated January 02, 2018 23:00 PM

AMI Instance redirecting

Updated September 19, 2017 17:00 PM

Nginx container health-check for AWS-ALB

Updated September 18, 2017 15:00 PM

Docker reverse proxy incorrect host redirect

Updated April 05, 2018 14:00 PM

docker nginx proxy: error ERR_TOO_MANY_REDIRECTS

Updated November 01, 2015 13:00 PM