nginx代理TCP端口如何配置(亲测)


#如果要代理TCP端口,需要在配置编译的时候加上stream模块

1、查看是否已存在stream模块
nginx -V
root@zyl:~# nginx -V
nginx version: nginx/1.19.3
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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-compat --with-file-aio --with-threads --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_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.3/debian/debuild-base/nginx-1.19.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

可以看到已包含了stream相关模块:
-with-stream --with-stream_ssl_module

2、如果没有该模块,重新编译nginx添加模块步骤如下

参考:https://blog.csdn.net/weixin_30892987/article/details/96127891?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control

3、nginx编译安装之-./configure 参数简单介绍
--prefix=/etc/nginx 	指向安装目录。
--sbin-path=/usr/sbin/nginx 	 指定执行程序文件存放位置。
--modules-path=/usr/lib/nginx/modules 	指定第三方模块的存放路径
--conf-path=/etc/nginx/nginx.conf	指定配置文件存放位置。

#修改配置文件

stream 与 http 是一个层级的,放到http节点后面即可。

stream { 
	upstream vod { 
		#hash $remote_addr consistent; 
		server 172.18.254.161:1680; #weight=5; 
	}
	server {
		listen 1680;
		#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		#ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
		#ssl_certificate /home/zyzx/STAR_haut_edu_cn.crt;#这里需要填写证书的地址,可以是pem或者crt
		#ssl_certificate_key /home/zyzx/STAR_haut_edu_cn.key;#这里需要填写证书key的地址
		proxy_connect_timeout 10s;
		proxy_timeout 30s;
		proxy_pass vod;
	}
}

或者这样

stream { 
	server {
		listen 1680;
		#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		#ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
		#ssl_certificate /home/zyzx/STAR_haut_edu_cn.crt;#这里需要填写证书的地址,可以是pem或者crt
		#ssl_certificate_key /home/zyzx/STAR_haut_edu_cn.key;#这里需要填写证书key的地址
		proxy_connect_timeout 10s;
		proxy_timeout 30s;
		proxy_pass 172.18.254.161:1680;
	}
}

#参考

http://www.weixueyuan.net/a/751.html


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值