Search code examples
javaspring-bootnginxreverse-proxynginx-reverse-proxy

nginx - multiple reverse proxy for spring boot applications (enabled spring security)


I have three Spring boot applications running on embedded tomcat on ports 8080, 8081, 8082.

I am trying to configure reverse proxy for all of them, When hitting the url I am getting 401 error which is from Spring security.

I have configured the nginx as follows:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {

        listen       80;
        server_name  localhost;

        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #also tried $remote_addr;

        location /first {
            proxy_pass http://localhost:8081;
        }
        location /second {
            proxy_pass http://localhost:8080;
        }
        location = /third {
            proxy_pass http://localhost:8082;
        }

    }

}

the url I am trying to access is http://localhost/second/ this gives me error saying There was an unexpected error (type=Unauthorized, status=401). Full authentication is required to access this resource

when tried to access http://localhost:8080/swagger-ui.html gives me 404 error.

when tried to access http://localhost:8080/swagger-ui.html shows me expected page of Swagger.


Solution

  • The issue was nginx was caching the page on the browser, hence any modification in nginx.conf file was not reflecting on browser. Here is the final working version of my nginx.conf

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
    
        sendfile        on;
        keepalive_timeout  65;
        gzip  on;
    
        server {
            listen       80;
            listen       [::]:80;
            server_name  localhost;
    
            access_log  logs/host.access.log  main;
            
            location / {
               root  "D:/projects/mySimpleProject/build";
               index index.html index.htm;
            }
    
            location /first/ {
                proxy_pass http://localhost:8080/;
            }
            location /second/ {
                proxy_pass http://localhost:8081/;
            }
            location /third/ {
                proxy_pass http://localhost:8082/;
            }
            
            error_page  404              /404.html;
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    For those who don't know, Restart the Nginx server and hit ctrl+F5 to refresh your browser every time you make a change in nginx.conf