Django project and Perfect setup of nginx config for www to nonwww and http to https and ip to domain redirects
Here I am sharing good setup of nginx config for following requirements.
before following config you need to create 2 A record for both domain to ip and www.domain to ip in your dns provider.
also for django settings file you can add domain and www.domain to ALLOWED_HOSTS
- http to https
- www to nonwww
- https://ip to domain
- http://ip to domain
redirects
place following config to your sites-enabled config file
server {
listen 80;
listen 443 ssl;
server_name yourserverip;
ssl_certificate /home/yourdomain.com.crt;
ssl_certificate_key /home/yourdomain.com.key;
add_header X-Frame-Options "SAMEORIGIN";
return 301 https://yourdomain.com$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /home/yourdomain.com.crt;
ssl_certificate_key /home/yourdomain.com.key;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS +RC4 RC4";
location /static/ {
alias /home/projectpath/staticfiles/;
}
location /media/ {
alias /home/projectpath/media/;
}
location / {
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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/projectpath/gunicorn.sock;
}
}
server {
listen 443 ssl;
server_name www.yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
server {
listen 80;
server_name yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
Comments