I want to redirect all my http
traffic to redirect to https
. I am using letsencrypt
. I read online that return 301 https://$server_name$request_uri;
would redirect all the traffic to my website over to https
but instead it results in ERR_TOO_MANY_REDIRECTS
.
Everything works fine without the above mention statement, but then I have to specifically specify https
in the URL. Here's my /etc/nginx/sites-available/default
file:
server {
listen 80 default_server;
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;
root /home/website/mywebsite/public;
index index.html index.htm index.php;
server_name mywebsite.me www.mywebsite.me;
return 301 https://$server_name$request_uri;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
Where am I going wrong?
What Does "Too Many Redirects" Mean? The ERR_TOO_MANY_REDIRECTS error indicates that the browser is stuck in an infinite redirection loop. An infinite redirection loop happens when you visit a URL pointing to another URL, which points back to the first one.
There are no limits in using 301 redirects on a site. You can implement more than 100k of 301 redirects without getting any penalty. But: Too many 301 redirects put unnecessary load on the server and reduce speed. Try to reduce direct redirects by using rules.
In my case it was Cloudflare. I had to change to Full SSL encryption
Change your config to below
server {
listen 80 default_server;
server_name mywebsite.me www.mywebsite.me;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;
root /home/website/mywebsite/public;
index index.html index.htm index.php;
server_name mywebsite.me www.mywebsite.me;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
}
Your current config redirects on both http and https to https. So it becomes a infinite loop because of the return statement. You want return statement only when connection is http. So you split it into two server blocks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With