Spring Boot和Nginx集成

Sam*_*Sam 5 reverse-proxy nginx proxypass docker spring-boot

在我的项目中,使用Spring引导和默认的tomcat服务器开发Web应用程序.我使用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>
    }
}
Run Code Online (Sandbox Code Playgroud)

现在让我们分别说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

有什么我想念的吗?

Gan*_*aju 8

这适合我.你能试试吗?

  1. 运行tomcat

    docker run -d -p 8080:8080 --name=tomcat tomcat:8 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行nginx

    docker run -d -p 80:80 --link tomcat:tomcat --name=nginx nginx
    
    Run Code Online (Sandbox Code Playgroud)
  3. 进入nginx容器并更新conf

    docker exec -it nginx bash
    
    Run Code Online (Sandbox Code Playgroud)

    /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;
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 重启nginx服务

    nginx -s reload
    
    Run Code Online (Sandbox Code Playgroud)
  5. 从主机浏览器通过nginx访问tomcat.您可能需要向/ etc/hosts添加条目

    http://subdomain.domain.com
    
    Run Code Online (Sandbox Code Playgroud)

完成nginx conf:nginx.conf

  • 接受了答案,因为它提供了一些调试路径.一旦我得到它就会发布解决方案. (2认同)