Resolve too many redirects using NGINX reverse proxy and Cloudflare

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://; }                    if ($limit_bots = 1) {                    return  301 $scheme://;            }            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 /;            proxy_redirect ws:// /api/socket;            proxy_pass;            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.

See also  How to view and read running processes on Linux

Originally posted 2019-02-08 23:59:27.

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply