linux下nginx安装其他模块

原创 2018-01-18 17:16 阅读(1989)次

nginx安装时默认并没有把所有模块都安装了,所以有时我们需要一些模块时,必须要自己再安装这些模块,比如负载均衡要用的一致性hash模块

要利用一致性hash实现下面的配置,就要自己安装一致性hash模块了

upstream backend_server1 { 
consistent_hash $request_uri;
server 192.168.8.195:80 weight=5 ;
#server 10.10.101.47:81 weight=5 ;
}
我的环境是nginx 1.10.3,是使用yum安装的,但是要安装其他的模块,必须使用源码安装,百度了一下,大神们都是用源码安装好整个nginx,安装时再带上安装模块,安装完后,再把当前系统里的nginx执行文件替换成源码安装完后的,具体情况如下:
1.查看当前nginx版本以及安装好的模块:
   nginx -V 查看:
    nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled

configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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-file-aio --with-threads --with-ipv6 --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_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

把configure arguments安装参数记录下来,等下用源码安装时要用
2.下载源码包和模块包
   nginx源码包:http://nginx.org/en/download.html
   ngx_http_consistent_hash源码包:https://github.com/replay/ngx_http_consistent_hash
   下载完后解压

   放在/home/nginx/目录下

3.更新下依赖包
  yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel  

4.进入解压后的nginx目录运行configure脚本,安装参数使用上面记录下来的参数并加上解压好的consistent_hash源码
  --add-module=/home/nginx/ngx_http_consistent_hash-master :
  具体如下:
  ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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-file-aio --with-threads --with-ipv6 --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_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/home/nginx/ngx_http_consistent_hash-master  
5.成功后,make
  ./objs/nginx -t测试新安装后的nginx能不能启动
6.替换nginx脚本
  备份 cp /usr/sbin/nginx /usr/sbin/nginx-bak
  替换 cp ./objs/nginx /usr/sbin/nginx
  启动nginx :service nginx start