English |
  • 美国VPS主机推荐
  • |
  • 代购服务
  • |
  • 10美元以下VPS
  • |
  • VPS新手指南/教程
  • |
  • 留言板
  • |
  • 关于
  • | 登录 |

    Nginx打开目录浏览功能(autoindex)

    2009年09月1日 上午 | 作者:VPS侦探

    Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件或你要启用目录浏览虚拟主机的配置文件,在server或location 段里添加上autoindex on;来启用目录流量,下面会分情况进行说明。

    另外Nginx的目录流量有两个比较有用的参数,可以根据自己的需求添加:

    autoindex_exact_size off;
    默认为on,显示出文件的确切大小,单位是bytes。
    改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

    autoindex_localtime on;
    默认为off,显示的文件时间为GMT时间。
    改为on后,显示的文件时间为文件的服务器时间

    1、整个虚拟主机开启目录流量

    在server段添加

    location / {
    autoindex on;
    autoindex_localtime on; #之类的参数写这里
    }

    2、单独目录开启目录流量
    2.1:直接二级目录开启目录流量
    location /down/ {
    autoindex on;
    }

    2.2:虚拟目录开启目录流量
    location /down/ {
    alias /home/wwwroot/lnmp/test/;
    autoindex on;
    }

    详细参照:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

    需要注意root和alias的区别:

    alias 设置的目录是准确的,可以理解为linux的 ln命令创建软连接,location就是软连接的名字。如上面2.2例子访问 http://域名/down/vpser.txt 是直接访问的/home/wwwroot/lnmp/test/下面的vpser.txt文件。

    root 设置的目录是根目录,locatoin里所指定名称的目录,必须在root设定下的目录有相同名字的目录。如果将上面2.2例子里的alias改成root 访问 http://域名/down/vpser.txt 是直接访问的的/home/wwwroot/lnmp/test/down/ 目录下的vpser.txt文件。

    需要注意的是alias目录必须要以 / 结尾且alias只能在location中使用。

    如果想希望做出漂亮的目录列表,支持header,footer则可以安装三方插件:
    http://wiki.nginx.org/NginxNgxFancyIndex

    重启nginx,使其生效。

    >>转载请注明出处:VPS侦探 本文链接地址:https://www.vpser.net/build/nginx-autoindex.html
    VPS侦探推荐:
    遨游主机VultrLinode搬瓦工LOCVPSKVMLAHOSTKVMHostXen80VPS美国VPS主机,国内推荐腾讯云阿里云
    欢迎加入VPS侦探论坛交流:https://bbs.vpser.net

    发表评论

    *必填

    *必填 (不会被公开)

    评论(5条评论)

    1. VPS侦探说道:

      @taokai, root /home/wwwroot/default/dl;替换为alias /home/wwwroot/default/dl/;

    2. taokai说道:

      重启了还是不行!

    3. taokai说道:

      user www www;

      worker_processes auto;

      error_log /home/wwwlogs/nginx_error.log crit;

      pid /usr/local/nginx/logs/nginx.pid;

      #Specifies the value for maximum file descriptors that can be opened by this process.
      worker_rlimit_nofile 51200;

      events
      {
      use epoll;
      worker_connections 51200;
      multi_accept on;
      }

      http
      {
      include mime.types;
      default_type application/octet-stream;

      server_names_hash_bucket_size 128;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;
      client_max_body_size 50m;

      sendfile on;
      tcp_nopush on;

      keepalive_timeout 60;

      tcp_nodelay on;

      fastcgi_connect_timeout 300;
      fastcgi_send_timeout 300;
      fastcgi_read_timeout 300;
      fastcgi_buffer_size 64k;
      fastcgi_buffers 4 64k;
      fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 256k;

      gzip on;
      gzip_min_length 1k;
      gzip_buffers 4 16k;
      gzip_http_version 1.1;
      gzip_comp_level 2;
      gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
      gzip_vary on;
      gzip_proxied expired no-cache no-store private auth;
      gzip_disable "MSIE [1-6]\.";

      #limit_conn_zone $binary_remote_addr zone=perip:10m;
      ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

      server_tokens off;
      #log format
      log_format access '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" $http_x_forwarded_for';
      access_log off;

      server
      {
      listen 81 default_server;
      #listen [::]:81 default_server ipv6only=on;
      server_name http://www.lnmp.org;
      index index.html index.htm index.php;
      root /home/wwwroot/default;

      location /images {
      root /home/wwwroot/default/dl;
      autoindex on;
      }

      #error_page 404 /404.html;
      include enable-php.conf;

      location /nginx_status
      {
      stub_status on;
      access_log off;
      }

      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
      {
      expires 30d;
      }

      location ~ .*\.(js|css)?$
      {
      expires 12h;
      }

      location ~ /\.
      {
      deny all;
      }

      access_log /home/wwwlogs/access.log access;
      }
      include vhost/*.conf;
      }

    4. VPSer说道:

      @Dianso, 把你的配置文件发上来看一下

    5. Dianso说道:

      还是没成功饿