1. 程式人生 > 其它 >Nginx反向代理FTP(公網IP代理到內網IP)

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等安全設定複雜。
一入運維深似海, 從此背鍋不是我。