Search code examples
pythonnginxflaskgunicornwerkzeug

Flask nginx proxy fix duplicates headers


I have a flask server deployed with nginx and gunicorn. I read this and have nginx setup to set these headers and have the werkzeug middleware proxy fix in place. Now the headers coming in on flask have the right ip addresses and hosts and stuff. Except for 1 small problem. The headers show up as duplicates separated by commas. Here's what I see when printing request.headers.

Host: site.com,site.com
X-Real-Ip: 12.345.67.898,12.345.67.898
X-Forwarded-For: 12.345.67.898,12.345.67.898
X-Forwarded-Proto: https,https

These headers, are the ones that nginx overrides. Why do they all repeat. They are repeated and divided by a comma. How can I fix this behavior? I have the nginx config and the werkzeug middleware proxy fix done exactly is in the example. Here's my nginx config more or less.

server {
    # dont worry about ssl, certbot handles that
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name site.com www.site.com;
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/site.sock;
        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;
    }
}

Solution

  • Richard Smith solved this problem. And that was the include proxy_params; line. I cat /etc/nginc/proxy_params and it contained the exact proxy_set_header lines that where already included. Bassically it set those headers twice. I have to either delete the include proxy_params line or delete the manual proxy_set_header lines.