Nginx + Python, Behind Multiple Proxies

Use in programming (Python / Django) a method HttpRequest.gethost () (# https://docs.djangoproject.com/en/dev/r … -response/">https://docs.djangoproject.com/en/dev/ref/request-response/ django.http.HttpRequest.gethost) but this method fails when the host is behind multiple proxies. In my application redirect some subdomains for her, and through the method mentioned recognize the subdomain in question.

Normally operates. The former method get the URI. (like classificados.fatimanews.com.br, classificados.bonitoinforma.com.br) but today I created a subdomain (classificados.portalaguasclaras.com.br), pointed to the IP of the machine, as I always do, and it happens that this time the method returns the IP of the machine and not the URL for me to identify it.

I use nginx. What would it take? Could you help me?

9 Replies

I don't know which HTTP header exactly Django recognizes, I use Pyramid. And for that I set a combination of:

proxysetheader Host $host;

proxysetheader X-Real-Ip $remote_addr;

proxysetheader X-Forwarded-For $proxyaddxforwardedfor;

or if you run Django under uwsgi, it's uwsgipassheader, etc… See nginx docs for more info on that.

I tried that and it did not work

uwsgipassheader Host;

proxysetheader Host $http_host;

proxypassheader Host;

What I doing wrong? I really need it.

Thanks man for any help.

This is what I pass when using uwsgi

uwsgiparam QUERYSTRING $query_string;

uwsgiparam REQUESTMETHOD $request_method;

uwsgiparam CONTENTTYPE $content_type;

uwsgiparam CONTENTLENGTH $content_length;

uwsgiparam REQUESTURI $request_uri;

uwsgiparam PATHINFO $document_uri;

uwsgiparam DOCUMENTROOT $document_root;

uwsgiparam SERVERPROTOCOL $server_protocol;

uwsgiparam REMOTEADDR $remote_addr;

uwsgiparam REMOTEPORT $remote_port;

uwsgiparam SERVERPORT $server_port;

uwsgiparam SERVERNAME $server_name;

You probably want the bottom one.

I already use these settings.

It's not working,

I'm almost giving up a vps. :/

@onovaes:

I tried that and it did not work

uwsgipassheader Host;

proxysetheader Host $http_host;

proxypassheader Host;

What I doing wrong? I really need it.

Thanks man for any help.

You're mixing uwsgi and proxy pass. which is it? uwsgi or proxy?

Sorry, but I do not know what I want. I do not know is if I want uwgi or proxies.

I just want to resolve the error of the initial question.

I can not get the URL when using the HttpRequest.get_host () in Django / Python in some cases. Usually works, but in some cases, instead of receiving the URL, IP comes.

My specialty is not servers.

Excuse my ignorance.

If you paste your config file here, it will be quite useful for figuring out what your configuration is.

my current settgins

location / {

uwsgipass djangoappskalist;

uwsgiparam CONTENTTYPE $content_type;

uwsgiparam CONTENTLENGTH $content_length;

uwsgiparam REQUESTURI $request_uri;

uwsgiparam PATHINFO $document_uri;

uwsgiparam DOCUMENTROOT $document_root;

uwsgiparam SERVERPROTOCOL $server_protocol;

uwsgiparam REMOTEADDR $remote_addr;

uwsgiparam REMOTEPORT $remote_port;

uwsgiparam SERVERPORT $server_port;

uwsgiparam SERVERNAME $server_name;

}

You are using uWSGI.

In my world, we use gunicorn (and therefore HTTP instead of uWSGI) and our front-end proxies pass the remote IP address in the X-Real-IP header, so we just proxypassheader X-Real-IP. So, I'm mostly guessing here. You'll probably need to do something like this:

uwsgi_param REMOTE_ADDR $http_x_real_ip;

Things will be different if your front-end isn't passing you X-Real-IP.

My google-fu was failing out on finding an example of uWSGI+nginx+X-Real-IP, unfortunately, so your mileage may vary. This is, in part, why I use gunicorn.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct