1

Hi im having trouble with my first Django site and not sure what im doing wrong.

I basically want to redirect http:// www. example.com and http:// example.com and https:// www. example.com all to > https://example.com

The redirect from http:// example.com seems to work.

This is what my conf file looks like.

server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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/django_project/media;
}

# your Django project's static files - amend as required
location /static {
    alias /home/django/django_project/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 Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
}


}

server {
listen 80;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}


server {
listen 80;
server_name example.com;
return 301 $scheme://$host$request_uri;
}



server {
listen [::]:443 ssl;
listen 443 ssl;
server_name www.example.com;
return 301 $scheme://$host$request_uri;
}

What am i doing wrong?

The answer from Richard has solved my problem.

  • See [this answer](http://stackoverflow.com/questions/42228191/nginx-redirect-non-www-to-www-https/42230968#42230968) for guidance. – Richard Smith Apr 16 '17 at 17:56
  • Possible duplicate of [Nginx no-www to www and www to no-www](http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www) – Dmytriy Voloshyn Apr 16 '17 at 18:48
  • Hey Richard, thanks so much, this almost works, the www now also redirects to the secure example.com. However, when i visit https://www.example.com it claims my certificates are not correct. ("They are meant for example.com and not www.example.com") Edit: i get this error with Firefox, but not with Chrome. I would think that the certificate would only be for the redirected url, and not for the original url – WalterTreeglove Apr 16 '17 at 18:51
  • Ah having extra certificates for the www part solved this also. Thanks alot! – WalterTreeglove Apr 16 '17 at 19:02

0 Answers0