Nginx rewrite http to https and proxy to another port, ERR_TOO_MANY_REDIRECTS error

Lansana Camara

Trying to use Nginx as a reverse proxy here. This is what I want to achieve:

  • Redirect example.com and www.example.com to https://example.com.
  • Proxy the request to another port.

This is the flow: example.com -> Nginx -> Go web server listening on port 5000

It seems that the rewriting is working properly, cause in the browser I get https://example.com, however I am getting this error in the browser:

ERR_TOO_MANY_REDIRECTS

If it matters, my DNS settings are as such:

@   - A - 11.XX.XX.XX
www - A - 11.XX.XX.XX

Here is my /etc/nginx/nginx.conf file:

events {
    worker_connections 1024;
}

http {
    server {
        listen      80;
        listen      [::]:80;
        server_name www.example.com example.com;

        rewrite ^(.*)$ https://example.com$request_uri permanent;

        location / {
                proxy_pass http://127.0.0.1:5000;
        }
    }

    server {
        listen      443 ssl;
        listen      [::]:443 ssl;
        server_name www.example.com example.com;

        ssl_certificate "/etc/letsencrypt/live/example.com/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/example.com/privkey.pem";

        rewrite ^(.*)$ https://example.com$request_uri permanent;

        location / {
            proxy_pass http://127.0.0.1:5000;
        }
    }
}

Any help would be appreciated. Networking noob here.

Lansana Camara

The other two answers were very helpful in fixing the answer to this question (Redirect loop). There was another bug however, which is that the www was showing up every time even though I redirected to non-www https version.

Here is the updated config that does the following:

  • Turn www to non-www
  • Turn http to https

    events {
        worker_connections 1024;
    }
    
    http {
        server {
            listen      80;
            listen      [::]:80;
            server_name www.example.com example.com;
    
            return 301 https://example.com$request_uri;
        }
    
        server {
            listen      443 ssl;
            listen      [::]:443 ssl;
            server_name www.example.com;
    
            ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
            ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
    
            return 301 https://example.com$request_uri;
        }
    
        server {
            listen      443 ssl;
            listen      [::]:443 ssl;
            server_name example.com;
    
            ssl_certificate "/etc/letsencrypt/live/example.com-0001/fullchain.pem";
            ssl_certificate_key "/etc/letsencrypt/live/example.com-0001/privkey.pem";
    
            location / {
                proxy_pass http://127.0.0.1:5000;
            }
        }
    }
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

EC2 Ubuntu NGINX config HTTPS Redirect giving me error: ERR_TOO_MANY_REDIRECTS

Http to Https Not all conditions work OR ERR_TOO_MANY_REDIRECTS

nginx reverse proxy redirects to http instead of https

Nginx redirect https->http(reverse proxy)->port(docker container)

Nginx config to proxy pass to external ip and port (https -> http)

HTTP to HTTPS Nginx too many redirects

NGINX rewrite location to another port

ERR_TOO_MANY_REDIRECTS Error to acess panel WordPress when i change to SSL (https)

HTTP Error 310 ERR_TOO_MANY_REDIRECTS with RequireHttpsAttribute ASP.NET Core

NGINX: Rewrite url and reverse proxy to a different port

ERR_TOO_MANY_REDIRECTS with nginx

Nginx and Cloudflare: err_too_many_redirects

Nginx reserve-proxy https and http connect error

Dealing with nginx 400 "The plain HTTP request was sent to HTTPS port" error

The plain HTTP request was sent to HTTPS port error Docker nginx

Cant setup iredmail behind existing jwilder-nginx-proxy ERR_TOO_MANY_REDIRECTS 301 Moved Permanently

How to merge htaccess redirect by language function with www to non www and http to https? browsers return: err_too_many_redirects

.htaccess error - ERR_TOO_MANY_REDIRECTS

Nginx too many redirects when redirecting http to https

Nginx rewrite HTTP to HTTPS says redirect loop?

IIS got ERR_TOO_MANY_REDIRECTS on rewrite rule

Nginx https reverse proxy another 502

Apache httpd reverse proxy returns SSL_ERROR_RX_RECORD_TOO_LONG when HTTP redirects to HTTPS

Laravel + Nginx + Cloudflare = ERR_TOO_MANY_REDIRECTS

ERR_TOO_MANY_REDIRECTS for Minio via NGINX Ingress Controller

nginx adding new site causes ERR_TOO_MANY_REDIRECTS

What is this ERR_TOO_MANY_REDIRECTS loading error?

Express.js ERR_TOO_MANY_REDIRECTS error

Symfony infinite loop leading to ERR_TOO_MANY_REDIRECTS error