简体   繁体   中英

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 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.

If you are still looking for an answer, this is how I managed to have it working:

  1. In addition to having each spring-boot-app running on a different port, make sure each of the spring-boot-app has a unique context path. See some examples ! on how to achieve that if you are not familiar with.
  2. Just match the context path of the spring-boot-app with that of nginx, something like this:

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

    I didn't test your nginx config, but if everything is configured properly, the steps above could be what you are looking for.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM