下载服务器Nginx-Fancyindex-Theme样式搭建
young / / nginx / 阅读量

安装nginx依赖库

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

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

安装gcc gcc-c++
$ yum install -y gcc gcc-c++
安装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
安装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
安装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:如果已经安装过ngx-fancyindex模块,直接看:三、下载服务器Nginx-Fancyindex-Theme主题配置。

注意:Nginx-Fancyindex-Theme主题配置需要使用此模块。仅安装ngx-fancyindex模块也可以美化nginx的目录浏览功能,但是还是不够好看

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

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

$ unzip ngx-fancyindex-master.zip
$ cd nginx-1.20.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解压出来的路径
如果安装过nginx,需要重新编译

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

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

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

下载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";
    }
}
用户访问权限配置

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

首先添加用户组

$ 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
启动nginx
$ cd /usr/local/nginx/sbin
$ ./nginx

重启命令

$ cd /usr/local/nginx/sbin
$ ./nginx -s reload

以上就是全部配置过程,END

原文:https://www.jianshu.com/p/5b2914c19253

支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章