python - flask+nginx+gunicorn返回index.html时出错

 iar2984165 发布于 2022-11-04 17:08

我准备按照flask + gunicorn + nginx建立一个小的web app。在本地测试都没有问题。只不过后面用到gunicorn和nginx的时候出现了问题。

问题描述:flask里面的view.py指定redirect(url_for('main.index'))时,网页会自动跳转到http://ip-address,正常的应该是跳转到http://ip-address:1025/

view.py中的redirect代码,想登录后重定向到index.html。

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(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.html', form=form)

nginx中的设置:

worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       1025;
        server_name  127.0.0.1:8080;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            proxy_pass http://127.0.0.1:8080;   # gunicorn host address
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

gunicorn 用gunicorn -w 2 -b 127.0.0.1:8080 manage:app来启动。
不知道是不是nginx中的设置有问题?

1 个回答
  • 确实是我的配置出现问题了。nginx上的设置没有说明端口号。
    proxy_set_header Host $host; 应该改为: proxy_set_header Host $host:$server_port;

    2022-11-08 05:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有