使用nginx实现ssh跳板机

使用nginx实现ssh跳板机

  • 基础环境
  • nginx安装
  • 检查NGINX支持STREAM模块
  • 配置NGINX转发TCP流量

基础环境

  1. 跳板机,IP:192.168.3.174
  2. 控制机01,IP:192.168.2.78
  3. 控制机02,IP:192.168.2.79

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

nginx安装

这里使用docker-compose安装

# 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
# 启动Nginx
docker-compose up -d

检查NGINX支持STREAM模块

# 进入容器
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 块同级)

# nginx.conf
worker_processes  1;

error_log  /etc/nginx/logs/error.log;

events {
    worker_connections  1024;
}

stream {
    include /etc/nginx/stream/*.conf;
}
# stream/ssh-01.conf
server {
    listen       2201;
    proxy_pass 192.168.2.78:22;
}
# stream/ssh-02.conf
server {
    listen       2202;
    proxy_pass 192.168.2.79:22;
}

重启Nginx即可
大功告成,开始享用吧
切忌,该方式不适合生产使用,存在安全风险!

你可能感兴趣的:(Shell,Docker,nginx,ssh,运维)