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为文件名
printf "fss:$(openssl passwd -crypt 123456)\n" >> /data/auth/users
nginx 的 http auth basic 的密码是用 crypt(3) 加密的
我们把生成的htpasswd文件放到/etc/nginx目录中,修改权限chmod 400 htpasswd来保护一下该文件。
切记,htpasswd文件不能放在/root目录下,否则会一直报错13: Permission denied,浏览器会提示500错误
3.修改nginx配置文件
针对特定目录加密
location ~ ^/shell/.*
{
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "Server Auth";
auth_basic_user_file /usr/local/nginx/conf/vhost/htpasswd;
}
重启nginx即可。访问网站www.test.com/devdoc,需要输入我们设置的用户名密码登录才能访问文件。
基于整个网站的认证,auth_basic要在php解释之前。
server {
listen 80;
server_name www.wkii.org akii.org;
root /www/akii;
index index.html index.htm index.php;
auth_basic "input you user name and password";
auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd;
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
access_log /logs/akii.org_access.log main;
针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。
server {
listen 80;
server_name www.wkii.org akii.org;
root /www/akii;
index index.html index.htm index.php;
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 ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
access_log /logs/akii.org_access.log main;
}
wget下载ftp或http服务器上需要用户名密码验证的文件
用户自己搭建的ftp服务器或设置了用户名密码验证的http服务器,都是需要验证后才能下载,没法直接通过wget 下载,必须加上验证参数才行。
FTP下:
wget --ftp-user=帐号 --ftp-password=密码 ftp://ip或域名/目录/文件名
HTTP下:
wget --http-user=帐号 --http-password=密码 http://ip或域名/目录/文件名
如果机器上装了lftp,也是一个不粗的选择
lftp ip -p port -u user,password登陆ftp服务器
然后get 文件名