提问者:小点点

nginx-用于Spring Boot应用程序的多个反向代理(启用spring security)


我有三个Spring Boot应用程序在端口8080,8081,8082上的嵌入式tomcat上运行。

我试图为他们配置反向代理,当点击url时,我得到了来自Spring security的401错误。

我将nginx配置如下:

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;
        }

    }

}

我试图访问的url是http://localhost/second/这给我错误说有一个意外的错误(type=未授权,status=401)。访问此资源需要完全身份验证

当尝试访问时http://localhost:8080/swagger-用户界面。html给我一个404错误。

当试图访问http://localhost:8080/swagger-ui.html显示我斯瓦格的预期页面。


共2个答案

匿名用户

如果你还在寻找答案,这就是我如何设法让它工作:

> < li >除了让每个spring-boot-app在不同的端口上运行,还要确保每个spring-boot-app都有唯一的上下文路径。看一些例子!如果你不熟悉的话。 < li>

只需将spring-boot-app的上下文路径与nginx的匹配即可,如下所示:

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

我没有测试你的nginx配置,但是如果一切都配置正确,上面的步骤可能是你正在寻找的。

匿名用户

问题是nginx正在浏览器上缓存页面,因此对nginx进行了任何修改。conf文件未在浏览器上反映。这是我的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;
        }
    }
}

对于不知道的用户,请重新启动Nginx服务器,并在每次更改Nginx.conf时按ctrl F5刷新浏览器