nginx 用户认证
nginx 用户认证
- vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
- yum install -y httpd #如果没有安装就安装,为了使用他的htpasswd
- /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xujb
- /usr/local/nginx/sbin/nginx -t && -s reload //测试配置并重新加载
返回错误吗500?
查看nginx_error.log 看到是查询htpasswd文件失败,查看文件所属看到不是nobody用户,使用chown nobody:nobody htpasswd,然后访问正常
- mkdir /data/wwwroot/test.com
- echo “test.com”>/data/wwwroot/test.com/index.html
- curl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证
- curl -uxujb:passwd 访问状态码变为200
编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗 - 针对目录的用户认证
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}