skip to Main Content

I have a website which is properly handling all traffic and redirecting everything to https, except for www traffic. I followed this related question, but it didn’t seem to fix it. This is my first time setting this up – any advice is appreciated. My goal is to redirect www.my_website.com to https://my_website.com, but currently it is redirecting to http://www.my_website.com

My DNS is set up as follows:

Type   | Name | Data         | Seconds
--------------------------------------
A      | @    | my_public_ip | Automatic
A      | www  | my_public_ip | Automatic

And my nginx is as follows:

server {
    listen       80;
    server_name  _;
    return 301 https://my_website.com;
}


# HTTPS server
server {
    listen       443 ssl;
    server_name  my_website.com;

    ssl_certificate      some_random.crt;
    ssl_certificate_key  some_random.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://localhost:8080;
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I actually figured out the issue, it was a small goof I made - I needed to change server_name my_website.com; to server_name my_website.com, www.my_website.com;


  2. This is a schematic which applies for cases like this in general. It catches http and redirect to https, and redirects www to non-www. SSL-Certificates can be given separately (or the same for both), depending on Certificate.

    server {
        # Handle http
        server_name www.my_website.com my_website.com;
        return 301 https://my_website.com$request_uri;
    }
    
    server {
        # Handle www
        listen       443 ssl;
        server_name  www.my_website.com;
        ssl_certificate      some_random.crt;
        ssl_certificate_key  some_random.key;
        return 301 https://my_website.com$request_uri;
    }
    
    server {
        listen       443 ssl;
        server_name  my_website.com;
        ssl_certificate      some_random.crt;
        ssl_certificate_key  some_random.key;
        # your 'real' server
    }
    

    And it fixes this special problem here, because there is no rule for SSL www requests given. It would be possible to give server_name www.my_website.com my_website.com; for SSL as well, if cert etc is valid for both.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search