IT评测·应用市场-qidao123.com

标题: 5. Nginx 负载均衡设置案例(附有详细截图说明++) [打印本页]

作者: 刘俊凯    时间: 2025-3-1 07:46
标题: 5. Nginx 负载均衡设置案例(附有详细截图说明++)
5. Nginx 负载均衡设置案例(附有详细截图说明++)

@
目录

1. Nginx 负载均衡 设置实例

需求说明/图解
windows 欣赏器输入: http://www.rainbowseacrm.com/search/look.html

负载均衡设置-思路分析/图解

负载均衡设置规则:
  1. upstream rsservers{
  2. server 192.168.12.134:8080;
  3. server 192.168.12.134:8081;
  4. fair;
  5. }
复制代码
  1. # 2. Nginx 负载均衡实现具体步骤如下
复制代码
  1. # Copyright (c) 1993-2009 Microsoft Corp.
  2. #
  3. # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
  4. #
  5. # This file contains the mappings of IP addresses to host names. Each
  6. # entry should be kept on an individual line. The IP address should
  7. # be placed in the first column followed by the corresponding host name.
  8. # The IP address and the host name should be separated by at least one
  9. # space.
  10. #
  11. # Additionally, comments (such as these) may be inserted on individual
  12. # lines or following the machine name denoted by a '#' symbol.
  13. #
  14. # For example:
  15. #
  16. #      102.54.94.97     rhino.acme.com          # source server
  17. #       38.25.63.10     x.acme.com              # x client host
  18. # localhost name resolution is handled within DNS itself.
  19. #        127.0.0.1       localhost
  20. #        ::1             localhost
  21. 127.0.0.1 account.wondershare.com
  22. # 配置 eureka 主机 和 ip 的映射
  23. 127.0.0.1 eureka9001.com
  24. 127.0.0.1 eureka9002.com
  25. 192.168.76.157 www.rainbowsea.com
  26. 192.168.76.159 www.rainbowseamall.com
  27. 192.168.76.160 www.rainbowseacrm.com
复制代码
这里我的 nginx.conf  路径是在 /usr/local/nginx/conf 路径下。


  1. upstream rsservers {
  2. 36         server 192.168.76.160:8080;
  3. 37         server 192.168.76.160:8081;
  4. 38
  5. 39     }
  6. 40
  7. 41     server {
  8. 42         listen       80;
  9. 43         server_name 192.168.76.160;
  10. 44        #server_name  localhost;
  11. 45         #charset koi8-r;
  12. 46
  13. 47         #access_log  logs/host.access.log  main;
  14. 48
  15. 49         location / {
  16. 50             root   html;
  17. 51             proxy_pass http://rsservers;
  18. 52             index  index.html index.htm;
  19. 53         }
  20. 54
复制代码
查看 nginx.conf 是否存在设置错误:
  1. [root@localhost nginx]# ./sbin/nginx -t
  2. [root@localhost nginx]# /usr/local/nginx/sbin/nginx -t
复制代码

重启 Nginx ,加载我们修改后的设置:
  1. [root@localhost nginx]# ./sbin/nginx -s reload
复制代码
  1. [root@localhost nginx]# ps -aux | grep nginx
复制代码




这里我先进入到  Tomcat 的 webapps 目录下,创建  look.html 文件,进行一个访问映射。
  1. <h1>tomcat 8080 search.... </h1>
复制代码
  1. [root@localhost opt]# cp -r ./tomcat/ ./tomcat8081
复制代码





修改 tomcat 的  conf\server.xml ,  留意要修改如下位置,否则该 Tomcat 是不能正常工作。



细节说明:差别版本的 tomcat 修改的端口还不一样,  小伙伴们灵活处理即可。

  1. <h1>tomcat 8081 search.... </h1>
复制代码
  1. [root@localhost product]# firewall-cmd --add-port=80/tcp --permanent # 防火墙开放 10000 端口
复制代码
  1. [root@localhost product]# firewall-cmd --reload # 重启防火墙
复制代码
  1. [root@localhost product]# firewall-cmd --list-all # 查看防火墙信息
复制代码

  1. [root@localhost bin]# ./startup.sh
复制代码
  1. [root@localhost bin]# netstat -an | more
复制代码


打开 Linux 当中的欣赏器: 访问


查看启动的端口,  确保有 8080  和  8081  端口在监听(提示:  如果 tomcat 没有监听 对应端口,  说明启动失败了,  可以尝试先实行  shutdown.sh  再实行  startup.sh  解决)




  1. [root@localhost nginx]# ./sbin/nginx
复制代码
  1. [root@localhost nginx]# ps -aux | grep nginx
复制代码

http://www.rainbowseacrm.com/search/look.html

3. 留意事项和避免的坑


带下划线,报错信息,如下:

4. 文档: Nginx 的 upstream 设置技巧

基本介绍:
Nginx 是一个反向署理软件,大部分的网站都接纳 Nginx 作为网站/平台的 服务器软件。Nginx 除了可以直接作为 web 服务器使用外,更多的情况是 通过反向署理将哀求转发给上游服务器。
设置上游服务器可以使用 upstream 进行设置,通过 upstream 可以实现服 务的负载均衡规则,可以提高服务器的高可用性。
地址:https://zhuanlan.zhihu.com/p/409693332
5. 末了:

“在这个末了的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4