Home > database >  Nginx constantly redirecting
Nginx constantly redirecting

Time:04-20

I'm deploying an app first time like this, running a FE container separate from the BE. Currently, this setup just ends up redirecting endlessly. Any idea why?

server nginx for that domain

server {
      listen 80;
      server_name domain;
      return 301 https://domain$request_uri;

      client_max_body_size 0;
}

 server {
      server_name domain;

      error_log /var/log/nginx/dom_error.log;
      access_log /var/log/nginx/dom_access.log;

      location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $host;
          proxy_pass http://127.0.0.1:1904;
          add_header  X-Host $host;
      }
      
        listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  }

nginx for that app

server {
    listen 80;
    server_name domain;

    location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://fe:3000;

        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location /api {
        proxy_pass http://be:8000/api;
    }

    location /mongo {
        proxy_pass http://mongodb:8081;
    }
}

Any clue what is wrong here?

CodePudding user response:

Turns out this issue was that nginx conf was not properly reset and changes weren't propagated. This config works properly

CodePudding user response:

A bit nonobvious configuration for me, your ide might need more specific explanation, but as far as there is only one redirect return 301 https://domain$request_uri; I assume that you could try different way:

if ($scheme = "http") {
    return 301 https://$server_name$request_uri;
}

Also I don't get client_max_body_size 0; config entry, do you understand why is it there?

  • Related