禁用 nginx 中特定路径的 https 会导致 ELB 后面的实例进入重定向循环

禁用 nginx 中特定路径的 https 会导致 ELB 后面的实例进入重定向循环

我正在使用 elb 终止 SSL。我想强制 /product/performancesummaries/ 仅使用 HTTP。这是我正在尝试的当前配置:

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 1;
        gzip_buffers 32 8k;
        gzip_http_version 1.1;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        # uWSGI serving Django.
        upstream django {
            # Distribute requests to servers based on client IP. This keeps load
            # balancing fair but consistent per-client. In this instance we're
            # only using one uWGSI worker anyway.
            ip_hash;
            server unix:/tmp/uwsgi.sock;
        }

  server {
    listen      80;
    server_name www.7geese.com *.amazonaws.com;
    charset     utf-8;


    # Your project's static media.
    location /static/ {
      alias /home/ubuntu/.virtualenvs/7geese/sevengeese/static_files/;
    }

    location /product/performancesummaries/ {
      if ($http_x_forwarded_proto = "https") {
          rewrite ^ http://$host$uri permanent;
      }
      uwsgi_pass  django;
      include     uwsgi_params;
    }

    # Finally, send all non-media requests to the Django server.
    location / {
      if ($http_x_forwarded_proto != "https") {
          rewrite ^ https://$host$uri permanent;
      }
      uwsgi_pass  django;
      include     uwsgi_params;
    }
  }

}

当我访问/产品/性能概要/,它会进入无限重定向循环,从 http 重定向到 https 再重定向到 http 等等。为什么它会进入无限重定向循环以及我该如何停止它。

答案1

您有两个location块,其中一个块将 HTTPS 重定向到 HTTP /product/performancesummaries/

  if ($http_x_forwarded_proto = "https") {
      rewrite ^ http://$host$uri permanent;
  }

另一个将 HTTP 重定向到 HTTPS /(即所有内容):

  if ($http_x_forwarded_proto != "https") {
      rewrite ^ https://$host$uri permanent;
  }

这可能是问题的原因,尽管我不完全确定为什么location要处理这两个 s。nginx 似乎可以做到这一点在少数情况下。

我会尝试通过以下方式修复此问题排除 /product/performancesummaries/location /HTTP 到 HTTPS 重定向:

      rewrite ^((?!/product/performancesummaries/).) https://$host$uri permanent;

答案2

上面描述的配置没有任何问题。无限循环是由于我的 django 应用程序中的重定向将 http 请求重定向到 https 而导致的。哎呀!

相关内容