Nginx 默认是不允许列出整个目录的。
如需此功能,打开 nginx.conf 文件或你要启用目录浏览虚拟主机的配置文件,在 location server 或 http 段中加入
autoindex on;
另外两个参数最好也加上去:
autoindex_exact_size off;
默认为 on,显示出文件的确切大小,单位是 bytes。
改为 off 后,显示出文件的大概大小,单位是 KB 或者 MB 或者 GB
autoindex_localtime on;
默认为 off ,显示的文件时间为 GMT 时间。
改为 on 后,显示的文件时间为文件的服务器时间。
在 nginx.conf 文件 中 server 段添加
location / {
autoindex on;
autoindex_localtime on; #之类的参数写这里
}
location /down/ {
autoindex on;
}
location /down/ {
alias /home/wwwroot/test/;
autoindex on;
}
location ~ \.(ini|conf|txt)$ {
deny all;
}
#禁止访问目录
location ^~ /test/ {
deny all;
}
#禁止访问目录下文件
location ^~ /test {
deny all;
}
# 禁止访问某个目录下的 php 后缀文件
location /directory {
location ~ .*\.(php)?$ {
deny all;
}
}
# 禁止访问多个目录下的 php 后缀文件
location ~* ^/(directory1|directory2)/.*\.(php)${
deny all;
}
= 表示精确匹配^~ 表示 uri 以某个字符串开头~ 正则匹配(区分大小写)~* 正则匹配(不区分大小写) !和!*分别为区分大小写不匹配及不区分大小写不匹配的正则/ 任何请求都会匹配= > ^~ > /location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
add_header Content-Disposition attachment;
}
到此这篇关于Nginx 禁止直接访问目录或文件的方法的文章就介绍到这了,更多相关nginx 禁止直接访问目录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
