java - nginx - Spring Boot 应用程序的多个反向代理(启用 Spring Security)

标签 java spring-boot nginx reverse-proxy nginx-reverse-proxy

我有三个 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 的预期页面。

最佳答案

如果您仍在寻找答案,这就是我设法让它发挥作用的方法:

  1. 除了让每个 spring-boot-app 在不同的端口上运行之外,还要确保每个 spring-boot-app 都有唯一的上下文路径。 See some examples !如何 如果您不熟悉的话,可以实现这一点。
  2. 只需将 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/

相关文章:

java - Eclipse 氧气升级 - "Java Virtual Machine Launcher - A Java Exception has occured)"

java - 在 Android Studio 中获取 "Gradle sync failed: connection reset"

web-services - 如何从 Spring Boot Endpoint Service 返回自定义 SOAP 错误?

物理服务器名称的 Nginx 变量

nginx - 在 nginx 图表中的 http-snippet 下定义 map 时出现语法错误

java - 如何调用java中的语句

java - 将基于 JOIN 的聚合查询结果映射到 Hibernate 属性

Java Spring REST API 处理许多可选参数

java - 我如何在 spring boot 中更改 logback 版本?

nginx - 为 Nginx 中的特定位置附加 auth_basic_user_file