Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + uWSGI + Nginx + SSL - request for working configuration (emphasis on SSL)

Does anyone have a working configuration for these four?

- Django
- uWSGI
- Nginx
- SSL 

The main question is how to correctly set up SSL for this? I've googled a lot, and still can't get it to work. I have a working set up for http with unix sockets, but that's as far as I could get.

There are some other answers posted, but they are mostly code snippets, and not a whole configuration.

like image 911
Aaron Lelevier Avatar asked Apr 23 '15 14:04

Aaron Lelevier


People also ask

What is Django uWSGI?

uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C. See also. The uWSGI docs offer a tutorial covering Django, nginx, and uWSGI (one possible deployment setup of many). The docs below are focused on how to integrate Django with uWSGI.


2 Answers

server {
    listen          80;
    server_name     example.com;
    rewrite ^/(.*)  https://example.com/$1 permanent;
}

server {
    listen          443 ssl;
    server_name     example.com;
    access_log      /var/log/nginx/example.com_access.log combined;
    error_log       /var/log/nginx/example.com_error.log error;

    ssl_certificate         /etc/nginx/ssl/example-unified.crt;
    ssl_certificate_key     /etc/nginx/ssl/example.key;

    location /static/ {
        alias /webapps/example/static/;
    }

    location /media/ {
        alias /webapps/example/media/;
    }

    location / {
        proxy_pass         http://localhost:8000/;
        proxy_redirect     off;

        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
        proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    }

}

This is basic nginx configuration that will work with SSL and will forward requests to uwsgi running on port 8000 (you can change this to socket if you want).

For advanced SSL settings check THIS.

like image 64
Domen Blenkuš Avatar answered Sep 30 '22 14:09

Domen Blenkuš


I am new to nginx,uwsgi and ssl. Here shares my testing nginx and uwsgi config.

Basically, there are four steps to deploy Django only support SSL/HTTPS.

  1. Setup a SSL Certificate
    • use openssl to generate server.crt and server.key

      openssl req -new -x509 -nodes -out server.crt -keyout server.key

  2. Config nginx.conf and uwsgi.ini under Django project
    • Set nginx.conf (sorry,the layout is weird in text block, so I insert a picture here.) enter image description here
    • symlink to this file from /etc/nginx/sites-enabled so nginx can see it

      sudo ln -s /path/to/django/example_nginx.conf /etc/nginx/sites-enabled/

    • config uwsgi.ini under django project enter image description here
  3. Config settings.py

    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
    SECURE_SSL_REDIRECT = True
    
  4. Restart nginx and uwsgi

    • restart nginx

      sudo /etc/init.d/nginx restart

    • run uwsgi

      uwsgi --ini /path/to/django/example_uwsgi.ini

like image 38
lily LIU Avatar answered Sep 30 '22 13:09

lily LIU