美文网首页Nginx高端成长之路
Nginx文件下载服务器Nginx-Fancyindex-The

Nginx文件下载服务器Nginx-Fancyindex-The

作者: 上帝与我同幻想 | 来源:发表于2018-03-09 22:41 被阅读32次

    前言

    看到公司使用nginx做了个文件下载服务器,于是我也想搭建一个,服务器是阿里云的,使用的是centos7系统,并使用Nginx-Fancyindex-Theme样式替换了nginx自带的样式。

    先来个效果展示
    登录.png 文件列表.png

    如果已经安装过nginx直接跳到:二、ngx-fancyindex模块安装

    一、安装nginx依赖库

    nginx安装首先要安装几个依赖库(gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)

    1.1 安装gcc gcc-c++
    $ yum install -y gcc gcc-c++
    
    1.2 安装PCRE库
    $ cd /usr/local/
    $ wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
    $ tar -zxvf pcre-8.36.tar.gz
    $ cd pcre-8.36
    $ ./configure
    $ make && make install
    
    1.3 安装SSL库
    $ cd /usr/local/
    $ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
    $ tar -zxvf openssl-1.0.1j.tar.gz
    $ cd openssl-1.0.1j
    $ ./config
    $ make && make install
    
    1.4 安装zlib库存
    $ cd /usr/local/
    $ wget http://zlib.net/zlib-1.2.11.tar.gz
    $ tar -zxvf zlib-1.2.11.tar.gz
    $ ./configure
    $ make && make install
    

    二、ngx-fancyindex模块安装

    如果已经安装过ngx-fancyindex模块的,直接看:三、下载服务器Nginx-Fancyindex-Theme主题配置。
    注意:Nginx-Fancyindex-Theme主题配置需要使用此模块。仅安装ngx-fancyindex模块也可以美化nginx的目录浏览功能,但是还是不够好看

    2.1 下载nginx
    $ cd /usr/local/
    $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
    $ tar -zxvf nginx-1.8.0.tar.gz
    
    2.2 如果还没有安装过nginx,则需要先安装此模块

    https://github.com/aperezdc/ngx-fancyindex 下载ngx-fancyindex,拷贝到服务器上,我放的路径是/usr/local

    $ unzip ngx-fancyindex-master.zip
    $ cd nginx-1.8.0/
    $ ./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --add-module=../ngx-fancyindex-master
    $ make && make install
    
    --add-module= 后面跟的是ngx-fancyindex-master解压出来的路径
    
    2.3 如果安装过nginx,需要重新编译

    找到下载解压出来的nginx源文件目录

    $ cd nginx-1.8.0/
    $ ./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --add-module=../ngx-fancyindex-master
    $ make   # 注意不要make install
    $ cp /objs/nginx  /usr/local/nginx/sbin/
    

    三、下载服务器Nginx-Fancyindex-Theme主题配置

    3.1 下载Nginx-Fancyindex-Theme主题

    下载地址:https://github.com/Naereen/Nginx-Fancyindex-Theme
    下载zip包(我用的是light主题),拷贝到服务器上,可以解压完后拷贝上去,不然需要安装unzip包来解压。
    注:主题的路径要和配置文件中代理的路径相同,我这里代理的是/usr/data,所以主题要放在/usr/data

    nginx配置文件
    server {
        listen       70;
        charset      utf-8;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /file {
            # 防止浏览器预览打开
            if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
                add_header Content-Disposition attachment;
            }
            # 这里是代理的路径,文件都要放在这里,Nginx-Fancyindex-Theme主题也要放在这个路径下
            alias /usr/data;  
            fancyindex on;
            fancyindex_exact_size off;
            fancyindex_localtime on;
    
            # 由于我配置了访问路径是http://ip:70/file,主题的路径前需要加上/file,
            # 如果配置的是location /,则路径为/Nginx-Fancyindex-Theme-light/header.html
            fancyindex_header "/file/Nginx-Fancyindex-Theme-light/header.html";
    
            # 由于我配置了访问路径是http://ip:70/file,主题的路径前需要加上/file
            # 如果配置的是location /,则路径为/Nginx-Fancyindex-Theme-light/footer.html
            fancyindex_footer "/file/Nginx-Fancyindex-Theme-light/footer.html";
    
            fancyindex_ignore "examplefile.html";
            fancyindex_ignore "Nginx-Fancyindex-Theme-light";
            fancyindex_name_length 255;
    
            # 配置用户访问权限,见3.2用户访问权限配置
            auth_basic        "Restricted";
            auth_basic_user_file "/usr/local/nginx/htpasswd";
        }
    }
    
    js、css路径问题

    与html路径问题一样,由于我localtion配置的是/file,所以需要修改Nginx-Fancyindex-Theme-light下header.htmlfooter.html页面的js和css路径,增加/file,不然会出现404页面。修改方式如下图所示,这里就不全部截图了。

    图1
    3.2 用户访问权限配置

    为了不让别人访问,可以增加用户权限配置

    首先添加用户组

    $ useradd -s /sbin/nologin -M nginx

    生成用户名和密码,写入htpasswd文件

    这里使用了openssl生成密码,其中usernamepassword自己替换成自己的
    $ printf "ttlsa:$(openssl passwd -username password)\n" >>/usr/local/nginx/htpasswd
    cat可以查看
    $ cat /usr/local/nginx/htpasswd

    3.3 启动nginx
    $ cd /usr/local/nginx/sbin
    $ ./nginx
    

    重启命令

    $ cd /usr/local/nginx/sbin
    $ ./nginx -s reload
    
    以上就是全部配置过程,END

    相关文章

      网友评论

        本文标题:Nginx文件下载服务器Nginx-Fancyindex-The

        本文链接:https://www.haomeiwen.com/subject/mwpmfftx.html