我有三个 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;
}
}
}
我尝试访问的网址是 http://localhost/second/这给了我错误消息有一个意外的错误(类型=未经授权,状态=401)。
访问此资源需要完全身份验证
当尝试访问http://localhost:8080/swagger-ui.html时给我 404 错误。
当尝试访问http://localhost:8080/swagger-ui.html时显示了 Swagger 的预期页面。
最佳答案
如果您仍在寻找答案,这就是我设法让它发挥作用的方法:
- 除了让每个 spring-boot-app 在不同的端口上运行之外,还要确保每个 spring-boot-app 都有唯一的上下文路径。 See some examples !如何 如果您不熟悉的话,可以实现这一点。
只需将 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 配置,但如果一切配置正确,上述步骤可能就是您正在寻找的内容。
关于java - nginx - Spring Boot 应用程序的多个反向代理(启用 Spring Security),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274716/