nginx修改upstream不重启的方法(ngx_http_dyups_module模块)

凉白开 Nginx1 59,5405字数 3211阅读10分42秒阅读模式

nginx很强大,第三方模块也不少,淘宝在nginx上很活跃,特别是章亦春,他参与的模块至少10+, 好了今天主角不是他,是一款动态配置upstream的模块,这个模块使用rest接口. 简单,方便,并且可以不需要重启nginx。但是有个问题比较明显,nginx重启之后,什么都没了.

1. 安装ngx_http_dyups_module

首先安装nginx动态upstream配置模块,如果你已经安装了nginx,那么轻参考ttlsa上的如何安装nginx第三方模块,会安装的请跳过.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# cd /usr/local/src/
# wget https://github.com/yzprofile/ngx_http_dyups_module/archive/master.zip \
-O  ngx_http_dyups_module-master.zip
# unzip ngx_http_dyups_module-master.zip
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module 
\--add-module=../ngx_http_dyups_module-master/
# make
# make install

2. 指令(Directives)

语法: dyups_interface
默认: none
配置段: location
启用配置upstream的接口文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

语法: dyups_read_msg_timeout time
默认: 1s
配置段: main
设置从共享内存中读取commands的超时时间,默认为1秒文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

语法: dyups_shm_zone_size size
默认: 2MB
配置段: main
设置存储commands的共享内存
This directive set the size of share memory which used to store the commands.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

语法: dyups_upstream_conf path
默认: none
配置段: main
这个指令用来指定upstream配置文件的路径,他会在启动的时候加载文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

语法: dyups_trylock on | off
默认: off
配置段: main
是否启用锁,如果启用了它,同一时刻有人在修改,那么将会返回409.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

3. restful接口

GET
/detail 获取所有upstream名称以及upstream里面的servers信息
/list 获取upstream列表
/upstream/name 使用upstream名称获取upstream信息文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

POST
/upstream/name 更新upstream
body 配置内容;
body server ip:port;文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

DELETE
/upstream/name 删除upstream,name相应修改文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

3.1 调用接口响应http状态码文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

500: 需要reload nginx
409: 重新调用一次接口,上个请求被锁了.
204:调用list或者detail时出现,表示没有响应内容
其他:你的命令错误,请修改
注意:你需要第三方模块来生成新的配置文件到nginx配置目录. 作者也没有说什么第三方模块,这个插件很好,不能生成配置文件,让他显得尤为不足.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

4. nginx配置

备注:以下配置有安装echo模块.文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

http {
    # 从upstream读取初始upstream配置
    dyups_upstream_conf  conf/upstream.conf;
    include conf/upstream.conf;

    # 默认主机
    server {
        listen   80;
        location / {
            proxy_pass http://$host;
        }
    }

    # 动态配置upstream的接口站点
    server {
        listen 81;
        location / {
            dyups_interface; # 这个指令表示这边是接口站点
        }
    }

    # upstream后面的realserver,2台801,,82
    server {
        listen 801;
        location / {
            echo 801; 
        }
    }

    server {
        listen 802;
        location / {
            echo 802;
        }
    }
}

upstream.conf配置文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

upstream ttlsa1 {
    server 127.0.0.1:801;
}

upstream ttlsa12 {
    server 127.0.0.1:802;
}

5. 使用方法演示

5.1 添加upstream文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# curl -d "server 127.0.0.1:801;server 127.0.0.1:802;" 127.0.0.1:81/upstream/ttlsa3
success

测试文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# curl -H "host: ttlsa3" 127.0.0.1
801

# curl -H "host: ttlsa3" 127.0.0.1
802

可以看到通过host的ttlsa3可以访问到upstream配置的两台服务器。如果你发现curl几次都是一样的,那么轻多试几次。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

5.2 查看upstream详细信息文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# curl 127.0.0.1:81/detail
ttlsa1
server 127.0.0.1:801

ttlsa2
server 127.0.0.1:802

ttlsa3
server 127.0.0.1:801
server 127.0.0.1:802

5.3 删除upstream文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# curl -i -X DELETE 127.0.0.1:81/upstream/ttlsa1
success

# curl 127.0.0.1:81/detail
ttlsa2
server 127.0.0.1:802

ttlsa3
server 127.0.0.1:801
server 127.0.0.1:802

5.4 增加带ip_hash的upstream文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

# curl -d "ip_hash;server 127.0.0.1:801;server 127.0.0.1:802;" 127.0.0.1:81/upstream/ttlsa4
success

# curl 127.0.0.1:81/upstream/ttlsa4
server 127.0.0.1:801
server 127.0.0.1:802

为什么没有带ip_hash的信息,本身就无法显示,那我们在看看weight会不会显示出来文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/

5.5 增加带weight的upstream

# curl -d "server 127.0.0.1:801;server 127.0.0.1:802 weight=2;" 127.0.0.1:81/upstream/ttlsa5
success

# curl 127.0.0.1:81/upstream/ttlsa5
server 127.0.0.1:801
server 127.0.0.1:801

还是不显示,虽然没显示,但是效果还是有的,大家自己去测试吧.

6. 注意事项

本模块不能和nginx_upstream_check_module一起使用,接下来的版本会支持。或者可以使用tenengine。淘宝真是不遗余力在推广他们的tenengine.

7. 结束语

ngx_http_dyups_module带的功能我很喜欢,但是最大的不足就是不能生成配置文件,所有内容都保存在内存中,希望以后的版本能够支持。有这个模块,shell脚本也可以修改upstream,不在需要重启nginx。

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 02/10/2013 04:51:08
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-modules-ngx_http_dyups_module/
评论  1  访客  1
    • K
      K 0

      你好,我参考你的步骤搭了Tengine,并使用了该模块,发现可以动态修改,用detail访问可看到修改后的结果,但是我用部署tomcat集群,通过tengine访问,发现修改后的upstream并没有生效,访问的时候还是以配置文件nginx.conf里的内容为准。请问该问题该如何解决,万分感谢

    评论已关闭!