• 系列七、Nginx负载均衡配置


    一、目标

            浏览器中访问http://{IP地址}:9002/edu/index.html,浏览器交替打印清华大学8080、清华大学8081。

    二、步骤

    2.1、在tomcat8080、tomcat8081的webapps中分别创建edu文件夹

    2.2、将index.html分别上传至edu文件夹

            注意事项:tomcat8080的edu文件夹中的index.html的内容为"清华大学8080"、"清华大学8081",index.html模板如下:

    1. "en">
    2. <head>
    3. "UTF-8">
    4. 首页
    5. "color: red;font-size: 30px" align="center">清华大学

    2.3、分别启动tomcat8080、tomcat8081

    1. # 启动tomcat8080
    2. /opt/tomcat/tomcat8080/apache-tomcat-8.5.63/bin
    3. ./startup.sh
    4. # 启动tomcat8081
    5. /opt/tomcat/tomcat8081/apache-tomcat-8.5.63/bin
    6. ./startup.sh

    2.4、测试

    1. http://192.168.173.222:8080/edu/index.html
    2. http://192.168.173.222:8081/edu/index.html

    2.5、修改nginx配置

    1. worker_processes 1;
    2. events {
    3. worker_connections 1024;
    4. }
    5. http {
    6. include mime.types;
    7. default_type application/octet-stream;
    8. sendfile on;
    9. keepalive_timeout 65;
    10. server {
    11. listen 80;
    12. server_name localhost;
    13. location / {
    14. root html;
    15. index index.html index.htm;
    16. }
    17. error_page 500 502 503 504 /50x.html;
    18. location = /50x.html {
    19. root html;
    20. }
    21. }
    22. # Nginx配置实例之反向代理2配置信息
    23. server {
    24. listen 9001;
    25. server_name localhost;
    26. location ~ /basketball/ {
    27. proxy_pass http://127.0.0.1:8080;
    28. }
    29. location ~ /football/ {
    30. proxy_pass http://127.0.0.1:8081;
    31. }
    32. }
    33. # Nginx负载均衡配置
    34. upstream loadBalanceServer{
    35. server 127.0.0.1:8080;
    36. server 127.0.0.1:8081;
    37. }
    38. server {
    39. listen 9002;
    40. server_name 127.0.0.1;
    41. location / {
    42. proxy_pass http://loadBalanceServer;
    43. }
    44. }
    45. }

    注意事项:修改完nginx.conf后,需要重新加载nginx的配置信息

    ./nginx -s reload

    2.6、测试

    http://192.168.173.222:9002/edu/index.html

    结果分析:通过上述配置,实现了负载均衡 

    三、坑总结

    坑1:配置完nginx的配置文件后,启动nginx如果出现下面的显示效果,说明upstream中配置的tomcat服务没有开启,需要开启才行。

     

    坑2、tomcat服务启动了,nginx也启动了,如果不能实现负载均衡功能,很有可能是tomcat由于端口冲突问题导致的,此时需要修改server.xml中的端口。

    Server:port、Connector  port、redirectPort。可以通过查看tomcat的日志查看具体的报错信息

    四、负载均衡的其他配置

    4.1、轮询(默认)

    4.2、weight(按照权重分配)

    1. weight 代表权重默认为 1,权重越高被分配的客户端越多。
    2. 指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。配置信息如下:
    3. upstream server_pool {
    4. server 192.168.6.148 weight=10;
    5. server 192.168.6.149 weight=6;
    6. }

    4.3、ip_hash

    1. 每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。 例如:
    2. upstream server_pool {
    3. ip_hash;
    4. server 192.168.5.21:80;
    5. server 192.168.5.22:80;
    6. }

    4.4、fair(第三方)

    1. 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    2. upstream server_pool{
    3. server 192.168.5.21:80;
    4. server 192.168.5.22:80;
    5. fair;
    6. }

  • 相关阅读:
    nacos配置管理
    java后端开发面试准备(1)-Redis缓存
    按图搜索义乌购商品(拍立淘) API
    离散数学考前小记
    【软件安装】ElasticSearch在Linux系统中的安装
    Git 命令大全
    磁盘io使用率高问题排查
    EMQ 助力青岛研博建设智慧水务平台
    mongodb如何删除数据并释放空间
    机器学习入门介绍
  • 原文地址:https://blog.csdn.net/HelloWorld20161112/article/details/132876358