The server Gunicorn runs on port 8000. Nginx listens on 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 is used, divided into main and 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')
This is the login view function:
@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)
When executing login, it jumps from ip:443/auth/login to ip/index, directly ignoring the port. However, there is no such problem when accessing Gunicorn directly. It is suspected that there is a problem with Nginx settings. Solve. . .
Solution: nginx settings: Ignore the proxy port, just add it
proxy_set_header Host $host:$server_port;
For example:
If you set proxy_set_header Host $host:2345;, when redirect('index'), Your flask instance will be redirected to $host:2345/index