Nginx redirect everything to www https Nginx redirect everything to www https nginx nginx

Nginx redirect everything to www https


server {    listen 80;    server_name example.com www.example.com    return 301 https://www.example.com$request_uri;}server {    listen 443 ssl;    server_name example.com     ssl_certificate Certificate/For/example.com/fullchain    ssl_certificate_key Key/For/example.com/     return 301 https://www.example.com$request_uri;}server {    listen 443;    server_name  www.example.com    ssl_certificate Certificate/For/www.example.com/fullchain    ssl_certificate_key Key/For/www.example.com/key    //do whatever you want with your https://www.example.com connection}   

Remember to have both certificates, one for www and one without, as if you redirect example to www without certificate, that's a non-secure step, and an error will show.

Hope I helped


I think you should specify server name for https server too:

server {    server_name www.example.com    listen 443 ssl;    ...    ssl_certificate /root/certs/fullchain.pem;    ssl_certificate_key /root/certs/privkey.pem;}

Also you can simplify http servers with combining them into one:

server {    listen 80;    server_name example.com www.example.com    return 301 https://www.$host$request_uri;}