Resolve too many redirects using NGINX reverse proxy and Cloudflare

Posted on

I have dockerized web app projects in my single server, so I have to use a reverse proxy to access each of the apps. I also manage to use Cloudflare as my DNS service. I also have enabled SSL for secure connection using NGINX. That’s the problem begins. If i enabled Cloudflare protection (with orange cloud enabled), I get too many redirects loop when i access my site.

Cloudflare has been explain why it happens. If the origin server happens to be configured to redirect HTTP requests to HTTPS, server responses back to Cloudflare are encrypted and since Cloudflare expecting HTTP traffic, it keeps resending the same request, resulting in a redirect loop. This causes browsers to display “The page isn’t redirecting properly” or “ERR_TOO_MANY_REDIRECTS” errors.

Here are some step to resolve too many redirects errors

Set SSL Crypto to Full (strict) in your Cloudflare Crypto options.

Access you Cloudflare dashboard and go to Crypto tab. Enable Full (strict) options.

Allow X-Forwarded-Proto in your header

Set NGINX configuration in you location block to allow X-Forwarded-Proto header. Add this line in it.

proxy_set_header X-Forwarded-Proto $scheme;

This is my full location blog from one of my blogs.

    location / {            #blocks blank user_agents            if ($http_user_agent = "") { return  301 $scheme://www.google.com/; }                    if ($limit_bots = 1) {                    return  301 $scheme://www.google.com/;            }            proxy_set_header Host $host;            proxy_set_header X-Forwarded-Host $host;            proxy_set_header X-Forwarded-Server $host;            proxy_set_header X-Forwarded-Proto $scheme;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_http_version 1.1;            proxy_set_header Upgrade $http_upgrade;            proxy_set_header Connection "upgrade";            proxy_redirect http://0.0.0.0:3001/ /;            proxy_redirect ws://0.0.0.0:3001/api/socket /api/socket;            proxy_pass http://0.0.0.0:3001/;            proxy_connect_timeout 300;            proxy_send_timeout    300;            proxy_read_timeout    300;    }

This will resolved too many redirects issue that i have been experienced. Hope it will help you too.

Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *