Nginx https反向代理死循环

Nginx https reverse proxy infinite loop

这是我为 flask 应用程序提供的站点可用 nginx 配置

server {
    listen                     80;
    server_name                _;
    access_log                 /var/log/nginx/nginx_access.log;
    error_log                  /var/log/nginx/nginx_error.log;
    rewrite ^ https://$http_host$request_uri? permanent;
}

server {
    listen                     443;
    server_name                _;
    access_log                 /var/log/nginx/nginx_access.log;
    error_log                  /var/log/nginx/nginx_error.log;

    ssl                        on;
    ssl_certificate            /etc/nginx/ssl/<redacted>.pem;
    ssl_certificate_key        /etc/nginx/ssl/<redacted>.key;
    ssl_session_timeout        5m;
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers  on;

    location / {
        proxy_pass             http://127.0.0.1:5000;
        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-Proto $scheme;
        }
}

我已经完成了问题 Nginx configuration leads to endless redirect loopnginx redirect loop with ssl。我似乎已经在其中指定了配置。

编辑

Flask 应用程序是 运行 通过 gunicorn/supervisord

主管config.conf

[program:config]
command=/usr/local/bin/gunicorn run:app --config /etc/gunicorn/gunicorn.conf --preload
directory=/srv/<application>
autostart=true
autorestart=true
startretries=10
stderr_logfile = /var/log/supervisord/<application>-stderr.log
stdout_logfile = /var/log/supervisord/<application>-stdout.log
user=root

独角兽gunicorn.conf

bind = '0.0.0.0:5000'
backlog = 2048
workers = 3
worker_class = 'sync'
worker_connections = 1000
timeout = 30
keepalive = 2
accesslog='/var/log/gunicorn/gunicorn_access.log'
errorlog='/var/log/gunicorn/gunicorn_error.log'
pidfile = '/tmp/gunicorn.pid'
loglevel = 'debug'

烧瓶应用程序

run.py

from app import app
from app import views

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

view.py

from app import app, session
from flask import render_template, json, jsonify
import datetime



@app.route("/hello/")
def render_templates():
    return render_template("display.html")

(... other code ..)

注意:我在 flask 应用程序前面有一个 ELB。 80 和 443 端口已打开。

输入:https://example.com/hello/输出:重定向循环

任何帮助都会提前appreciated.Thanks。

我确实弄清楚了问题。

nginx 配置应该是

server {
    listen                     80;
    server_name                _;
    access_log                 /var/log/nginx/nginx_access.log;
    error_log                  /var/log/nginx/nginx_error.log;

    location / {
        proxy_pass             http://127.0.0.1:5000;
        proxy_set_header       Host $host;
        proxy_set_header       X-Real-IP $remote_addr;
        proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

由于 ELB 将 HTTPS 加密卸载到 HTTP 请求,我之前的配置是将所有 HTTP 请求重定向到 HTTPS。