【问题标题】:Nginx https reverse proxy infinite loopNginx https 反向代理无限循环
【发布时间】:2016-01-09 17:07:55
【问题描述】:

这是我用于烧瓶应用程序的站点可用 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 运行

主管配置.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

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 端口是开放的。

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

任何帮助将不胜感激。在此先感谢。

【问题讨论】:

  • 没有。后端不重定向。 @JoeDoherty
  • 发布端点的烧瓶代码。
  • 我没有看到重定向循环顺便说一句,我被拒绝连接。循环是否只存在于 ELB 后面?
  • 抱歉,地址有误。我已将其更改为 example.com。网站在公司内部。因此不能在这里发布。
  • 好的,没问题。不过,我们将需要查看 Flask 配置和一些代码。上面看不到任何剧集。

标签: redirect nginx flask gunicorn supervisord


【解决方案1】:

我确实发现了问题。

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。

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2021-10-21
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多