Cloudflare and Nginx

Hello, when i use this Nginx config for my nginx server, i get this error on my browser when i visit my website (polido.pt): ERR_TOO_MANY_REDIRECTS
server {
listen 80;
server_name polido.pt www.polido.pt;

location / {
rewrite ^ https://$host$request_uri permanent;
}
}

server {
listen 443;
server_name polido.pt www.polido.pt;

location / {
alias /; # The root directory
index pingu.mp4;
}
}
server {
listen 80;
server_name polido.pt www.polido.pt;

location / {
rewrite ^ https://$host$request_uri permanent;
}
}

server {
listen 443;
server_name polido.pt www.polido.pt;

location / {
alias /; # The root directory
index pingu.mp4;
}
}
But with this config file for my Nginx server (default one) everything works ok and the website loads:
server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
5 Replies
polido
polidoOP17mo ago
I just want to know what is causing this issue. Is it because Cloudflare already provides an SSL certificate or?
Hello, I’m Allie!
Yes. Because Cloudflare provides an SSL certificate, an https: connection from the browsers perspective may actually be sent to nginx as a http: connection. You should be able to fix this by setting your Domain's SSL/TLS encryption mode to Full, or Full(Strict), if your nginx service has a valid CA cert.
polido
polidoOP17mo ago
Yes, i have a valid certbot Certificate for my domain, but do you think that i should use it since Cloudflare already provides one for free?
Hello, I’m Allie!
You can if you want to(it should allow you to use Full(Strict)). Otherwise, you can use Cloudflare Tunnels to provide a secure connection to your server, and then completely disable HTTPS on nginx(and close both port 80 and 443 in your firewall).
polido
polidoOP17mo ago
Nice, i want to do that!

Did you find this page helpful?