Nginx autoindex & FancyIndex

Nginx 可以方便的配置用作文件列出,提供下载服务。

server {
    listen 80;
    server_name files.xbits.net;
    autoindex on;
    charset utf-8;
    autoindex_exact_size off;
    autoindex_localtime on;
    #add_after_body /autoindex.html;
    autoindex_format html;
    root /var/www/files/;
    # auth_basic "password required!";
    # auth_basic_user_file conf.d/passwd;
}

Note: 原生的ngx_http_autoindex_module就能实现此功能,但是唯一缺点就是丑,且对于长文件名不友好,在web界面上只能显示默认50个字符的filename,当然可以“硬”修改源码,使其显示更长的filename,但是页面不能自动适配,很戳。。。。

修改 src/http/modules/ngx_http_autoindex_module.c:

Change these lines:

#define NGX_HTTP_AUTOINDEX_PREALLOCATE 50
#define NGX_HTTP_AUTOINDEX_NAME_LEN 50

#define NGX_HTTP_AUTOINDEX_PREALLOCATE 100
#define NGX_HTTP_AUTOINDEX_NAME_LEN 100

cd /tmp
wget https://github.com/aperezdc/ngx-fancyindex/archive/master.zip
unzip master.zip
# 重新编译,追加模块
./configure .... --add-module=/tmp/ngx-fancyindex-master
server {
    listen 4430 ssl;
    server_name files.xbits.net;
    charset utf-8,gbk;
    fancyindex on;
    fancyindex_exact_size off;
    fancyindex_localtime on;
    fancyindex_default_sort date;
    fancyindex_directories_first on;
    fancyindex_name_length 150;
    fancyindex_ignore autoindex.html;

    # ssl
    ssl_certificate /etc/letsencrypt/live/xbits.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xbits.net/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;
    root /var/www/files/;
    # auth_basic "password required!";
    # auth_basic_user_file conf.d/passwd;
}
  • linux/nginx/auto_index_fancyindex.txt
  • 最后更改: 2019/04/16 18:31
  • (外部编辑)