前言
看到公司使用nginx做了個(gè)文件下載服務(wù)器,于是我也想搭建一個(gè),服務(wù)器是阿里云的,使用的是centos7系統(tǒng),并使用Nginx-Fancyindex-Theme樣式替換了nginx自帶的樣式。
先來個(gè)效果展示


下面講下具體的安裝方式
一、安裝nginx依賴庫
tips:如果已經(jīng)安裝過nginx直接看:二、ngx-fancyindex模塊安裝
nginx安裝首先要安裝幾個(gè)依賴庫(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模塊安裝
tips:如果已經(jīng)安裝過ngx-fancyindex模塊,直接看:三、下載服務(wù)器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,拷貝到服務(wù)器上,我放的路徑是/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/
三、下載服務(wù)器Nginx-Fancyindex-Theme主題配置
3.1 下載Nginx-Fancyindex-Theme主題
下載地址:https://github.com/Naereen/Nginx-Fancyindex-Theme
下載zip包(我用的是light主題),拷貝到服務(wù)器上,可以解壓完后拷貝上去,不然需要安裝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 {
# 防止瀏覽器預(yù)覽打開
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
add_header Content-Disposition attachment;
}
# 這里是代理的路徑,文件都要放在這里,Nginx-Fancyindex-Theme主題也要放在這個(gè)路徑下
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;
# 配置用戶訪問權(quán)限,見3.2用戶訪問權(quán)限配置
auth_basic "Restricted";
auth_basic_user_file "/usr/local/nginx/htpasswd";
}
}
js、css路徑問題
與html路徑問題一樣,由于我localtion配置的是/file,所以需要修改Nginx-Fancyindex-Theme-light下header.html和footer.html頁面的js和css路徑,增加/file,不然會出現(xiàn)404頁面。修改方式如下圖所示,這里就不全部截圖了。

3.2 用戶訪問權(quán)限配置
為了不讓別人訪問,可以增加用戶權(quán)限配置
首先添加用戶組
$ useradd -s /sbin/nologin -M nginx
生成用戶名和密碼,寫入htpasswd文件
這里使用了openssl生成密碼,其中username和password自己替換成自己的
$ 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