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
I actually figured out the issue, it was a small goof I made - I needed to change
server_name my_website.com;
toserver_name my_website.com, www.my_website.com;
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.
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.