需求:生產有個接口是通過socket通信。nginx1.9開始支持tcp層的轉發,通過stream實現的,而socket也是基於tcp通信。
實現方法:Centos7.2下yum直接安裝的nginx,添加新模塊支持tcp轉發;重新編譯Nginx並添加 --with-stream 參數。
實現過程:
1. 查看nginx版本模塊
[root@pre ~]# nginx -V

2. 下載一個同版本可編譯的Nginx
cd /opt
wget http://nginx.org/download/nginx-1.12.2.tar.gz tar xf nginx-1.12.2.tar.gz && cd nginx-1.12.2
3. 備份原Nginx文件
mv /usr/sbin/nginx /usr/sbin/nginx.bak cp -r /etc/nginx{,.bak}
4. 重新編譯Nginx
檢查模塊是否支持,比如這次添加 limit 限流模塊 和 stream 模塊:
./configure –help | grep limit
這里寫圖片描述
ps:-without-http_limit_conn_module disable 表示已有該模塊,編譯時,不需要添加
./configure –help | grep stream
這里寫圖片描述
ps:–with-stream enable 表示不支持,編譯時要自己添加該模塊
根據第1步查到已有的模塊,加上本次需新增的模塊: --with-stream
cd /opt/nginx-1.12.2 ./configure --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --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_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --with-stream
以上編譯時,如出現缺少依賴,一般需要安裝以下模塊,安裝完再次編譯:
yum -y install libxml2 libxml2-dev libxslt-devel yum -y install gd-devel yum -y install perl-devel perl-ExtUtils-Embed yum -y install GeoIP GeoIP-devel GeoIP-data yum -y install pcre-devel yum -y install openssl openssl-devel
5. 編譯通過,繼續驗證
繼續輸入:make
make完成后不要繼續輸入“make install”,以免現在的nginx出現問題
以上完成后,會在objs目錄下生成一個nginx文件,先驗證:
/opt/nginx-1.12.2/objs/nginx -t /opt/nginx-1.12.2/objs/nginx -V
6. 替換Nginx文件並重啟
cp /opt/nginx-1.12.2/objs/nginx /usr/sbin/ nginx -s reload
7. 檢查
[root@pre ~]# nginx -V

添加模塊成功!!!