Home > OS >  Linux/Nginx too many redirects
Linux/Nginx too many redirects

Time:09-22

I tried many nginx configurations until I found what worked. I'm unsure if there is some kind of stored record of all my changes on my server because when I open my webapp, I get an error "too many redirects". It has the same amount of redirects as when it worked before, just a different configuration. Can anyone offer any insight?

server {

    listen 80;
    server_name cigars-rec.com;

    location / {
        return 301 https://$host$request_uri;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

}

upstream cr2-cigar-recommender {
    server  cr2-cigar-recommender:8501;
}

server {
    listen 443 ssl;
    server_name cigars-rec.com;

    ssl_certificate /etc/letsencrypt/live/cigars-rec.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cigars-rec.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # streamlit config
    location / {
        proxy_pass http://cigars-rec.com;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }
    
    location /page1 {
        default_type "text/html";
        alias /templates/page1.html;
    }

    location /page2 {
        default_type "text/html";
        alias /templates/page2.html;
    }

    location /page3 {
        default_type "text/html";
        alias /templates/page3.html;
    }

}

CodePudding user response:

In your configuration file, you have this directive in your http server block for port 80:

location / {
        return 301 https://$host/$request_uri;
}

This appears to be an intentional http to https redirect, which is likely exactly what you want.

But then your https server block has a proxy_pass that tells nginx to make an http connection to http://cigars-rec.com/ and return the response back to the client. That http URL will be served by the http server block that issues a redirect.

This would mean that any request to the http or https version of your site that is matched by that location / will always return a redirect and cause the condition that you described.

  • Related