1. Nginx http安全认证

http安全认证模块只有两个参数,可以放在server{}标签内 也可以放在Location 下面。
官方使用案例:
location / {
auth_basic “closed site”; #提示信息
auth_basic_user_file conf/htpasswd; #密码文件路径
}

详情请参考官方链接:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

2. http安全认证实战

在下面的配置文件中,红色部分属于http安全验证配置部分。
server {

  1. listen 80;<br /> server_name www.etiantian.org;<br /> location / {<br /> auth_basic "hello";<br /> auth_basic_user_file /app/nginx/conf/htpasswd;<br /> root html/www;<br /> index index.html;<br /> }<br /> }

经过上面的配置之后,打开网站就会提示输入用户名和密码,用户名和密码我们需要单独创建。密码不支持明文模式,必须用软件来生成加密的密文。软件下载地址https://www.linux.ac.cn/scripts/htpasswd.py ,这是一个Python脚本,使用方法如下:
创建htpasswd文件的同时创建用户admin,密码123456
[root@web01 ~]# python htpasswd.py -c /app/nginx/conf/htpasswd -b admin 123456
创建好后,重新启动Nginx,在打开网页的时候就会弹出需要输入用户名和密码了。

参数说明:-c 创建htpasswd文件并指定创建到 /app/nginx/conf/目录下;
-b 后面接用户名admin 和密码123456,用户名和密码中间有空格隔开。