一、操做背景nginx
操做系統版本:CentOS Linux release 7.4.1708 (Core) nginx版本:1.13.4
nginx從1.9.0版本開始,新增了ngx_stream_core_module模塊,使nginx支持四層負載均衡。默認編譯的時候該模塊並未編譯進去,須要編譯的時候添加--with-stream,使其支持stream代理。
二、nginx編譯添加stream模塊web
2.一、查看原nginx編譯參數bash
[root@test-server sbin]# nginx -V nginx version: nginx/1.13.4 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41
2.二、添加stream模塊進行從新編譯負載均衡
此處nginx源碼目錄爲:/usr/local/src/nginx-1.13.4,即爲編譯命令執行目錄。 編譯命令以下: ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41 --with-stream
2.三、進行make操做dom
此處nginx源碼目錄爲:/usr/local/src/nginx-1.13.4,即爲編譯命令執行目錄。 make 此處必定不能使用make install命令,執行該命令會將原有nginx目錄進行覆蓋。
三、關停nginx同時複製新的nginx啓動文件socket
關閉nginx服務 systemctl stop nginx 備份原有nginx二進制文件。 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-no-strem 複製新編譯好的nginx二進制文件。今後處nginx源碼目錄爲:/usr/local/nginx-1.13.4。即爲編譯命令執行目錄。 cp ./objs/nginx /usr/local/nginx/sbin/nginx
四、啓動測試 tcp
啓動nginx。 systemctl start nginx 查看nginx模塊信息。 nginx -V nginx version: nginx/1.13.4 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41 --with-stream 能夠看到stream模塊已經編譯到nginx內了。
五、nginx stream模塊配置簡析測試
stream段的配置要與http段在同級目錄。此處引用的爲官方nginx說明配置。
stream { upstream backend { hash $remote_addr consistent; server backend1.example.com:12345 weight=5; server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3; } upstream dns { server 192.168.0.1:53535; server dns.example.com:53; } server { listen 12345; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass backend; } server { listen 127.0.0.1:53 udp reuseport; proxy_timeout 20s; proxy_pass dns; } server { listen [::1]:12345; proxy_pass unix:/tmp/stream.socket; } }
舉一個栗子,利用stream模塊代理 zk服務的2181端口。ui
stream { upstream zk_server { server 172.16.3.8:2181 weight=5; } server { listen 2181 tcp; proxy_responses 1; proxy_timeout 20s; proxy_pass zk_server; } }
六、編譯nignx systemd服務啓動文件spa
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install]