首页  >  问答  >  正文

用Nginx代理Gunicorn启动的flask应用时,redirect忽略了端口?

服务器Gunicorn运行在8000端口。Nginx监听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,分为main和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')

这是登录的view函数:

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

执行登录时,从ip:443/auth/login跳转到了ip/index,直接忽略掉了端口。然而直接访问Gunicorn就没有这种问题,怀疑是Nginx设置有问题。求解。。。

ringa_leeringa_lee2714 天前638

全部回复(1)我来回复

  • 伊谢尔伦

    伊谢尔伦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

    回复
    0
  • 取消回复