nginx re-route all data based on port (nginx proxy)
Asked Answered
H

1

2

I'm still new to nginx and I want to accomplish this.

I have two servers (server1 and server2), with an sftp server (bitvise) on server1. And on server2 I have an nginx docker container running.

I want to configure nginx so when trafic comes to server2 (the one with nginx) on port 22 , it get redirected to server1, where my sftp sever is present.

I have an dns "transfer.test.com" mapped to my server2 public IP (tested).

This is the configuration I have added to nginx conf file.

server {
 listen 22;
 server_name transfer.test.com;
 return 301 https://google.com;


 location / {
   set $sftp server1-private-ip:22;
   proxy_pass  $sftp;
  }
} 

server1-private-ip is the private IP of server1 (the one with sftp).

but till now its not working. I can connect to sftp using filezile using the private IP of server1 BUT I can't connect to sftp using filezila using the private IP of server2, means the trafic is not getting redirected.

Thank you for the help.

Hathaway answered 17/9, 2021 at 9:25 Comment(0)
A
6

If you want to use nginx as a proxy to non-HTTP protocols like SSH or SFTP, you should define your server in a stream context rather than http one. Typical main configuration file (usually /etc/nginx/nginx.conf) looks like

user              <username>;
worker_processes  <number>;
...

events {
    worker_connections  <number>;
}

http {
    include       /etc/nginx/mime.types;
    ... # other global http directives here
    include       /etc/nginx/conf.d/*.conf;
}

As you can see, configuration files for individual servers (or server groups) are being included within the http context. You should add stream block to your main configuration file:

user              <username>;
worker_processes  <number>;
...

events {
    worker_connections  <number>;
}

http {
    ...
}

stream {
    server {
        listen      22;
        proxy_pass  <server1_private_ip>:22;
    }
}

Directives like server_name or location are meaningless in the server blocks defined under the stream context. Please note that for using above configuration nginx should be compliled with ngx_stream_core_module and ngx_stream_proxy_module modules.

Attention answered 17/9, 2021 at 9:57 Comment(5)
I added this stream { server { listen 22; set $sftp jz-nginx:80; proxy_pass $sftp; } } to my nginx.conf file, under http{} but still when I curl transfer.test.com i don't get the nginx welcome page, jz-nginx:80 where jz-nginx is a local nginx docker containerHathaway
and I even tried stream { server { listen 22; proxy_pass <server1_private_ip>:22; } } but still when I try to connect to server2_private_ip:22 from filezilla it does not work Thank you for your helpHathaway
What nginx -t command says?Attention
after adding this stream { server { listen 22; set $sftp <server1-private-ip>:22; proxy_pass $sftp; } } the nginx -t says : nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successfulHathaway
nginx configuration file seems to be ok. Sorry, I don't know how to help further. I'm not very familiar with docker networking.Attention

© 2022 - 2024 — McMap. All rights reserved.