1

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;
    }
}
kennemat
  • 180
  • 2
  • 8
  • 1
    What is the output of `nginx -T` – Tarun Lalwani Jan 21 '22 at 07:48
  • 1
    You may find this a useful reference: https://github.com/h5bp/server-configs-nginx/blob/main/conf.d/templates/example.com.conf – AD7six Jan 21 '22 at 21:27
  • 1
    Does this answer your question? [Nginx no-www to www and www to no-www](https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www) – AD7six Jan 21 '22 at 21:29

2 Answers2

1

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.

araisch
  • 1,727
  • 4
  • 15
0

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;

kennemat
  • 180
  • 2
  • 8
  • There is no redirect in the `server_name my_website.com` server block - it's not clear how this is any part of a solution. Please edit the answer to clarify (and accept an answer so the question is considered resolved :)). – AD7six Jan 26 '22 at 11:26