Nginx stream模块来端口转发

wget https://nginx.org/download/nginx-1.18.0.tar.gz
ls
tar -zxvf nginx-1.18.0.tar.gz
yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
cd nginx-1.18.0
./configure –prefix=/opt/nginx –sbin-path=/opt/nginx/sbin/nginx –conf-path=/opt/nginx/conf/nginx.conf –with-http_stub_status_module –with-http_gzip_static_module –with-stream
make
make install
cd /opt/
ls
cd nginx/
cd conf/
vi nginx.conf
添加本地10091端口转发ip和端口192.168.0.91:2222

stream {
server {
listen 10091;
proxy_connect_timeout 600s;
proxy_timeout 3s;
proxy_pass 192.168.0.91:2222;
proxy_socket_keepalive on;
}
}

cd ..
cd sbin/
./nginx -t
./nginx
netstat -tnlp

  ps -ef | grep nginx

  从容停止   kill -QUIT 主进程号

  快速停止   kill -TERM 主进程号

  强制停止   kill -9 nginx

按上面来测试后发现ssh过不去

[root@Test1 ~]# ssh -p 10091 root@192.168.0.66
root@192.168.0.66’s password:
Authentication failed.

输入密码后认证失败,查看日志

Mar 14 08:30:37 TEST2 sshd[1220]: Connection closed by 192.168.0.66 port 52548 [preauth]

其实stream模块按标准写就好了

stream {
upstream ssh {

    hash $remote_addr consistent;
    server 192.168.0.91:2222;
}
 server {
    listen 10091;
    proxy_connect_timeout 60s; 
    proxy_timeout 60s;
    proxy_pass ssh;
}

}

Mar 14 08:44:38 TEST2 sshd[1232]: Connection closed by 192.168.0.66 port 52552 [preauth]
Mar 14 08:44:56 TEST2 sshd[1234]: Bad protocol version identification ‘GET / HTTP/1.1’ from 192.168.0.66 port 52554
Mar 14 08:55:21 TEST2 sshd[1236]: Bad protocol version identification ‘GET / HTTP/1.1’ from 192.168.0.66 port 52556
Mar 14 08:55:29 TEST2 sshd[1237]: Accepted password for root from 192.168.0.66 port 52558 ssh2
Mar 14 08:55:29 TEST2 sshd[1237]: pam_unix(sshd:session): session opened for user root by (uid=0)
Mar 14 08:55:31 TEST2 sshd[1237]: Received disconnect from 192.168.0.66 port 52558:11: disconnected by user
Mar 14 08:55:31 TEST2 sshd[1237]: Disconnected from 192.168.0.66 port 52558
Mar 14 08:55:31 TEST2 sshd[1237]: pam_unix(sshd:session): session closed for user root

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注