Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.is_secure() always returns false with uwsgi server

We are using Ngnix + uWSGI setup for our Django-based application.

Our problem is that request.is_secure() always returns false even though we are serving content on https.

As mentioned in the uWSGI documentation, I have set uwsgi_param UWSGI_SCHEME $scheme in nginx configuration or uwsgi_params, but it is of no use.

We also have Nginx + apache based setup for the same application, there it works just fine.

Any help will be appreciated.

Thanks in advance.

like image 575
pankajanand18 Avatar asked Jul 11 '12 21:07

pankajanand18


3 Answers

For gunicorn server fixed with:

Add this line to nginx.conf file:

proxy_set_header        X-Forwarded-Proto           $scheme;

And add this line to settings.py file:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
like image 71
Omid Raha Avatar answered Nov 15 '22 10:11

Omid Raha


Problem solved !!

We tried couple of things to fix it but couldn't get it working. I will put some more information here about the problem that we analyzed later on. Our current setup on webfaction looks like this :

WebFaction Nginx -> our nginix -> uwsgi server

what we found that there was some problem in configuration of webfaction nginx that it was passing all the traffic of (https and http) to our nginx on http protocol itself. So first we changed this setup to pass on the right traffic to right server.

Still we found that $scheme set by both nginx server is not correct, so what we finally did is to set the following in our nginx for https configuration:

uwsgi_param UWSGI_SCHEME https;

this solved the problem as of now.

like image 44
pankajanand18 Avatar answered Nov 15 '22 10:11

pankajanand18


I had half a day of hell fixing this issue, so saving everyone the headache

Firstly,

set

uwsgi_param   HTTP_X_FORWARDED_PROTO    $scheme;

in nginx.conf

Then drop this line into settings.py

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

If you're on Django<1.4, you'll also need to enable the django-secure middleware

like image 24
pragman Avatar answered Nov 15 '22 11:11

pragman