nginx 配置前后端分离web项目
·
环境
1、nginx windows
2、idea spring boot 2.2.4 做 Ajax请求
一、nginx 配置文件
配置信息:监听端口、静态文件位置
server {
# 监听端口号
listen 9001;
server_name localhost;
location / {
# proxy_pass http://localhost:80;
# root html;
# index index.html index.htm;
root F:/nginxTest/;
}
二、本地文件

三、测试
1、nginx 测试

2、访问静态资源

静态资源如下,稍后测试ajax请求!
<div>
测试nginx !!
</div>
<script src="jquery.js"></script>
<script type="text/javascript">
function a() {
$.ajax({
url: '/testNginx',
success: function () {
console.log("test sueecess ! ");
}
})
}
a();
</script>
3、结合后端测试 ajax

一、nginx 配置
添加了反向代理,转发到 80 端口。
server {
# 监听端口号
listen 9001;
server_name localhost;
location / {
proxy_pass http://localhost:80;
# root html;
# index index.html index.htm;
root F:/nginxTest/;
}
二、controller 代码
@RestController
public class testNginx {
@RequestMapping("/testNginx")
public String test() {
return "test is success!";
}
}
三、日志
2020-07-04 14:07:14.963 DEBUG 15544 --- [p-nio-80-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing ["test is success!"]
2020-07-04 14:07:14.971 DEBUG 15544 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
四、测试访问其他文件

本机磁盘文件。

五、另外一种配置
1、nginx配置
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|html){
root F:/nginxTest/;
}
2、测试


这样一套的配置依旧可以访问到静态资源, 注意此时没有配置路由转发, ajax请求失败!!!
更多推荐



所有评论(0)