Spring Boot和Nginx集成 [英] Spring Boot and Nginx integration
问题描述
在我的项目中,Web应用程序是使用默认的tomcat服务器的Spring引导开发的。
我正在使用NGINX作为负载平衡器,并按照以下原则在NGINX配置中配置了我的spring-boot-web-app:
In my project, web app is developed using Spring boot with default tomcat server. I am using NGINX as load-balancer and have configured my spring-boot-web-app in NGINX configuration as follows:
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_ip 和 nginx_port 。
我的网络应用程序的工作网址为: http:// web_app_ip:web_app_port / rest / echo / hi
Now lets say NGINX IP and port as nginx_ip and nginx_port respectively. Also working URL for my web app as: http://web_app_ip:web_app_port/rest/echo/hi
上述网址正常。但是当我尝试通过NGINX打到相同的URI时,它会将404.通过NGINX使用的URL作为:
http:// nginx_ip:nginx_port / spring-boot-web-app / rest / echo / hi
The above URL works fine. But when i try to hit same URI via NGINX it throws 404. URL used via NGINX as: http://nginx_ip:nginx_port/spring-boot-web-app/rest/echo/hi
我失踪了?
推荐答案
这对我有用。你可以试试吗?
This works for me. Can you try this?
-
运行tomcat
Running tomcat
docker run -d -p 8080:8080 --name=tomcat tomcat:8
运行nginx
Running nginx
docker run -d -p 80:80 --link tomcat:tomcat --name=nginx nginx
进入nginx容器并更新conf
Go inside nginx container and update the conf
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;
}
}
重新启动nginx服务
Restart nginx service
nginx -s reload
从主机浏览器通过nginx访问tomcat。您可能需要在/ etc / hosts中添加条目
Access the tomcat through nginx from host browser. You may need to add entry to /etc/hosts
http://subdomain.domain.com
完成nginx conf: nginx.conf
Complete nginx conf: nginx.conf
这篇关于Spring Boot和Nginx集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!