Nginx反向代理FTP(公网IP代理到内网IP)

一、Nginx反向代理通常只做七层负载均衡,但现在我集群需要FTP做文件系统,上传下载web文件,所以要做TCP “四层代理”,Nginx要是想实现四层负载均衡,就需要添加“--with-stream” 模块。

二、环境软件版本准备

系统:CentOS Linux release 7.2.1511 (Core)

软件:Nginx-1.18

 

 

 

三、安装编译环境

yum install -y pcre-devel zlib zlib-devel gcc gcc-c++ make 

四、查看nginx版本和已安装的模块,没有--with-stream

nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --http-proxy-temp-path=/usr/local/nginx/proxy --http-fastcgi-temp-path=/usr/local/nginx/fcgi --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-path=/usr/local/nginx/scgi --with-pcre --with-http_ssl_module

五、我集群已经做了nginx代理,所以只需要平滑添加“--with-stream”模块,切换到nginx-1.18源码目录操作

cd /usr/local/nginx/sbin/              #先备份nginx启动文件,关闭nignx服务
[root@RS2 sbin]# cp nginx nginx.old
[root@RS2 sbin]#systemctl stop nginx
[root@RS2 sbin]#cd /root/app/nginx-1.18.0
[root@RS2 nginx-1.18.0]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--with-pcre \
--with-http_ssl_module \
--with-stream
[root@RS2 nginx-1.18.0]#make
[root@RS2 nginx-1.18.0]cp ./objs/nginx /usr/local/nginx/sbin/nginx

六、vsftpd服务端配置,安装过程就不放出来了,用的yum。ftp用的虚拟用户连接

[root@serverA vsftpd]# cat vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
listen_ipv6=NO
guest_enable=YES
guest_username=virtftp
pasv_enable=YES
pasv_promiscuous=YES
#pasv_address=192.168.1.13
pasv_address=193.168.0.128
pasv_min_port=8000
pasv_max_port=8002
max_clients=50
max_per_ip=3
allow_writeable_chroot=YES

pam_service_name=vsftpd.vu
user_config_dir=/etc/vsftpd/ftp_user
userlist_enable=YES
tcp_wrappers=YES

pasv_promiscuous关闭存在安全隐患;
在实际组网情况下,Nginx转发时可设置保留源IP信息,但是客户端与服务端无法直接访问,因此只能放弃保留源IP信息。

七、Nginx_proxy配置,stream模块是四层负载均衡,是和http模块同一级别,刚开始我做不知道,错把stream放在http模块里,导致一直如下报错

[root@RS2 conf.d]# nginx -t
nginx: [emerg] "stream" directive is not allowed here in /usr/local/nginx/conf.d/ftp.conf:1
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

 

八、知道原因后,我们把stream写在,nginx.conf中便可

[root@RS2 nginx]# vim /usr/local/nginx/conf/nginx.conf
stream {
upstream ftp {
    server 192.168.1.10:21;
    }
server {
    listen 1100;
    #失败重试
    proxy_next_upstream on;
    proxy_next_upstream_timeout 0;
    proxy_next_upstream_tries 0;
    #超时配置
    proxy_connect_timeout 1s;                                                               
    proxy_timeout 10m;                                                                      
    #限速配置                                                                               
    proxy_upload_rate 10240k;                                                               
    proxy_download_rate 20480k;                                                             
    #上游服务器                                                                             
    proxy_pass ftp;                                                                         
   } 
upstream ftp_1 {
    server 192.168.1.10:8000;
   }
server {
    listen 8000;
    proxy_pass ftp_1;
   }
upstream ftp_2 {
    server 192.168.1.10:8001;
   }
server {
    listen 8001;
    proxy_pass ftp_2;
   }         
upstream ftp_3 {
    server 192.168.1.10:8002;
   }
server {
    listen 8002;
    proxy_pass ftp_3;
   }      

 这里在ftp_1,ftp_2,ftp_3就不配置数据连接的具体控制(诸如限速)

 九、把服务都重启一遍不报错,就开始测试连接,我这里用的物理机windows10访问,和用web3主机测试连接

 1.windows10

 

 

 

 

 

 

 2.web3,这要连接,还需要在web3添加一张网卡,是和nginx_proxy同外网段的IP地址,这里就把他当成外网主机,模拟外网主机连接ftp服务器

[root@serverC ~]# ftp 193.168.0.128 1100
Connected to 193.168.0.128 (193.168.0.128).
220 (vsFTPd 3.0.2)
Name (193.168.0.128:root): ftpadmin
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (193,168,0,128,31,66).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Oct 22 08:10 22
-rw-r--r--    1 0        0              25 Oct 06 02:54 index.html
-rw-r--r--    1 0        0              45 Oct 06 03:07 index.php
-rwxr-xr-x    1 0        0             400 Oct 17 08:03 inotifyrsync.sh
-rw-------    1 0        0           33642 Oct 23 06:00 nohup.out
drwxr-xr-x   12 0        0            4096 Oct 06 03:07 phpMyAdmin
drwxr-xr-x    5 1006     1006         4096 Oct 22 21:41 wordpress
226 Directory send OK.
ftp> 

 

 十、这样做Nginx可以反向代理FTP,但也有弊端

      存在以下缺点:

  • 控制连接和数据连接是否来自同一连接无法验证,存在安全隐患;
  • FTP被动模式下,数据端口范围较宽时,Nginx添加配置比较麻烦;
  • Nginx代理机需要开放较多端口,诸如iptables,firewall等安全设置复杂。

 

posted @ 2021-11-09 11:15  道观猫  阅读(4919)  评论(0编辑  收藏  举报