server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location /index.html {
# root html;
#index index.html index.htm;
#}
location /api/ {
proxy_pass http://192.168.10.242:8080/;
}
}
将8080下的端口的api进行代理,依次增加以api开始
如果是java SpringBoot项目,用了swagger,则需要清空swagger,那个参数就是让它不自动获取本机host,而是根据请求参数获取
springfox:
documentation:
swagger:
v2:
host:
2. nginx前后端分离ajax请求
server {
listen 80; //虚拟发布的最终端口
server_name localhost;
#charset koi8-r;
![](/qrcode.jpg)
#access_log logs/host.access.log main;
location /test.html {
root html;
}
location /api/ {
proxy_pass http://192.168.10.242:8080/; //监听的后端api
}
页面ajax调用方式:页面位置html/test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("/api/anno/doTest2");
});
});
</script>
</head>
<body>
<div id="div1"><h2>使用 jQuery AJAX 修改文本内容</h2></div>
<button>获取外部内容</button>
</body>
</html>