在nginx.conf文件server{}中添加下列,记得自行修改location后的路径名称
location /server1 {
proxy_pass http://127.0.0.1:8080/;
}
location /server2 {
proxy_pass http://127.0.0.1:8081/;
}
添加完后记得重载nginx.conf文件
sudo nginx -s reload
下面有些坑:
注意
proxy_pass http://127.0.0.1:8081/后面加上/
代理springboot程序,使用了thymeleaf引擎,注意引入js.css,images时不要用@{},否则会404
下面两种是错误例子
<script th:src="@{static/bootstrap/js/jquery-3.4.1.min.js}"></script>
<script src="/static/bootstrap/js/jquery-3.4.1.min.js"></script>=
正确示范
<script src="static/bootstrap/js/jquery-3.4.1.min.js"></script>=
nginx location proxy_pass 后面的url 加与不加/的区别
建议看看这篇文章NGINX配置之二: nginx location proxy_pass 后面的url 加与不加/的区别. - 九号云 - 博客园
大佬的结论很简单 如果访问http://ip:port/locationName
- 如果proxy_pass 后面的url末尾带有
/则访问的是http://ip:port/proxy_pass_url - 如果proxy_pass 后面的url末尾没有
/则访问的是```http://ip:port/locationName/proxy_pass_url``
区别在与实际访问的url要不要和location 的名称相拼接
本文详述如何在nginx.conf中配置代理SpringBoot应用,包括正确设置location及proxy_pass参数,避免404错误。强调了URL结尾斜杠的重要性,并提供了引入静态资源的正确示例。
3474

被折叠的 条评论
为什么被折叠?



