nginx容器启动非常方便,省去了编译安装的麻烦,但是,官方镜像没有安装一些第三方的模块,所以在已有容器中添加模块,一开始感觉不太可能,但是网上搜索了一下发现可以实现给容器添加模块,所以特意记录下来遇到的一些问题和思考,给有同样需求的小伙伴一些帮助。
首先需要到容器里查看nginx版本和configure的参数配置,进入容器,使用nginx -V命令查看:
如上图所示,笔者的nginx版本是1.16.1,其实笔者之前的nginx版本是1.20.2,最新的稳定版本,但是后来降级换成了1.16.1版本的。为啥降级,先卖个关子,后面再说。
然后,到nginx官网去下载1.16.1版本的nginx源码包,下载路径:nginx.org/en/download…
如上图所示,选择红框的版本单击下载。 接着,克隆第三方主动检查后端服务模块,git clone github.com/yaoweibin/n… 代码仓库。
笔者将上面两个文件都放在了一个目录下/opt/cxweb/soft-temp,这样方便后续安装配置。
接下来,进入该目录/opt/cxweb/soft-temp,解压缩nginx源码包,执行 tar -zxvf nginx-1.16.1.tar.gz,
然后,进入nginx解压目录,运行以下命令,给nginx打第三方模块补丁:
patch -p1 < ../nginx_upstream_check_module/check_1.16.1+.patch
这里回答下之前说的关子,笔者之前用的nginx1.20.2版本,到这一步的时候,打的补丁是check_1.20.1+.patch,发现死活打不上去,报错,后来无奈换成了1.16.1版本。
接下来就是运行 ./configure XXXX --add-moudle=../nginx_upstream_check_module,其中XXXX部分就是上面进入容器里面nginx -V 查看的编译配置,在本例中是:--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.16.1/debian/debuild-base/nginx-1.16.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=../nginx_upstream_check_module/,执行./configure发现报错,缺少pcre 和 openssl库,执行下面命令安装: yum install -y pcre pcre-devel yum install openssl-devel,之后就可以正常配置成功,然后执行make编译nginx。
这一通下来,nginx就编译成功了,下面就是把编译后的nginx映射到容器里面,只要将容器删除,然后重新配置启动脚本,执行的命令分别如下:
1.删除已有的容器: 使用docker ps 查看nginx容器id,然后使用 docker rm -f 容器id,删除容器
2.配置nginx启动脚本,将nginx编译后的文件映射进去,并且也要将一些lib库映射进去,脚本如下:
docker run --name config-nginx -p 80:80 -p 443:8443 -v /opt/cxweb/nginx/html:/etc/nginx/html -v /opt/cxweb/nginx/front:/mnt/front -v /opt/cxweb/nginx/prd:/mnt/prd -v /opt/cxweb/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -v /opt/cxweb/nginx/conf:/etc/nginx/conf.d -v /opt/cxweb/soft-temp/nginx-1.16.1/objs/nginx:/usr/sbin/nginx -v /usr/lib64:/usr/lib -d nginx:1.16.1
其中的 -v /opt/cxweb/soft-temp/nginx-1.16.1/objs/nginx:/usr/sbin/nginx 和 -v /usr/lib64:/usr/lib 就是将nginx编译后的文件和依赖的lib库映射进去。
这样配置的nginx容器就是有主动检查后端服务的能力了。
接下来简单演示下如何使用主动检测:
upstream rpa-api-cluster {
server 172.21.15.72:59896;
server 172.21.15.73:59896;
#http健康检查相关配置
check interval=3000 rise=2 fall=3 timeout=3000 type=http;
#/health/status为后端健康检查接口
check_http_send "GET /heartbeat/heartbeat HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name rpa-api.njtlh.cn;
location ~* \.(ftpquota|htaccess|asa|mdb)$ {
deny all;
}
location / {
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 30s;
proxy_pass http://rpa-api-cluster/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
以上就是本次分享的内容,如果觉得有帮助,请一键三连,有问题请留言,我会及时解答,谢谢!