how to run https supported django application in gunicorn/nginx

May 23, 2016 817 views
DigitalOcean Django

I am using 'sslserver' in django but here not able open the url as https with out using ssl certificate

if i use "python runsslserver" able to access url as https but in gunicorn, Please guide me how to do this.

1. /etc/init/gunicorn.conf

description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

If the process quits unexpectedly trigger a respawn

setuid root
setgid root
chdir /root
exec gunicorn \
--name=zephyr \
--pythonpath=zephyr \
--bind= \
--config /etc/gunicorn.d/ \

exec /root/myenv/bin/gunicorn --workers 3 --bind unix:/root/myproj/myproj.sock myproj.wsgi:application

  1. sudo service gunicorn start 3 . /etc/nginx/sites-enabled/django ---------------------------------------------- upstream appserver { server failtimeout=0; } server { listen 80; listen [::]:80 ipv6only=on; return 301 https://$host$requesturi; } server { listen 443 defaultserver; listen [::]:443 defaultserver ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; clientmaxbodysize 4G; servername; keepalivetimeout 5; addheader Strict-Transport-Security "max-age=31536000; includeSubdomains;"; location /media { alias /root/myproj/media; } # your Django project's static files - amend as required location /static { alias /root/myproj/staticfiles; } location / { proxysetheader X-Forwarded-For $proxyaddxforwardedfor; proxysetheader Host $httphost; proxyredirect off; proxypass http://app_server; } #ssl on; #sslcertificate /etc/nginx/ssl/server.crt; #sslcertificate_key /etc/nginx/ssl/server.key; }
  2. sudo service nginx restart
  3. gunicorn myproj.wsgi:application --bind


1 Answer

To run guicorn from the CLI in the manner you're describing, you may need to define a --keyfile and --certfile in order for it.

Because you referenced your Nginx setup (which had these values commented out for the self-signed cert), it looks like there may be something getting mixed up in how to connect the two; I recommend looking at this sample setup for running gunicorn with Nginx:

It contains a sample configuration for a known-working configuration, which should be able to get you started.


Joseph D. Marhee
Customer Success Engineer

Have another answer? Share your knowledge.