zoukankan      html  css  js  c++  java
  • nginx 负载均衡

    1. 中文文档:https://www.nginx.cn/doc/
    2. 负载均衡几种策略:https://segmentfault.com/a/1190000014483200       https://www.cnblogs.com/yanggb/p/10895326.html
    3. 轮询,ip_hash, 加权weight,least_conn最少连接方式,fair响应时间最少的优先(需要安装第三方包),url_hash 也需要安装第三方包
    4. 采用轮询时,简单测试分析。nginx配置
      upstream test_upstream {
          #ip_hash;
          server 127.0.0.1:8867;
          server 127.0.0.1:8870;
      }
      
      server{
          server_name localhost;
          listen 8869;
      
          location / {
              proxy_pass http://test_upstream;
          }
      
      
      }
      upstream
      server {
              listen        8870;
              server_name  localhost;
              root   "E:upstream";
              location / {
                  index index.php index.html;
      
              }
              location ~ .php(.*)$ {
                  fastcgi_pass   127.0.0.1:9000;
                  fastcgi_index  index.php;
                  #fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
                  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                  fastcgi_param  PATH_INFO  $fastcgi_path_info;
                  fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                  include        fastcgi_params;
              }
      }
      
      // 另一个server文件相似,监听不同端口,目录不同,配置不列出来了
      server

      PHP测试代码8867端口对应第一个PHP脚本,8870端口对应第二个PHP脚本

      <?php
      $date = date('H:i:s');
      
      file_put_contents('./access.log', "$date --- request arrived
      ", FILE_APPEND);
      echo 'upstream directive must be outside server block.';
      sleep(5);
      file_put_contents('./access.log', "response return 
      ", 8);
      PHP
      <?php
      
      $date = date('H:i:s');
      file_put_contents('./access.log', "$date --- request arrived(upstream dir)
      ", 8);
      phpinfo();
      
      file_put_contents('./access.log', "response return(upstream dir)
      ", 8);
      View Code

      (1)此策略会根据请求的先后时间顺序分发请求(为了方便区分,一个PHP测试代码里加上了延时5秒,另一个PHP脚本没加延时,并且写入日志文件进行分析判断),在测试中发现:打开一个标签页发起请求(在请求有延时的脚本没有返回结果前,再次刷新就会被轮询到另一个服务端口了,之前的请求就停止了~这个可以根据设置的日志判断出来)我设置的是在请求刚到达的时候写入一次日志,然后延时5秒后再写入一次日志。这样按照上面的操作(有延时那个PHP脚本没有返回结果前刷新页面,记住手速要快~)。当前请求的那个脚本就停止了(看日志会发现只写入一次)。  (浏览器不断刷新,对服务器发起多个请求,最终浏览器只会显示一个结果,但是服务器却是把每个请求都处理完。PHP好像是可以设置如果客户端断开连接,服务端代码也可以停止执行。https://www.php.net/manual/zh/features.connection-handling.php
      (2)在测试过程中发现一个问题(测试代码中一个是加了延时的PHP脚本,另一个是没加延时的),在发起一个请求的同时,再打开一个新的tab发起请求(应该被分发到没有延时的PHP服务,但是发现要等到第一个请求结束后,第二个才会接受到响应)在网上搜索,发现鸟哥很早之前提过这个问题。浏览器对相同url的请求串行化问题:https://www.laruence.com/2011/07/16/2123.html

  • 相关阅读:
    SqlServer 查看数据库中所有存储过程
    SqlServer 查看数据库中所有视图
    SqlServer 查询表的详细信息
    SqlServer 遍历修改字段长度
    net core 操作Redis
    Tuning SharePoint Workflow Engine
    Open With Explorer
    Download language packs for SharePoint 2013
    Change Maximum Size For SharePoint List Template when Saving
    Six ways to store settings in SharePoint
  • 原文地址:https://www.cnblogs.com/bneglect/p/13553204.html
Copyright © 2011-2022 走看看