Django How to redirect only example.com to https and *.example.com to http?


Django How to redirect only example.com to https and *.example.com to http?



I need http://example.com to redirect to https://example.com. Whereas http://www.example.com, http://api.example.com must not redirect i.e, subdomains need not redirect to https.



I can understand the configuration here by looking at it. But don't know to move further. Please help me.
My configuration so far is:



sites-available/default.py


upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
return 301 https://example.com$request_uri;
}

server {
server_name example.com www.example.com cloud.example.com api.example.com;

listen 443; # <-

ssl on; # <-
ssl_certificate /etc/ssl/example_cert_chain.crt; # <-
ssl_certificate_key /etc/ssl/example.key; # <-
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/media;
}

# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/static/;
}

# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; # <-
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;

proxy_pass http://app_server;
}

}




1 Answer
1



Edit this:


server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
return 301 https://example.com$request_uri;
}



Into:


server {
server_name example.com www.example.com cloud.example.com api.example.com;
listen 80;
if ($http_host = "example.com") {
rewrite ^ https://example.com$request_uri permanent;
}
}





nginx throws error: nginx[28994]: nginx: [emerg] unknown directive "if($http_host" in /etc/nginx/sites-enabled/django:8
– whitehathackersree
Jul 1 at 11:55



nginx[28994]: nginx: [emerg] unknown directive "if($http_host" in /etc/nginx/sites-enabled/django:8





Edited it just now, give it a go, checked it with nignx -t just in case :) @whitehathackersree
– Dusan Gligoric
Jul 1 at 12:27





Thank you :) @Dusan
– whitehathackersree
Jul 1 at 14:29






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?