美文网首页
nginx + upsync-module实现配置动态更新

nginx + upsync-module实现配置动态更新

作者: andrewkk | 来源:发表于2020-12-29 10:31 被阅读0次

参考:
https://github.com/weibocom/nginx-upsync-module
https://github.com/xiaokai-wang/nginx_upstream_check_module

自己可制作镜像:这里个人是参考大佬的镜像 优点就是免编译dockerfile
资源如下:
https://github.com/qist/k8s/blob/master/dockerfile/alpine-nginx/Dockerfile
构建镜像容器命令:
docker pull juestnow/nginx:1.19.6
docker run --name nginx-test -d -p 80:80 -p 8080:8080 -p 443:443 --net=host --restart=always -v /opt/nginx/www:/usr/share/nginx/html -v /opt/nginx/servers:/etc/nginx/servers -v /opt/nginx/conf.d:/etc/nginx/conf.d -d juestnow/nginx:1.19.6

============================================================================

docker run --name nginx-test -d -p 80:80 -p 443:443 \
--restart=always \
-v /opt/nginx/www:/usr/share/nginx/html \
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/logs:/var/log/nginx \
-v /opt/nginx/conf.d:/etc/nginx/conf.d \
-d nginx:1.19.6

wget http://nginx.org/download/nginx-1.19.6.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz
git clone https://github.com/weibocom/nginx-upsync-module.git
git clone https://github.com/xiaokai-wang/nginx_upstream_check_module.git
git clone git://github.com/gnosek/nginx-upstream-fair.git

tar -xf nginx-1.19.6.tar.gz && cd nginx-1.19.6
patch -p1 < /opt/nginx_upstream_check_module/check_1.12.1+.patch
image.png
如果只需用到nginx_upstream_check_module + nginx-upsync-module 即可如下安装 具体看自己需求
./configure --add-module=/opt/nginx_upstream_check_module --add-module=/opt/nginx-upsync-module
image.png
make
make install
image.png
核心模块:
--with-openssl-opt=enable-tls1_3 \
--add-module=../nginx-upsync-module \
--add-module=../nginx_upstream_check_module \
--with-openssl=../openssl-1.1.1i 

基本模块:
注释:目录可以自定义好 笔者用的是如下配置参数编译
./configure --prefix=/apps/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/apps/nginx/log/error.log \
--http-log-path=/apps/nginx/log/access.log \
--pid-path=/apps/nginx/run/nginx.pid \
--lock-path=/apps/nginx/run/nginx.lock \
--http-client-body-temp-path=/apps/nginx/cache/client_temp \
--http-proxy-temp-path=/apps/nginx/cache/proxy_temp \
--http-fastcgi-temp-path=/apps/nginx/cache/fastcgi_temp \
--http-uwsgi-temp-path=/apps/nginx/cache/uwsgi_temp \
--http-scgi-temp-path=/apps/nginx/cache/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-pcre \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-openssl=../openssl-1.1.1i \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-ipv6 \
--with-openssl-opt=enable-tls1_3 \
--add-module=../nginx-upsync-module \
--add-module=../nginx_upstream_check_module 



检查:
which nginx 
image.png
docker cp /objs nginx-test:/etc/nginx/
docker exec -it nginx-test /bin/bash
cd /etc/nginx/objs  
cp nginx /  /usr/sbin/nginx  #替换掉编译后的nginx文件

nginx-t
ldd /usr/sbin/nginx
image.png
找一个可用的pcre拷贝进容器
docker cp /tmp/libpcre.so.1 nginx-test:/lib/x86_64-linux-gnu/
docker cp /tmp/libpcre.so.1.2.0 nginx-test:/lib/x86_64-linux-gnu/
image.png
测试nginx动态配置:
upstream bmsapi {
     server 127.0.0.1:2365;
     upsync 127.0.0.1:8500/v1/catalog/service/imagine-bms upsync_timeout=20m upsync_interval=500ms upsync_type=consul_services strong_dependency=off;
     upsync_dump_path /etc/nginx/servers/www.imagine-bms.com.conf;
     check interval=3000 rise=2 fall=5 timeout=10000;
         keepalive 20000;
}
server {
    listen 80;
    server_name test-docker-bmsapi.xxx.com;
    large_client_header_buffers 4 128k;
    location / {
        client_body_buffer_size 1024k;
        client_max_body_size    0;
        proxy_connect_timeout 300s;
        proxy_send_timeout   900;
        proxy_read_timeout   900;
        proxy_buffer_size    64k;
        proxy_buffers      4 32k;
        proxy_busy_buffers_size 64k;
        proxy_redirect     off;
        proxy_hide_header  Vary;
        proxy_set_header   Accept-Encoding '';
        proxy_set_header   Host   $host;
        proxy_set_header   Referer $http_referer;
        proxy_set_header   Cookie $http_cookie;
        proxy_set_header   X-Real-IP  $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_buffering    off;
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        proxy_temp_file_write_size   64k;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://bmsapi/;
    }
}

最后通过upstream bmsapi获取动态docker-java容器端口 成功部署:


image.png

相关文章

网友评论

      本文标题:nginx + upsync-module实现配置动态更新

      本文链接:https://www.haomeiwen.com/subject/kshfoktx.html