原创  nginx 添加stream 模块代理tcp,连接redis ,nginx

分类:运维 2021-01-09T12:37:26    56人阅读   

1、nginx 必须是1.9.x以上才能添加stream 模块(异常:./configure: error: invalid option "--with-stream")

2、rtmp、stream、http 等各个模块监听的端口不能一样(异常:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use))

平滑升级1.8到最新 

1、下载最新版

​wget  http://nginx.org/download/nginx-1.19.6.tar.gz #下载
tar -zxvf nginx-1.19.6.tar.gz      #解压
cd nginx-1.19.6

2、升级

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V  #查看之前的编译参数信息
nginx version: nginx/1.8.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module 
[root@localhost ~]#
./configure  --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-stream&&make # 设置编译参数并编译,不要make install ,不然就成覆盖安装了 
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old  # 备份原有nginx
cp objs/nginx /usr/local/nginx/ #拷贝文件到运行目录
make upgrade #升级
nginx -s restart 
/usr/local/nginx/sbin/nginx -V # 查看版本信息 已经是1.19.6 了

3、添加stream 模块配置:

stream {
	 upstream redis {
		 server  127.0.0.1:6379 max_fails=3 fail_timeout=30s;
	 }

	 server {
		  listen 1935;
		  proxy_connect_timeout 1s;
		  proxy_timeout 3s;
		  proxy_pass redis;
	 }
}


现在可以通过1935 代理 mysql 、redis 的连接了 (以上配置只展示了 redis配置) 。


分享到: