zoukankan      html  css  js  c++  java
  • nginx upstream 一致性哈希模块

    ngx_http_upstream_consistent_hash 模块是一个负载均衡器,使用一个内部一致性hash算法来选择合适的后端节点。与PHPmemcache模块memcache.hash_strategy兼容,这意味着可以使用php-memcache模块将内容存储到memcached集群中,而后通过nginx在集群中找到值。

    该模块通过使用客户端信息(如:$ip, $uri, $args等变量)作为参数,使用一致性hash算法将客户端映射到后端节点。

    该模块可以根据配置参数采取不同的方式将请求均匀映射到后端机器,比如:

    • consistent_hash $remote_addr:可以根据客户端ip映射
    • consistent_hash $request_uri: 根据客户端请求的uri映射
    • consistent_hash $args:根据客户端携带的参数进行映射

    指令

    语法:consistent_hash    variable_name

    默认值:none

    上下文:upstream

    配置upstream采用一致性hash作为负载均衡算法,并使用配置的变量名作为hash输入。

    2

    3

    4

    5

    # cd /tmp

    # wget https://github.com/replay/ngx_http_consistent_hash/archive/master.zip

    # unzip master.zip

    ##下面的步骤实在编译nginx的时候写的

    nginx第三方模块安装方法:

    ./configure --prefix=

    1

    cd /tmp/nginx-1.14.0

    ./configure --prefix=/你的安装目录  --add-module=/第三方模块目录

    在未安装nginx的情况下安装nginx第三方模块

    1

    2

    3

    4

    5

    6

    7

    8

    # ./configure --prefix=/usr/local/nginx-1.4.1

    --with-http_stub_status_module

    --with-http_ssl_module --with-http_realip_module

    --with-http_image_filter_module

    --add-module=../ngx_pagespeed-master --add-module=/第三方模块目录

    # make

    # make install

    # /usr/local/nginx-1.4.1/sbin/nginx

    在已安装nginx情况下安装nginx模块

    # ./configure --prefix=/usr/local/nginx-1.4.1 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --add-module=../ngx_pagespeed-master # make # /usr/local/nginx-1.4.1/sbin/nginx -s stop # cp objs/nginx /usr/local/nginx/sbin/nginx # /usr/local/nginx-1.4.1/sbin/nginx

    1

    2

    3

    4

    5

    6

    7

    8

    9

    # ./configure --prefix=/usr/local/nginx-1.4.1

    --with-http_stub_status_module

    --with-http_ssl_module --with-http_realip_module

    --with-http_image_filter_module

    --add-module=../ngx_pagespeed-master

    # make

    # /usr/local/nginx-1.4.1/sbin/nginx -s stop

    # cp objs/nginx /usr/local/nginx/sbin/nginx

    # /usr/local/nginx-1.4.1/sbin/nginx

    相比之下仅仅多了一步覆盖nginx文件.

    总结,安装nginx安装第三方模块实际上是使用--add-module重新安装一次nginx,不要make install而是直接把编译目录下objs/nginx文件直接覆盖老的nginx文件.如果你需要安装多个nginx第三方模块,你只需要多指定几个相应的--add-module即可.

    [warning]备注:重新编译的时候,记得一定要把以前编译过的模块一同加到configure参数里面.[/warning]

    upstream somestream {

          consistent_hash $request_uri;

          server 10.50.1.3:11211;

          server 10.50.1.4:11211;

          server 10.50.1.5:11211;

        }

    ...

    server {

            listen       80;

            server_name  localhost;

            location / {

              default_type text/html;

              set $memcached_key $request_uri;

              memcached_pass somestream;

              error_page      500 404 405 = @fallback;

            }

            location @fallback {

              root /srv/www/whatever;

              fastcgi_intercept_errors on;

              error_page 404 = @404;

              set $script $uri;

              set $path_info "";

              include /usr/local/nginx/conf/fastcgi_params;

              fastcgi_param SCRIPT_FILENAME /srv/www/whatever/test.php;

              fastcgi_param SCRIPT_NAME $script;

              fastcgi_param REQUEST_URI $uri;

              fastcgi_pass   127.0.0.1:9000;

            }

        }

  • 相关阅读:
    .net开发微信(1)——微信订阅号的配置
    工作中EF遇到的问题
    .net Entity Framework初识1
    Razor视图
    jquery中利用队列依次执行动画
    .net找List1和List2的差集
    angularjs ng-if 中的ng-model 值作用域问题
    Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题
    springboot 启动排除某些bean 的注入
    angularjs 初始化方法执行两次以及url定义错误导致传值错误问题
  • 原文地址:https://www.cnblogs.com/hoewang/p/10257217.html
Copyright © 2011-2022 走看看