nginx访问认证【访问nginx需要用户名密码】

###

访问认证主要用于企业内部人员访问的地址上,例如:企业网站后台,MySQL客户端phpmyadmin、企业内部的CRM、WIKI网站平台等。

> 创建密码认证文件并进行授权

1)创建密码文件
yum install httpd-tools -y
htpasswd -bc /wx/nginx/conf/htpasswd admin 123456
2)授权(有些nginx安装权限设置400会出现权限错误,详情查看nginx错误日志)
chmod 744 /application/nginx/conf/htpasswd     
  

> 修改nginx配置 - 重启nginx

注意:如果需要对不同location做不同的认证,server下面不可以写root html/www;,需要将路径全部写到对应的location中,否则会有500报错


[root@web02 extra]# vim www.conf server { listen
80;
# root html/www; server_name www.etiantian.org; location
/ { root html/www; index index.html index.htm; auth_basic "登录认证"; auth_basic_user_file /wx/nginx/conf/htpasswd; } location /download {
# 下载文件所在目录 alias
/etc/nginx/html/download; auth_basic "登录认证"; auth_basic_user_file /wx/nginx/conf/htpasswd1; } } ############################################################ ##参数说明: auth_basic 语法:auth_basic straining|off; 默认值:auth_basic off; 使用位置:http、server、location、limit_except auth_basic_user_file 语法:auth_basic_user_file 默认值:—— 使用位置:http、server、location、limit_except auth_basic_user_file参数后接认证密码文件

> 测试配置结果

[root@web02 www]# curl -u admin www.etiantian.org
Enter host password for user 'admin':
web01 www.etiantian.org

###

 

原文地址:https://www.cnblogs.com/faithH/p/14517868.html