Q:

how to redirect http://www to https://www - nginx

I'm going to redirect http://www.example.com to https://www.example.com. in nginx server-block I have below snippet:
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.$host$request_uri;

}
In case example.com it works properly and redirects http://example.com to https://www.example.com, but in http://www it doesn't redirect properly and redirect the browser to https://www.www.example.com. How can   I solve this problem?

A:

Use $server_name instead of $host, example:
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

