1、下载安装nginx
2、在nginx安装目录如("D:\nginx-1.18.0")
3、在打开bin目录下找到配置文件nginx.conf中配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
#root html;
#index index.html index.html;
#}
location / {
proxy_pass http://192.172.3.7:8081;
}
1.规则一(转发路径proxy_pass
+ path
)
#1.Nginx配置如下
- 举例一:
1 location / { 2 proxy_pass http://127.0.0.1:8080; #此时适应减法规则 proxy_pass + path 3 } 4 5 #2.用户访问http://127.0.0.1/payment 6 7 path = http://127.0.0.1/payment 减去协议 ip 端口 = /payment 8 proxy_pass = http://127.0.0.1:8080 9 10 #3. 最终访问路径:http://127.0.0.1:8080/payment
- 举例二:
#1.Nginx配置如下 location /hello { proxy_pass http://127.0.0.1:7071; #此时适应减法规则 proxy_pass + path } #2.用户访问http://127.0.0.1/hello/sentinel path = http://127.0.0.1/sentinel 减去协议 ip 端口 = /hello/payment proxy_pass = http://127.0.0.1:7071 #3. 最终访问路径:http://127.0.0.1:7071/hello/sentinel
2.规则二(转发路径proxy_pass
+ (path-location)
)
- 判断规则:当location中定义的转发proxy_pass路径减去 协议 ip 端口有其它内容(包括斜杠),适用当前规则
#1.Nginx配置如下 location / { proxy_pass http://127.0.0.1:8080/; #此时适应减法规则 proxy_pass + (path-location) } #2.用户访问http://127.0.0.1/payment path = http://127.0.0.1/payment 减去协议 ip 端口 = /payment location = / path-location = payment proxy_pass = http://127.0.0.1:8080/ #3. 最终访问路径:http://127.0.0.1:8080/payment
- 举例二:
#1.Nginx配置如下 location /hello { proxy_pass http://127.0.0.1:7071/; #此时适应减法规则 proxy_pass + (path-location) } #2.用户访问http://127.0.0.1/hello/sentinel path = http://127.0.0.1/sentinel 减去协议 ip 端口 = /hello/payment location = /hello path-location = /sentinel proxy_pass = http://127.0.0.1:7071/ #3. 最终访问路径:http://127.0.0.1:7071/sentinel
Nginx中的负载均衡:
#1.编辑nginx.conf文件 #2.在http节点里添加:定义负载均衡设备的 ip及设备状态 upstream myServer{//要代理的服务器 server 192.168.254.131:8080; server 192.168.254.131:8088; } server{ listen 80; //nginx服务器访问端口 server_name localhost; //nginx服务器地址 #3.转发路径处填写前面定义的设备组 location / { proxy_pass http://myServer/;//nginx代理的服务器地址 } }