Nginx做负载均衡的简单配置

目标效果:  通过访问 http://192.168.43.196:9001/sunny/    可以分别访问到 http://127.0.0.1:8081/sunny/http://127.0.0.1:8082/sunny/

效果图:

 

方法一:

具体配置:

http标签中添加:

upstream sunny_pool {
  server 127.0.0.1:8081;
  server 127.0.0.1:8082;
}

server中添加:

location / {
  proxy_pass http://sunnypool;
  root html;
  index index.html index.htm;
}

修改server标签中监听IP和端口:

 listen 9001;              #修改监听端口为9001,默认是80
 server_name 192.168.43.196;   #修改监听IP,默认是localhost

# 启动Nginx ,Tomcat 。浏览器访问 http://192.168.43.196:9001/sunny/   自动重定向到  http://127.0.0.1:8081/sunny/   或者   http://127.0.0.1:8082/sunny/   了。

方法二:

 
添加配置文件 sunny_pool,sunny_proxy
sunny_pool中配置:

upstream sunny_pool {
  server 127.0.0.1:8081;
  server 127.0.0.1:8082;
}

sunny_proxy 中配置:

location / {
  proxy_pass http://sunny_pool/;
}

修改server标签中监听IP和端口:

 listen 9001;              #修改监听端口为9001,默认是80
 server_name 192.168.43.196;   #修改监听IP,默认是localhost

 

# 启动Nginx ,Tomcat 。浏览器访问 http://192.168.43.196:9001/sunny/   自动重定向到  http://127.0.0.1:8081/sunny/   或者   http://127.0.0.1:8082/sunny/   了。

 

另外两篇Nginx心得。

Nginx做动静分离:https://www.cnblogs.com/sunnycc/p/14213980.html

Nginx做反向代理:https://www.cnblogs.com/sunnycc/p/14209425.html

好文要顶 关注我 收藏该文  
posted @ 2020-12-30 21:43  爵士灬  阅读(154)  评论(0编辑  收藏  举报