使用nginx实现ssh跳板机方式

基础环境

  • 跳板机,IP:192.168.3.174

  • 控制机01,IP:192.168.2.78

  • 控制机02,IP:192.168.2.79

控制机01、控制机02只允许跳板机访问。

nginx安装

这里使用docker-compose安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# docker-compose.yml
version: '3.7'
services:
  nginx:
    restart: always
    image: registry.cn-hangzhou.aliyuncs.com/eddy/nginx:1.27
    privileged: true
    environment:
      TZ: Asia/Shanghai
    ports:
      - 8001:8001
      - 2201:2201
      - 2202:2202
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./http:/etc/nginx/http
      - ./stream:/etc/nginx/stream
      - ./logs:/etc/nginx/logs
1
2
# 启动Nginx
docker-compose up -d

检查NGINX支持STREAM模块

1
2
3
4
5
# 进入容器
docker exec -it [容器名] /bin/sh
# 如果输出 with-stream,说明已支持。
# 若未输出,需重新编译Nginx并添加 --with-stream 参数。
nginx -V 2>&1 | grep -o with-stream

配置NGINX转发TCP流量

在Nginx配置文件(如 /etc/nginx/nginx.conf)中添加 stream 块(与 http 块同级)

1
2
3
4
5
6
7
8
9
10
11
12
# nginx.conf
worker_processes  1;
 
error_log  /etc/nginx/logs/error.log;
 
events {
    worker_connections  1024;
}
 
stream {
    include /etc/nginx/stream/*.conf;
}
1
2
3
4
5
# stream/ssh-01.conf
server {
    listen       2201;
    proxy_pass 192.168.2.78:22;
}
1
2
3
4
5
# stream/ssh-02.conf
server {
    listen       2202;
    proxy_pass 192.168.2.79:22;
}

重启Nginx即可

大功告成,开始享用吧

切忌,该方式不适合生产使用,存在安全风险!


您可以还会对下面的文章感兴趣:

暂无相关文章