NGINX 端口转发

下载安装nginx:

cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-stream --without-http
make && make install

注意上边的 --with-stream 用来处理tcp/udp转发的

修改配置文件:
vi /usr/local/nginx/conf/nginx.conf


#user nobody;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
worker_rlimit_nofile 65535;

events {
use epoll;
worker_connections 65535;
}


stream {
#tcp
server {
listen 80;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass 192.168.2.126:80;
}
#udp
server {
listen 2999 udp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass 192.168.2.126:2999;

}
}
#负载转发
# stream {
# upstream backend1 {
# server 192.168.1.78:8181;
# server 192.168.1.79:8181;
# }
# server {
# listen 10008;
# proxy_connect_timeout 5s;
# proxy_timeout 20s;
# proxy_pass backend1;
# }
# }

 

启动nginx

即可完成,注意防火墙添加端口