nginxsslnginx-reverse-proxynginx-config

Nginx goes to infinite redirect loop when trying to host 2 applications on 1 domain


I have frontend app that I want to appear on all urls, except /admin. On /admin i need another to show application.

Frontend app works fine, but when i go to domain_name/admin i get infinite redirect loops until browser throws error.

My nginx config:

upstream front {
    server frontend:3000;
}

upstream admin {
    server web:8000;
}

server {
    
    listen 80;

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

server {
    
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name "domain_name";
    ssl_certificate /etc/nginx/certs/domain_name.crt;
    ssl_certificate_key /etc/nginx/certs/domain_name.key;
    
    location / {
        proxy_pass http://front;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    
    location /admin/ {
        proxy_pass http://admin/admin;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /home/app/web/staticfiles/;
    }
    
    location /media/ {
        alias /home/app/web/mediafiles/;
    }

}

I've tried multiple variations with url routing like setting web:8000/admin in upstream and tried dividing server block into 2, but faced another errors. How do i fix this?

Nginx is in docker btw.


Solution

  • The solution was to add slashes at the end of proxy_pass fields e.g. http://front/;. I am not show why it works this way but ok