用Nginx代理Gunicorn啟動的flask應用程式時,redirect忽略了連接埠?
ringa_lee
ringa_lee 2017-05-16 17:09:30
0
1
628

伺服器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_lee
ringa_lee

ringa_lee

全部回覆(1)
伊谢尔伦

解決方案:nginx設定:忽略了代理的端口,添加上就好
proxy_set_header Host $host:$server_port;
例如:
如果,你設定proxy_set_header Host $host:2345;,當redirect('index'),你flask實例就會重定向到$host:2345/index

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!