nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet

nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet

Nginx custom autoindex with XSLT

转载注明来源: 本文链接 来自osnosn的博客,写于 2019-04-11.

  • 另有个项目,用php写的单文件,显示文件目录。https://github.com/osnosn/autoindex

需要在浏览器页面中浏览服务器的目录。Apache2很容易配置,还能通过配置让目录浏览显示比较美观。
Nginx也很容易配置,Nginx提供了相应的ngx_http_autoindex_module 模块,该模块提供了我们想要的功能。
例如,要让/bt/目录能显示,nginx的配置这样写:

location ^~ /bt/ {
   autoindex on;
   autoindex_format html;
   #autoindex_localtime off;
   charset utf-8;
   include /etc/nginx/default.d/php71w-fpm.conf;
}

文件修改时间,可用参数autoindex_localtime设置为utc或当地时间。
效果如下图,可惜不太美观:
nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet_第1张图片

那再用上 ngx_http_xslt_module 模块。
长文件名溢出自动显示省略号。效果如下:
nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet_第2张图片

nginx配置如下:

location ^~ /bt/ {
   autoindex on;
   autoindex_format xml;
   xslt_stylesheet /data/www/html/autoindex.xslt cpath="$uri";
   include /etc/nginx/default.d/php71w-fpm.conf;
}

autoindex.xslt 这个文件有点大。
似乎ngx_http_xslt_module只支持stylesheet-1.0,不支持2.0。但支持libexslt,可以使用EXSLT函数。
xml中只提供UTC时间。不能通过nginx的配置修改。
时区修改在xslt文件中,TIMEDIFF变量。
改为PT0H时间不变,PT8H加8小时,-PT6H减6小时。
autoindex.xslt文件如下:




  

  
  
  
  <!DOCTYPE html>
 
 
   IndexOf <xsl:value-of select="$cpath"/>
   
   
   

<!--
   ========= config in nginx: ===============
    autoindex  on;
    autoindex_format  xml;
    xslt_stylesheet  /..path_to.../html/autoindex.xslt  cpath="$uri";
   ========= config in nginx: ===============
    modify variable "TIMEDIFF".
-->

   
   
 
 
   
Index of
:.end

Name Size Modified
../
B B KB MB GB
--

转载注明来源: 本文链接 来自osnosn的博客

你可能感兴趣的:(nginx的autoindex,目录浏览,配置和美化,美观的xslt_stylesheet)