nginx http redirect duplica erroneamente os parâmetros (via AWS ELB)

1

Por exemplo, quando eu navego

http://example.com/foo?x=1&y=2

nginx me redireciona para

https://example.com/foo?x=1&y=2?x=1&y=2

Agora, se eu continuar redirecionando, obtenho

https://example.com/foo?x=1&y=2?x=1&y=2?x=1&y=2?x=1&y=2

Ele continua dobrando os parâmetros, sem saber o que eu fiz de errado.

Minha configuração do nginx:

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;
  }
}

Essa é uma arquitetura do ECS com contêineres nginx e de aplicativo na mesma instância do EC2.

    
por Casper 05.01.2018 / 00:31

1 resposta

5

A diretiva rewrite anexa a string de consulta à URL de substituição por padrão, de maneira semelhante ao [QSA] do Apache. A partir da documentação :

If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:

rewrite ^/users/(.*)$ /show?user=$1? last;

Mas não é como você deve corrigir o problema.

Isso tem mais um problema, pois o redirecionamento http para https é ineficiente. Esse if deve ser avaliado em todas as solicitações , e o rewrite tem uma regex gratuita. Consulte Reescrita de impostos para obter mais informações.

Em vez disso, você deve ter um bloco server completamente separado para HTTP versus HTTPS e remover completamente if / rewrite do bloco HTTPS server .

server {
  listen 80;
  listen [::]:80; # You also forgot this...

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

  return 301 https://{{ .SERVER_NAME }}$request_uri;
}
    
por 05.01.2018 / 01:12