一、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等安全設置復雜。