When using Nginx to proxy the flask application started by Gunicorn, does redirect ignore the port?
ringa_lee
ringa_lee 2017-05-16 17:09:30
0
1
629

The server Gunicorn runs on port 8000. Nginx listens on 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;
        }

blueprint is used, divided into main and auth

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

This is the login view function:

@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)

When executing login, it jumps from ip:443/auth/login to ip/index, directly ignoring the port. However, there is no such problem when accessing Gunicorn directly. It is suspected that there is a problem with Nginx settings. Solve. . .

ringa_lee
ringa_lee

ringa_lee

reply all(1)
伊谢尔伦

Solution: nginx settings: Ignore the proxy port, just add it
proxy_set_header Host $host:$server_port;
For example:
If you set proxy_set_header Host $host:2345;, when redirect('index'), Your flask instance will be redirected to $host:2345/index

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!