本文介绍了Nginx https 反向代理无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 Flask 应用程序的站点可用 nginx 配置

this is my site-available nginx configuration for flask application

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 配置导致无限重定向循环nginx 重定向循环和 ssl.我似乎已经在其中指定了配置.

I have gone through the questionsNginx configuration leads to endless redirect loop and nginx redirect loop with ssl. I seem to have the configuration specified in them already.

编辑

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

Flask application is running via gunicorn/supervisord

Supervisor config.conf

Supervisor 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 gunicorn.conf

Gunicorn 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'

烧瓶应用

运行.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 ..)

注意:我在烧瓶应用程序前面有一个 ELB.80和443端口是开放的.

NB: I have an ELB in front of the flask application. 80 and 443 ports are open.

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

Input: https://example.com/hello/ Output: Redirected Loop

任何帮助将不胜感激.提前致谢.

Any help will be appreciated.Thanks in advance.

推荐答案

我确实解决了这个问题.

I did figure out the issue.

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.

As ELB does an unloading of HTTPS encryption to HTTP request , my previous configuration was redirecting all my HTTP requests into HTTPS.

这篇关于Nginx https 反向代理无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 16:57