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

发布于 2022-09-04 13:34:47 字数 1526 浏览 9 评论 0

服务器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设置有问题。求解。。。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2022-09-11 13:34:47

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文