Home > OS >  nginx not redirecting www to https
nginx not redirecting www to https

Time:01-22

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

CodePudding user response:

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
}

CodePudding user response:

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;

  •  Tags:  
  • Related