首页 > 技术文章 > nginx文件目录权限设置

shijingjing07 2016-12-30 17:43 原文


1.有时我们web服务器上的某个文件夹只允许特定人员访问,这时我们需要在nginx配置文件中配置该文件夹的访问权限。

2.生成用户名单
在nginx中我们使用htpasswd来生成用户名单
下载这个python文件:http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py (nginx wiki里推荐的)
运行示例:

chmod 777 htpasswd.py
./htpasswd.py -c -b htpasswd username password

#-c为生成文件 htpasswd为文件名

nginx 的 http auth basic 的密码是用 crypt(3) 加密的
我们把生成的htpasswd文件放到/etc/nginx目录中,修改权限chmod 400 htpasswd来保护一下该文件。

3.修改nginx配置文件

server {
server_name www.test.com;
root /usr/share/nginx/html;
location /devdoc {
autoindex on;#显示文件列表
index index.html index.htm;#默认首页
charset utf-8;#编码
auth_basic "Restricted";#访问权限类型
auth_basic_user_file /etc/nginx/htpasswd;#用户名单
}
}

重启nginx即可。访问网站www.test.com/devdoc,需要输入我们设置的用户名密码登录才能访问文件。如下图所示:

推荐阅读