AnswerBun.com

Setup ssl on nginx for a django project

Server Fault Asked by Mohammad Mahzoun on October 16, 2020

I want to setup ssl for Nginx, my project is a Django and I also use gunicorn as wsgi Http server.

I add following lines in my settings.py code :

CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

I don’t know if it’s necessary to do this, then I configure my Nginx in the following form:

server {
    listen 80;
    server_name <name>;
    return 301 https://$host$request_uri;
}

server {
    #listen 80;
    listen 443 default ssl;
    client_max_body_size 4G;

    server_name <name>;

    #ssl                  on;
    ssl_certificate      /etc/nginx/ssl/ssl.crt;
    ssl_certificate_key  /etc/nginx/ssl/ssl.key;

    ssl_session_timeout  5m;

    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
    keepalive_timeout 5;

# path for static files
    root /home/deploy/;

    location /static/ {
    }
    location /media/ {
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_pass http://app_server;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/deploy/static;
    }
}

Nginx configure is correct I think because its redirect 80 to 443,but nothing happens, 80 request sent, then Nginx redirect it to 443, but nothing happen, it can’t connect to gunicorn or project.

Should I do something with gunicorn? my certificate is self-signed, or what should I do?

regards 🙂

One Answer

You need to add this section to your nginx configuration. upstream is used for proxying requests to your app referenced by proxy_pass:

    upstream app_server {
        server 127.0.0.1:6000; // your gunicorn server
    }

Answered by molivier on October 16, 2020

Add your own answers!

Related Questions

ConvertToHTML converting wrong

1  Asked on February 9, 2021 by jonathan-davies

 

Shibboleth in sub-path URL

1  Asked on February 8, 2021 by j-b

 

How to resize docker volume after creation?

1  Asked on February 8, 2021 by gkman

 

Access Denied DHCP Tool

1  Asked on February 8, 2021 by drew-chapin

     

Custom file/folder permissions with S3FS?

1  Asked on February 8, 2021 by t-brian-jones

         

AWS RDS – Bottleneck

0  Asked on February 6, 2021 by tyralcori

     

Zabbix sensor ‘no data’

1  Asked on February 6, 2021 by jakub-kuszneruk

   

How to enable Ookla script to start automatically after server reboot?

1  Asked on February 5, 2021 by mangaldeep-dey

   

iptables port redirect with Docker

2  Asked on February 5, 2021 by jamescun

   

Where to securely deploy Citrix Netscaler?

1  Asked on February 4, 2021 by user54507

   

Kubernetes: How to mount volumes into Windows pods?

1  Asked on February 4, 2021 by rabejens

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP