Lukasz Dynowski
Lukasz Dynowski

Reputation: 13700

Missing nginx headers in uWSGI application (flask)

My python application (Flask) runs behind uWSGI (application server), and in the front of Nginx (http server), and is packaged in docker container. What I try to achieve is to forward real IP address from http server (Nginx) into my app server (uWSGI).

To do, so I specify X-Real-IP and X-Forwarded-For headers in my nginx config. Unfortunately, inspecting flask's request.headers I can only see Host header and remaining two are missing.

The same setup works with gunicorn, and there my headers are present. Any idea how to fix it?

server {
  listen 80;
  server_name app.local;

  ## uWSGI setup for API
  location /api {
    include       /etc/nginx/uwsgi_params;
    uwsgi_pass    unix:///var/run/app.uwsgi.sock;
    uwsgi_param   Host                  $host;
    uwsgi_param   X-Real-IP             $remote_addr;
    uwsgi_param   X-Forwarded-For       $proxy_add_x_forwarded_for;
  }
}

Upvotes: 0

Views: 2087

Answers (1)

Lukasz Dynowski
Lukasz Dynowski

Reputation: 13700

I found a solution. I just had to prefix my X-HEADERS with HTTP. Here is an example.

# From
uwsgi_param   X-Real-IP         $remote_addr;
uwsgi_param   X-Forwarded-For   $proxy_add_x_forwarded_for;

# To
uwsgi_param   HTTP_X-Real-IP        $remote_addr;
uwsgi_param   HTTP_X-Forwarded-For  $proxy_add_x_forwarded_for;

Upvotes: 5

Related Questions