Search code examples
nginxgoogle-cloud-platformcloudflare

Nginx cause too many redirects


I have created a GCP cloud instance to host RockerChat. I am using Nginx and Cloudflare to access it with my domain. But it shows a too many redirects error.

ERR_TOO_MANY_REDIRECTS

I am a newbie to this, any help is much appreciated!

Access with ip_address:port is ok but with domain leads to a too many redirects error. Below is my Nginx config:

events {

worker_connections 1024;

}

http {

client_max_body_size 1000m;

server {

listen 80;

server_name chat.miahomebuyerservice.com.;

return 301 https://$host$request_uri;

}

server {

listen 443 ssl;

server_name chat.miahomebuyerservice.com;

http2 on;

ssl_certificate /etc/nginx/certs/chat-cert.pem;

ssl_certificate_key /etc/nginx/certs/chat-key.pem;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

location / {

  proxy_set_header   X-Real-IP $remote_addr;

  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

  proxy_set_header   Host $host;

  proxy_pass         http://34.125.124.2:8890/;

  proxy_http_version 1.1;

  proxy_set_header   Upgrade $http_upgrade;

  proxy_set_header   Connection "upgrade";

}

Any help is much appreciated!


Solution

  • This issue could be causing by multiple reason; I doubt to pinpoint solution but here’s checklist:

    • Generally broswer accept 3-4 hops of redirect. More than that it prompt too many redirect error on client end.
    • Try to switch to Development Mode in cloudflare temporarily to narrow down the root cause if redirect loop cause by cloudflare.
    • Try to disable Automatic HTTPS Rewrites and HTTP Strict Transport Security.
    • Optionally If you using loadbalancer between your backend and gcp instance which is re-encrypting the http traffic. Try to disable it and test url directly.
    • Either redirect https from cloudflare or you let ngnix rewrite rule but putting both together will cause this error. If your application has it’s SSL functionality then that might be case that it could be translating http request to https and causing the loop back. If your backend application is already hardcode with https, keep it simple to not use redirect 301.
    • Enable the developer mode on your browser and check your url source if any embedded link within your source code is creating loop-back. I am sure your backend is not causing it, but just to doublecheck.
    • Try to test your cloudflare encryption to flexible, Full from full strict.
    • Since you are new on cloudflare, Please note that Full strict SSL, re-encrypt the requests from origin server (in your case ngnix server) and it may require you to configure CA certificate. https://developers.cloudflare.com/ssl/origin-configuration/origin-ca/
    • Perform the checklist as per this link https://developers.cloudflare.com/ssl/troubleshooting/too-many-redirects/
    • I tested your url with https://www.redirect-checker.org/. It’s definitely caused via misconfigured cloudflare parameter on https and http-https loop.

    I hope above points may able to solve your issue.