一、Windows下Nginx使用
1、下载windows版nginx解压
2、在cmd中进入到该文加目录下,运行nginx.exe
运行之后关闭cmd并不会将nginx一并关闭掉,进入控制台查看进程,nginx是存在两个运行的进程(nginx多路复用的特点):
如果需要关闭nginx,重新打开cmd进入到nginx文件目下执行:nginx.exe -s stop即可。
二、配置Nginx实现其请求转发的功能
1、修改默认端口防止冲突(不改也可以)
server {
listen 81;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
2、配置nginx转发规则,加载http花括号中
server {
listen 9001;
server_name localhost;
location ~ /eduservice/ {
proxy_pass http://localhost:8001;
}
location ~ /eduoss/ {
proxy_pass http://localhost:8002;
}
}
3、保存nginx配置文件之后重启nginx: nginx -s reload
4、测试
(1)启动项目的两个Services,分别占用8001和8002端口
(2)通过9001端口访问项目接口
A、查询service-edu所有讲师内容:http://localhost:9001/eduservice/teacher/findAll
查询结果,可以发现我们访问9001接口,访问路径中带有eduservice,Nginx为我们转发到8001端口实现数据的请求: