編譯nginx平滑添加stream模塊


https://www.cnblogs.com/crysmile/p/9565048.html

--很好

1、操作背景

操作系統版本:CentOS Linux release 7.4.1708 (Core)
nginx版本:1.13.4

nginx從1.9.0版本開始,新增了ngx_stream_core_module模塊,使nginx支持四層負載均衡。默認編譯的時候該模塊並未編譯進去,需要編譯的時候添加--with-stream,使其支持stream代理。

2、nginx編譯添加stream模塊

2.1、查看原nginx編譯參數

[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.2、添加stream模塊進行重新編譯

1
2
3
4
此處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.3、進行make操作

此處nginx源碼目錄為:/usr/local/src/nginx-1.13.4,即為編譯命令執行目錄。
make

此處一定不能使用make install命令,執行該命令會將原有nginx目錄進行覆蓋。

3、關停nginx同時復制新的nginx啟動文件

1
2
3
4
5
6
7
8
關閉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

4、啟動測試 

復制代碼
啟動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內了。
復制代碼

5、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端口。

1
2
3
4
5
6
7
8
9
10
11
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;
     }
}

 

6、編譯nignx systemd服務啟動文件

復制代碼
[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]
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM