玩玩 Nginx 2-----给Nginx添加第三方模块(动态更新upstream)

      接上一篇,我们在初始化安装的时候添加了nginx_lua模块,然后了解到nginx不可以动态加载模块,所以当你安装第三方模块的时候需要覆盖nginx文件.接下来一起看看如何安装nginx第三模块吧!

     1.我们先看看 目前nginx的配置

[root@localhost src]# cd /usr/local/nginx/
[root@localhost nginx]# ./sbin/nginx  -V
nginx version: nginx/1.8.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_devel_kit-0.2.19 --add-module=/usr/local/src/lua-nginx-module-0.9.13
[root@localhost nginx]# 

     2.今天我们以一款动态配置upstream的模块为例子 (lua-upstream-nginx-module) https://github.com/yzprofile/ngx_http_dyups_module

[root@localhost src]# wget https://github.com/yzprofile/ngx_http_dyups_module/archive/master.tar.gz \
> -O  ngx_http_dyups_module-master.tar.gz

     3.解压,编译

[root@localhost src]# tar  -zvxf ngx_http_dyups_module-master.tar.gz
[root@localhost src]# cd nginx-1.8.0
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_devel_kit-0.2.19 --add-module=/usr/local/src/lua-nginx-module-0.9.13 --add-module=/usr/local/src/ngx_http_dyups_module-master
[root@localhost src]# make

    4.然后覆盖nginx文件并重新开启nginx

[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx  -s stop
[root@localhost nginx-1.8.0]# cp objs/nginx /usr/local/nginx/sbin/nginx 
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx  

    5.测试下原有lua模块 正常

   

    6.下面测试我们的lua-upstream-nginx-module

        先修改nginx  主要是添加 动态配置upstream的接口站点 我们这里用端口81来 默认配置了 ttlsa1跟ttlsal2

        [root@localhost nginx]# vim conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    
    upstream ttlsa1 {
        server 127.0.0.1:801;
    }

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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hello { 
            default_type 'text/plain'; 
            content_by_lua 'ngx.say("hello, lua")'; 
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
View Code

      6.1然后重启nginx

      [root@localhost nginx]# ./sbin/nginx -s reload 

      6.2开始测试

           浏览器访问 http://10.12.7.32:81/list 

           我这里是远程访问,没有开启81端口所以第一次悲剧了

           编辑防火墙列表

           [root@localhost nginx]# vim /etc/sysconfig/iptables 

           把81端口添加

          

           不过你要是在centos操作的话就忽略此步骤

           测试无非就是http请求

           centos下操作

[root@localhost nginx]# curl -d "server 127.0.0.1:801;server 127.0.0.1:802;" 127.0.0.1:81/upstream/ttlsa3 
[root@localhost nginx]# curl 127.0.0.1:81/detail
ttlsa1
server 127.0.0.1:801 weight=1 max_fails=1 fail_timeout=10 backup=0 down=0

ttlsa12
server 127.0.0.1:802 weight=1 max_fails=1 fail_timeout=10 backup=0 down=0

ttlsa3
server 127.0.0.1:801 weight=1 max_fails=1 fail_timeout=10 backup=0 down=0
server 127.0.0.1:802 weight=1 max_fails=1 fail_timeout=10 backup=0 down=0
View Code

           浏览器下

                

               

                 

           ok,http的话c#操作肯定很方便,不过据说这家伙不能upstream 持久化,nginx重启后就啥也没有了,接下来还是的看看有没有其他更好的!

           目前看nginx资料都是在一些运维的站点比较多,前两篇也是结合http://www.ttlsa.com去看的,网站不错,名字也很棒《运维生产时间》!

posted @ 2017-03-30 17:01  __Burt  阅读(1267)  评论(0编辑  收藏  举报