Ubuntu服务器上使用nginx搭建文件服务器解决多点应用部署文件的共享问题

最近做一个分布式项目遇到文件存储问题,用户上传的文件经过负载均衡后可能上传到不同过的服务器上面,导致有的服务器没有文件,这样经过负载均衡后可能访问文件失败。想到了两种解决方式,1是使用同步工具如rsync等将每个服务器新添加的数据同步到其他服务上面,2是通过建立文件服务器然后将每个服务上传过的文件直接上传到文件服务器上面,本次解决方式使用了第二种,具体步骤如下:

1.安装nginx文件服务器:

sudo apt install nginx

vim /etc/nginx/nginx.conf 修改配置文件如下 : 

user www-data; 
worker_processes auto; 
pid /run/nginx.pid; 
 
events { 
        worker_connections 768; 
        # multi_accept on; 
} 
 
http { 
    server { 
        client_max_body_size 4G; 
        listen  8089;   
        server_name   180.76.138.xxx;  
        root /home; 
            location / { 
                 autoindex on;  
                 autoindex_exact_size on; 
                 autoindex_localtime on; 
        } 
    } 
        ## 
        # Basic Settings 
        ## 
 
        sendfile on; 
        tcp_nopush on; 
        tcp_nodelay on; 
        keepalive_timeout 65; 
 
        # server_names_hash_bucket_size 64; 
        # server_name_in_redirect off; 
 
        include /etc/nginx/mime.types; 
        default_type application/octet-stream; 
 
        ## 
        # SSL Settings 
        ## 
 
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 
        ssl_prefer_server_ciphers on; 
 
        ## 
        # Logging Settings 
        ## 
 
        access_log /var/log/nginx/access.log; 
        error_log /var/log/nginx/error.log; 
 
        ## 
        # Gzip Settings 
        ## 
 
        gzip on; 
        gzip_disable "msie6"; 
 
        # gzip_vary on; 
        # gzip_proxied any; 
        # gzip_comp_level 6; 
        # gzip_buffers 16 8k; 
        # gzip_http_version 1.1; 
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 
 
        ## 
        # Virtual Host Configs 
        ## 
 
        include /etc/nginx/conf.d/*.conf; 
        include /etc/nginx/sites-enabled/*; 
} 
 
 
#mail { 
#       # See sample authentication script at: 
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript 
#  
#       # auth_http localhost/auth.php; 
#       # pop3_capabilities "TOP" "USER"; 
#       # imap_capabilities "IMAP4rev1" "UIDPLUS"; 
#  
#       server { 
#               listen     localhost:110; 
#               protocol   pop3; 
#               proxy      on; 
#       } 
#  
#       server { 
#               listen     localhost:143; 
#               protocol   imap; 
#               proxy      on; 
#       } 
#}

2.搭建ftp服务器,用来接收上传的文件:

sudo apt install vsftpd

vim /etc/vsftpd.conf 修改配置文件内容如下:

# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files. 
listen_ipv6=YES 
#
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#   
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# If enabled, vsftpd will display directory listings with the time
# in  your  local  time  zone.  The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
#chroot_local_user=YES
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES


# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# This option should be the name of a directory which is empty.  Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
pam_service_name=ftp
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO

#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES
                   

创建用户,设置密码,-d为登陆起始目录:

sudo useradd -d /home/static/blogimgs -s /bin/bash ftpuser

passwd ftpuser

在上面的文件中chroot_list_file=/etc/vsftpd.chroot_list是添加ftp用户的文件,vim /etc/vsftpd.chroot_list 将刚才建立的用户添加到里面:

3.使用FTPClient类上传文件到服务器的方法,本方法的两个参数一是文件,二是文件的重命名,方法最后返回远程服务保存文件的路径,将其持久化到数据库中。代码如下:

    public String uploadFile(File image, String remoteFileName) {

        String ip = "180.76.138.xxx";            //服务器IP地址
        String userName = "ftpuser";        //用于登陆服务器的用户名
        String passWord = "xxxxxx";            //登陆密码
        String remoteDirectoryPath = "/home/static/blogimgs";    //远程文件夹的绝对路径
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            ftpClient.login(userName, passWord);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //不加图片乱码
            ftpClient.changeWorkingDirectory(remoteDirectoryPath);
            InputStream is = new FileInputStream(image);
            ftpClient.storeFile(remoteFileName, is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "http://180.76.138.xxx:8089/static/blogimgs/" + remoteFileName;

    }

经测试远程上传的文件成功,可以看到服务器的文件,在前端页面中所有的URL即为远程服务器的文件路径。

Ubuntu服务器上使用nginx搭建文件服务器解决多点应用部署文件的共享问题_第1张图片

4.总结:

nginx搭建文件服务器还是很简单的,还有其他很多搭建文件服务器的方法,比如使用tomcat等等,在分布式系统中为了达到系统的高可用性,应该使用前文中提到的文件同步工具将远程文件服务器的文件同步到备份的服务器中。

 

你可能感兴趣的:(web项目,文件服务器)