3

I followed the instructions at https://stackoverflow.com/a/11733363/2532070 to redirect www to non-www. I'm trying to redirect the following formats:

http://example.com
http://www.example.com
https://www.example.com

all to:

https://example.com

http://example.com is redirecting to https. However, the other two, with www., are not. Here is my nginx.conf:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}
#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example.com$request_uri;
}

#
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      /src/bin/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/private/STAR_example_com.key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    # rest goes here...

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

    client_max_body_size 4G;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /src/media;
    expires 1y;
    add_header Cache-Control "public";
    }

    # your Django project's static files - amend as required
    location /static {
        alias /src/static;
    expires 1y;
    add_header Cache-Control "public";
    }

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

I'm not seeing anything in the nginx log file to indicate the error. Is there somewhere to look for the error when I try to access a www. version? Thanks!

Community
  • 1
  • 1
YPCrumble
  • 26,610
  • 23
  • 107
  • 172

1 Answers1

3

You are most likely experiencing problems with browser caching. Try purging your cache, e.g. check the disable cache in Chrome dev tools network tab or in Firefox's dev tools settings. That should fix it.

Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
  • thanks for your help! Tried that and no help. I'm getting an `Failed to load resource: net::ERR_NAME_NOT_RESOLVED` error still (maybe this browser error helps?). – YPCrumble Apr 04 '15 at 21:25
  • This indicates a DNS problem, double check that you have no typo anywhere in the domain names within your nginx configuration. – Fleshgrinder Apr 04 '15 at 21:51
  • Thanks - does this mean I need to point www.example.com to my IP address like I have example.com? (Maybe this is a beginner mistake?) – YPCrumble Apr 04 '15 at 21:54
  • 1
    No, you just have to make sure that your DNS resolves www.example.com and example.com to the IP of your server. I guess you are not running the DNS yourself, so check with your domain provider that everything is alright on his end. You may use `nslookup example.com` or `dig example.com` to directly query the DNS system on your command line. – Fleshgrinder Apr 04 '15 at 22:13