Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active May 13, 2024 06:25
Show Gist options
  • Save gilangvperdana/9b5f6179a22935cdfd0655a290b2d2ba to your computer and use it in GitHub Desktop.
Save gilangvperdana/9b5f6179a22935cdfd0655a290b2d2ba to your computer and use it in GitHub Desktop.
Nginx Reverse Proxy for SSH

Nginx Reverse Proxy for SSH

Use Case

  • 10.10.10.50 is Target VM will to SSH
  • We will expose this to localhost:32

Configuration

  • Make upstream conf :
nano /etc/nginx/conf.d/stream.conf.ssh
stream {
  upstream ssh { 
    server        10.10.10.50:22; 
  } 
  server { 
    listen       32; 
    proxy_pass  ssh; 
  } 
}
  • Include conf to nginx.conf for Single SSH :
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

include /etc/nginx/conf.d/*.conf.ssh;
  • Include conf to nginx.conf for Multiple SSH :
stream {
  upstream vpn { 
    server        192.168.3.22:22; 
  } 
  server { 
    listen       30; 
    proxy_pass  192.168.3.22:22; 
  }

  upstream radius {
    server        192.168.3.221:22;
  }
  server {
    listen       31;
    proxy_pass  192.168.3.221:22;
  }
 
  upstream dsnmasq {
    server        192.168.3.157:22;
  }
  server {
    listen       32;
    proxy_pass  192.168.3.157:22;
  }

}
  • Verification
nginx -t
  • Reload
service nginx reload
  • Now you can access on localhost:32

Dont Have stream package on nginx

sudo apt-get update -y
sudo apt-get install -y libnginx-mod-stream
  • Make an module-enabled
nano modules-enabled
load_module modules/ngx_stream_module.so;
service nginx reload

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment