nginx rewrite from www to non-www
Solution 1:
The $server_name
refers to the server name you have defined in the virtual host block. Therefore your additional block causes a redirect cycle redirecting back to itself.
You have to use a literal domain name there instead of a variable.
For SSL domain with www
, you have to add listen 443 ssl;
to the block and certificate values.
So, this should be your third block:
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 https://example.com$request_uri;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
...
}