Heim  >  Fragen und Antworten  >  Hauptteil

Ignoriert die Umleitung den Port, wenn Nginx als Proxy für die von Gunicorn gestartete Flask-Anwendung verwendet wird?

Der Server Gunicorn läuft auf Port 8000. Nginx hört auf 443. . .

server {
        listen 443;
        server_name _;

        location / {
                proxy_pass http://127.0.0.1:8000;
                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;
        }

        location /static {
                alias /home/tiweb/SecPostsWeb/app/static;
        }

Verwenden von Blueprint, unterteilt in Haupt- und Authentifizierungsschlüssel

from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

Dies ist die Login-Ansichtsfunktion:

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.objects(email=form.email.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user, form.remember_me.data)
            return redirect(request.args.get('next') or url_for('main.index'))
        flash('Invalid username or password.')
    return render_template('auth/login_v3.html', form=form)

Beim Ausführen der Anmeldung wird von ip:443/auth/login zu ip/index gesprungen, wobei der Port direkt ignoriert wird. Beim direkten Zugriff auf Gunicorn tritt ein solches Problem jedoch nicht auf. Es wird vermutet, dass ein Problem mit den Nginx-Einstellungen vorliegt. Lösen. . .

ringa_leeringa_lee2714 Tage vor630

Antworte allen(1)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-05-16 17:11:30

    解决方案:nginx设置:忽略了代理的端口,添加上就好
    proxy_set_header Host $host:$server_port;
    例如:
    如果,你设置proxy_set_header Host $host:2345;,当redirect('index'),你flask实例就会重定向到$host:2345/index

    Antwort
    0
  • StornierenAntwort