我准备按照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中的设置有问题?
确实是我的配置出现问题了。nginx上的设置没有说明端口号。
proxy_set_header Host $host;
应该改为:proxy_set_header Host $host:$server_port;