Q: Flask Redirect Creates Invalid URL with Nginx and Certbot I have done a ton of research and altered the code to try to fix this but nothing is working. Flask route redirection is now working properly with Nginx and Certbot. Getting to the website works fine , clicking a link in the navigation works fine, but when Flask tries to redirect to another URL (Flask route) it creates a invalid URL that looks like this:
https://example.com/signup

After submitting the form (POST) request this is what I see for the URL.

https://example.com,example.com/login

Here is the Flask Python code:
@main.route('/signup', methods=['GET', 'POST'])
def sign_up():
    '''sign up user'''
    
    form = SignupForm()
    if form.validate_on_submit():
        return redirect(url_for('main.login'))
    return render_template('main/signup.html', form=form)

@main.route('/login', methods=['GET', 'POST'])
def login():
    '''login user'''

    form = LoginForm()
    if form.validate_on_submit():
        pass
    return render_template('main/login.html', form=form)


The Nginx website configuration:
server {

    server_name example.com www.example.com;
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/example/example.sock;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP     $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name example.com www.example.com;
    return 404; # managed by Certbot

}


If I type this manually it works, which means that the Python/Flask code is okay.
https://example.com/login


A: The solution is in the Nginx location block. Notice the line include proxy_params;. It ended up including the same information twice.
location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/example/example.sock;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP     $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    }

Since the file /etc/nginx/proxy_params included the proxy_set_header information I did not have to add it again. The location block now looks like this.
location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/example/example.sock;
    }

