I'm trying to redirect http to https. I use letsencrypt for ssl certificates. My config looks like this
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
server_name example.com www.example.com;
root /var/www/landing;
location /.well-known/ {
root /var/www/;
}
}
When I'm trying to access example.com, I get a browser error saying that there were too many redirects. The error occurs for both http://example.com and https://example.com, the server block is accessed when I go to http://www.example.com because I get redirected to https://example.com and then I get the error above.
How can I fix this?