Table of Contents
一、参数说明
二、实现访问认证步骤
三、Nginx访问认证出现状态码403 forbidden的原因
四、在网站展示目录结构
需求:在实际工作企业中有时需要我们为网站设置访问账号和密码权限,这样操作后,只有拥有账号密码的人才能访问网站内容。
依赖模块: ngx_http_auth_basic_module,Nginx默认就已经安装,不需要添加模块
一、参数说明
1、auth_basic
指定设置认证提示字符串的内容的。
2、auth_basic_user_file
用于设置认证的密码文件的完整路径。
二、实现访问认证步骤
1、配置文件
server {listen 80;server_name localhost;location / {root html;index index.html index.htm;auth_basic "please input your account and password for account!";auth_basic_user_file /usr/local/nginx/conf/vhost/htpasswd; //建议写完整路径,若配置错误路径,会不厌
其烦的出现403forbidden//上面两句是重点}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
2、创建htpasswd文件
[root@proxy-server conf]# cd vhost/
[root@proxy-server vhost]# touch htpasswd
3、安装httpd,并用htpasswd命令生成账号密码
[root@proxy-server vhost]# yum install httpd -y
[root@proxy-server vhost]# htpasswd -bc /usr/local/nginx/conf/vhost/htpasswd haha 123456
[root@proxy-server vhost]# chmod 400 /usr/local/nginx/conf/vhost/htpasswd
4、重启Nginx,访问
认证界面:
认证成功:
三、Nginx访问认证出现状态码403 forbidden的原因
1、未指定首页文件参数
location / {root html;index index.html index.htm;}
//上面这两行指定有错误
2、指定的文件不存在
比如说第一种情况的index.html文件在html目录里根本就不存在
3、html目录没有访问权限,造成无法访问html文件
4、Nginx配置文件中配置了allow、deny等权限控制
5、auth_basic_user_file 密码文件路径指定错误
四、在网站展示目录结构
1、主配置文件的配置
location / {root html;autoindex on;}
2、删除html目录下的index.html文件
3、访问
可以点击文件达到浏览不同网页的效果
root如果后面设置为 /的效果:
location / {root /;autoindex on;}
根据上述图片,可以推断出root的设置需要谨慎。
apache在展示目录结构浏览的时候,会存在宿主目录禁锢的问题,不然存在安全问题。Nginx默认就实现了宿主目录禁锢。