一.首先下载nginx
百度云盘:https://pan.baidu.com/s/1Y7JsGzsCr6SfhZLh0v4LzQ
提取密码:zpp6
二.修改nginx>>conf下的文件,nginx.conf
加上一段,例如:
location /waimai {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_pass http://localhost:8081;
}
这是监听80端口,当遇见/waimai时候,实际上会转向8081端口访问.其实,如果不打开nginx(这个默认占用80端口),http://localhost:8081是可以直接运行的,而打开nginx后,除了上述链接可以外,http://localhost:80/waimai也可以使用.
下面些一个简单实例,我这里使用的是springboot搭建的框架,写一个controller
@RestController
@RequestMapping(value = “user”,method=RequestMethod.POST)
public class UserController {
@RequestMapping(value = “login”)
public String login() {
return "登陆成功";
}
}
使用postman工具运行
运行nginx.exe,开启反向代理