1

Trying to understand how to force www. in my domain and redirect anything like ww.example.com or test.example.com -> www.example.com

Everything works fine when I go to www.example.com or example.com, I'm just trying cover myself if people try going to the wrong domain and I shouldnt have to add anything more than 'www.example.com' and 'example.com' in my django ALLOWED_HOSTS

i set everything up according to:

How To Use the Django One-Click Install Image

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

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

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

    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;
    }
}
masegaloeh
  • 17,978
  • 9
  • 56
  • 104
John
  • 123
  • 4

1 Answers1

1

You haven't define any redirect directive in nginx. Here the proper config.

#here the redirect section. If server doesn't match www.example.com or example.com, it will redirected to http://www.example.com
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name _;

    return 301 http://www.example.com$request_uri;
}

#your main config files, handles request whenever Host header match www.example.com or example.com
server {
    listen 80;
    listen [::]:80;

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

    client_max_body_size 4G;
    server_name www.example.com;

    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; 
    }

    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;
    }
}
masegaloeh
  • 17,978
  • 9
  • 56
  • 104
  • 1
    You cannot have two `server` blocks with port 80 and `default_server` options. So, you should remove `default_server` from the latter block, and also put `www.example.com` to the `server_name` in the latter one. – Tero Kilkanen Aug 29 '14 at 07:02
  • Ooopps, forgot about those mistakes :o. Yes, I simply copy paste from OP config without modifying it. Thanks @TeroKilkanen for another correction :) – masegaloeh Aug 29 '14 at 07:09
  • something still isnt right about it, I made the changes but nginx fails to restart – John Aug 29 '14 at 17:04
  • What's error message did you get? – masegaloeh Aug 29 '14 at 21:49
  • no errors, but i didnt get any redirections – John Aug 29 '14 at 21:52
  • did you have any configuration files inside `/etc/nginx`? Probably there is duplicate of directive `server_name _;`or `default_server`. Of course you can try to `grep` it. – masegaloeh Aug 29 '14 at 22:22
  • checking the error logs, I got this: 2014/08/30 21:03:01 [emerg] 6126#0: duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/django:16 – John Aug 31 '14 at 01:04
  • Try to comment the 'listen [::]:80' line – masegaloeh Aug 31 '14 at 01:08
  • @John Based on this [thread](http://serverfault.com/a/277667/218590), you must specify `ipv6only=on` **once**. See my updated answer. – masegaloeh Aug 31 '14 at 05:26
  • I made the change and everything seems to work ok, thanks! – John Aug 31 '14 at 18:19