I build a website with Google domain on the Nginx server. I want to redirect all links to secure and non www link. For example:
http://example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
Whenever I access to site, the page says redirected you too many times.
Here is my nginx.conf
###### Redirect to secure site
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
###### Redirect to non www site
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 https://example.com$request_uri;
}
###### Main config for secure and non www site
server {
listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
listen 443 http2 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
root /usr/share/nginx/example.com;
}
301 Moved Permanently
redirects are cached by the browser, so he might follow an old redirect. Restart your browser to clear the cache.www.example.com
is aCNAME
orA
record doesn't matter. If it isn't a problem with your browser (which cached a redirection loop from a previous config), maybe you didn't reload nginx after the change. You can debug the problem withcurl -i
.