NFS + Nginx搭建小型文件服务器(实现上传和下载)

一、需求分析:所有文件均存储在A服务器目录下,实现文件的上传下载。

       代码部署在C服务器,A服务器共享目录给C服务器,实现文件的上传;B服务器挂载A服务器目录,借助Nginx代理实现Http方式的文件下载。

二、网络流程图: 

            

三、具体实施:

1、实现上传

A服务器相关配置

①A服务器安装NFS服务

yum install nfs-utils
# 修改配置文件(此文件一般是空的)
vi /etc/exports

//支持配置多台服务器

/home/share/ 10.0.33.18(rw,sync,insecure,no_subtree_check,no_root_squash) #B服务器挂载端
/home/share/ 10.0.7.36(rw,sync,insecure,no_subtree_check,no_root_squash)  #C服务器,测试上传用的


######到此服务端配置完成

ro 只读

rw 读写

sync: 资料同步写入到内存与硬盘当中

async:资料会先暂存于内存当中,而非直接写入硬盘

all_squash 所有登录用户指定为nobody

no_all_squash 以当前登录的用户所设定的权限(默认设定)

anonuid 在使用all_squash时的选择,可以对登录的帐号指定为指定的用户ID帐号

anougid 在使用all_squash时的选择,可以对登录的帐号指定为指定的组ID帐号

root_squash root用户指定为nobodyno_root_squash:(允许远程用户以root帐号登录(比较不安全))不讲root用户及所属用户组映射为匿名用户或用户组,默认root是被映射为匿名用户的nfsnobody,所有即使开了rw写权限,客户机也使无法写入的,这个不映射为匿名用户,还保留原来的用户权限就可以读写了,因为一般都是用root用户登录的。

②C服务器测试上传,直接上代码

package com.docker.dockertest.test;

import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;

import java.io.*;

public class NfsTransferFile {

    private static final String NFS_IP = "10.0.33.18";
    private static final String NFS_DIR = "/home/share";

    public static void main(String[] args) {
        uploadFileToNfs();
//        downLoadFileFromNfs();
    }

    //上传本地文件到Nfs服务器指定目录
    public static void uploadFileToNfs() {
        String localDir = "/Users/Downloads/haha.txt";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //创建一个本地文件对象
            File localFile = new File(localDir);
            //获取本地文件的文件名,此名字用于在远程的Nfs服务器上指定目录创建同名文件
            String localFileName = localFile.getName();
            Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3);
            //创建远程服务器上Nfs文件对象
            Nfs3File NfsFile = new Nfs3File(nfs3, "/" + localFileName);
            //打开一个文件输入流
            inputStream = new BufferedInputStream(new FileInputStream(localFile));
            //打开一个远程Nfs文件输出流,将文件复制到的目的地
            outputStream = new BufferedOutputStream(new NfsFileOutputStream(NfsFile));

            //缓冲内存
            byte[] buffer = new byte[1024];
            while ((inputStream.read(buffer)) != -1) {
                outputStream.write(buffer);
            }
            System.out.println("文件上传完成!");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }


}

2、实现下载

①A服务器配置共享到B服务器

上面已配置A到B的共享

②B服务器挂载指定目录

yum -y install nfs-utils
systemctl start nfs-utils
systemctl enable nfs-utils
rpcinfo -p
mount  10.0.33.19:/home/share  /home/utry

③B服务器搭建Nginx代理

搭建Nginx的步骤详见:

Nginx深入学习(一篇搞定)

这边只需配置nginx访问静态文件,对外提供http下载方式。

 server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

# 访问路径带有share 的,一律跳转 xxx.xx.xx.xx/home/utry/xx.txt location
^~ /share { root /home/utry/; }
原文地址:https://www.cnblogs.com/dk1024/p/15557426.html