首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

nginx autoindex-索引浏览功能

2013-08-10 
nginx autoindex-目录浏览功能?Nginx默认是不允许列出整个目录的。如需此功能:__配置实例__?location ?/ ?{

nginx autoindex-目录浏览功能

?

Nginx默认是不允许列出整个目录的。

如需此功能:

__配置实例__

?

location ?/ ?{

: autoindex ?on;

}

指导

[#autoindex autoindex]

[#autoindex_exact_size autoindex_exact_size]

[#autoindex_localtime autoindex_localtime]

?

Template:Anchor

?

autoindex

syntax: autoindex [ on|off ]

?

default: autoindex off

?

context: http, server, location

?

激活/关闭自动索引

?

Template:Anchor

?

autoindex_exact_size

syntax: autoindex_exact_size [ on|off ]

?

default: autoindex_exact_size on

?

context: http, server, location

?

设定索引时文件大小的单位(B,KB, MB 或 GB) Template:Anchor

?

autoindex_localtime

syntax: autoindex_localtime [ on|off ]

?

default: autoindex_localtime off

?

context: http, server, location

?

开启以本地时间来显示文件时间的功能。默认为关(GMT时间)

?

================================

?

?

如: 让/var/www/soft 这个目录在浏览器中完成列出.

一、设置目录浏览

1、打开/usr/local/nginx/conf/nginx.conf,找到WebServer配置处,加入以下内容:

location /soft/ {
root /var/www/; ?此处为soft的上一级目录
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}

2、保存退出,重启nginx服务即可。

?

[root@localhost Soft]#ps aux | grep -v grep | grep nginx | awk ‘{print $2}’ | xargs kill -9 ? ?#结束进程

[root@localhost Soft]#nginx ?#启动进程

访问你的站的:http://loalhost/soft,就可以列出目录了。

但是这样的话,别人也很容易看到你目录的内容,下面我们像Apache那样为目录加个访问验证。

二、设置访问验证

1、创建类htpasswd文件

[root@localhost Soft]#wget -c http://jafee.net/Soft/InstallPack/htpasswd.sh

[root@localhost Soft]#bash htpasswd.sh

提示输入用户名、密码、及认证文件名,脚本会自动生成认证文件,这里默认路径是保存在了/usr/local/nginx/conf 下,如果你的nginx目录不是这里,可以修改htpasswd.sh替换你的nginx目录。

我这里是:/usr/local/nginx/conf/test.conf ?#记下此路径

2、为Nginx添加auth认证配置

location ^~ /soft/
{
auth_basic “MyPath Authorized”;
auth_basic_user_file ?/usr/local/nginx/conf/test.conf; ? ?#这里写前面脚本返回的文件路径;
}

#”MyPath Authorized”为提示信息,可以自行修改。

3、修改好配置后,重启nginx,访问http://localhost/soft/ 就会提示输入用户名和密码,认证成功后,即可列出目录。

4、需要注意的是,加上认证之后该目录下的php文件将不会被解析,会让你下载,如果要使其能够解析php可以将上面的配置改为:

location ^~ /soft/?{
location ~ .*\.(php|php5)?$?{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
auth_basic “Authorized users only”;
auth_basic_user_file?/usr/local/nginx/conf/test.conf ;
}

?

?

来自:http://wiki.nginx.org/NginxNgxFancyIndex ?和 ?http://www.huzs.net/?p=688

?

?========================================

1.在配置文件中添加验证语句:
? 添加auth_basic "auth user" //名字任起
????? auth_basic_user_file? /etc/nginx/htpasswd.conf?? //目录自己定
2.创建上面所定的目录/etc/nginx/htpasswd.conf
? 利用apache所带的密码生成工具htpasswd(密码必须是crypt加密的)
? /usr/bin/htpasswd -nb user passwd
? or
? /usr/bin/htpasswd? -c /etc/nginx/htpasswd.conf? user
3.修改配置文件??

  • server?{
  • ????????listen 80;
  • ????????server_name localhost;
  • # auth_basic?"auth user";
  • ?# auth_basic_user_file?/etc/nginx/htpasswd.conf;
  • ????????#charset koi8-r;
  • ????????#access_log logs/host.access.log main;
  • ????????location?/?{
  • ????????????root?/mnt/?;
  • # auth_basic?"auth user";
  • # auth_basic_user_file?/etc/nginx/htpasswd.conf;
  • ????????????index index.php index.html index.htm;
  • location ~ ^/admin/.* {

    ????????location ~ \.php$ {

    ????????????fastcgi_pass? 127.0.0.1:9000;

    ????????????fastcgi_index index.php;

    ????????????include fastcgi_params;

    ????????}

    ????????auth_basic "auth";

    ????????auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass;

    ????}

  • ????????}

    就是location ~ ^/admin/.* {…}?保护admin目录下的所有文件。如果你只设了/admin/?那么直接输入/admin/index.php还是可以访问并且运行的。

    ^/admin/.*?意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

    ?

  • 热点排行