您的位置:首页 > 运维架构 > Nginx

Nginx配置——用户认证

2017-01-01 20:07 417 查看
## 生成密码文件
[root@localhost ~]# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd wyy
New password:
Re-type new password:
Adding password for user wyy
解释说明:
借助于apache的工具htpasswd实现生成用户,密码
-c 新建一个文件指定目录(...nginx/conf)
## 使用验证
[root@localhost ~]# vim /usr/local/nginx/conf/vhosts/test.conf
location ~ .*admin\.php$ { # 所匹配的需要身份认证,这也可以是一个目录(目录就不用加php解析了)
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
解释说明:
红色字体不加的话,它就会直接下载了.php的那个文件,说明了.php并没有真正地解析。
## 检查语法并重启
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t
[root@localhost vhosts]# /etc/init.d/nginx reload

## 测试
[root@localhost vhosts]# curl -x127.0.0.1:80 www.test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
解释说明:
401表示输入用户名密码认证
[root@localhost vhosts]# curl -uwyy:123456 -x127.0.0.1:80 www.test.com/admin.php
解释说明:
显示出来的如果是php代码块,就说明php没有解析出来,显示的如果是html标签就是解析出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx用户认证