【问题标题】:Spring Boot and Nginx integrationSpring Boot 和 Nginx 集成
【发布时间】:2016-08-31 15:57:25
【问题描述】:

在我的项目中,Web 应用程序是使用带有默认 tomcat 服务器的 Spring boot 开发的。 我使用 NGINX 作为负载平衡器,并在 NGINX 配置中配置了我的 spring-boot-web-app,如下所示:

location /spring-boot-web-app {
     proxy_pass http://spring-boot-web-app/
}

http {
    upstream /spring-boot-web-app {
        server <IP_of_spring_boot_app>:<Port_of_spring_boot_app>
    }
}

现在让我们将 NGINX IP 和端口分别称为 nginx_ipnginx_port。 我的网络应用程序的工作 URL 也为:http://web_app_ip:web_app_port/rest/echo/hi

上面的 URL 工作正常。但是当我尝试通过 NGINX 访问相同的 URI 时,它会抛出 404。通过 NGINX 使用的 URL 为: http://nginx_ip:nginx_port/spring-boot-web-app/rest/echo/hi

我有什么遗漏吗?

【问题讨论】:

标签: nginx docker spring-boot reverse-proxy proxypass


【解决方案1】:

这对我有用。你可以试试这个吗?

  1. 运行tomcat

    docker run -d -p 8080:8080 --name=tomcat tomcat:8 
    
  2. 运行 nginx

    docker run -d -p 80:80 --link tomcat:tomcat --name=nginx nginx
    
  3. 进入 nginx 容器并更新配置文件

    docker exec -it nginx bash
    

    /etc/nginx/nginx.conf:

    server {
       listen 80 default_server;
      server_name subdomain.domain.com;
      location / {
          proxy_pass http://tomcat:8080;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
      }
    }
    
  4. 重启nginx服务

    nginx -s reload
    
  5. 从主机浏览器通过 nginx 访问 tomcat。您可能需要向 /etc/hosts 添加条目

    http://subdomain.domain.com
    

完成 nginx 配置:nginx.conf

【讨论】:

  • 非常感谢您的帮助“Gangaraju”。但是你提出的 docker 命令我不能使用。因为我正在使用 consul 来做这件事并定义依赖关系。
  • 它可能不适合您的环境。但是尝试通过与这些步骤进行比较来调试您的问题并检查您缺少的内容。我建议你在其他环境中运行这些容器来交叉验证 nginx 配置/以便更好地理解
  • 非常感谢 Gangaraju 的及时帮助。非常感谢!1
  • 接受了答案,因为它提供了一些调试途径。一旦我得到它会发布解决方案。
猜你喜欢
  • 1970-01-01
  • 2015-05-29
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多