题目:
使用基于账号访问的配置,来配置通过 www.haha.com:8080/custom/index.html 访问时显示“你可以访问”,如果是 www.haha.com:8080/requir/index.html 则提示需要用户名和密码才能访问。
创建身份认证文件
[root@localhost conf.d]# htpasswd -cb passwdfile zhangsan 123456
Adding password for user zhangsan
[root@localhost conf.d]# chmod 600 passwdfile
[root@localhost conf.d]# chown nginx:root passwdfile
[root@localhost conf.d]# ll passwdfile
-rw-------. 1 nginx root 47 Mar 24 19:20 passwdfile
以下是这样设置的好处,在这个实验中不这样设置也没有什么影响,为了安全和专业还是养成习惯。
权限设置为 600
安全性:权限600表示文件所有者有读和写的权限,而其他用户和组没有任何权限。这样可以确保只有所有者(即nginx用户)能够访问和修改该文件,防止其他用户或进程意外或恶意地读取或修改密码文件,从而保护用户认证信息的安全性,避免敏感信息泄露。
所有者设置为 nginx
进程运行身份:Nginx 通常以nginx用户身份运行进程。将passwdfile的所有者设置为nginx,是为了让 Nginx 进程能够以其运行身份(nginx用户)对该文件具有适当的访问权限,以便在需要进行用户身份验证时能够读取密码文件中的信息,而不会因为权限不足导致身份验证失败。如果文件所有者不是nginx,可能会出现 Nginx 进程无法访问密码文件的情况,进而无法正常进行基于账号访问的身份验证功能。
书写配置文件
在 /etc/nginx/conf.d
目录下创建或编辑一个配置文件,如 haha.conf
第一种
server {
listen 8080;
server_name www.haha.com;
root /opt/haha;
location /custom/index.html {
index index.html;
}
location /requir/index.html {
auth_basic "请输入用户名和密码";
auth_basic_user_file /etc/nginx/conf.d/passwdfile;
}
}
第一种
[root@localhost conf.d]# echo '你可以进入。'>/opt/haha/custom/index.html
通过html文件来显示你可以进入。
[root@localhost conf.d]# systemctl restart nginx
[root@localhost conf.d]# curl www.haha.com:8080/custom/index.html
你可以进入。
第二种
server {
listen 8080;
server_name www.haha.com;
root /opt/haha;
location /custom/index.html {
default_type text/plain;
return 200 "你可以访问";
}
location /requir/index.html {
auth_basic "请输入用户名和密码";
auth_basic_user_file /etc/nginx/conf.d/passwdfile;
}
}
location /custom/index.html:匹配 /custom/index.html 的请求,使用 default_type text/plain; 指定返回内容的类型为纯文本,return 200 "你可以访问"; 返回状态码 200 和文本 “你可以访问”。
200不能省略
在 Nginx 配置中,如果location /custom/index.html块中没有default_type text/plain;这一行配置,可能会产生以下影响:
内容类型识别问题:Nginx 默认会根据文件的扩展名来确定响应的Content - Type头信息。但在这个配置中,由于使用return直接返回文本内容,而不是返回一个实际的文件,Nginx 没有文件扩展名可以参考来确定内容类型。因此,缺少default_type配置可能导致 Nginx 无法正确设置Content - Type头信息,或者将其设置为默认的application/octet - stream(表示二进制流)。这可能会使客户端(如浏览器)在解析和显示内容时出现问题,例如,浏览器可能会将其当作二进制文件下载而不是直接显示文本内容。
浏览器行为异常:浏览器根据Content - Type来决定如何处理接收到的数据。如果Content - Type不正确,浏览器可能会采取错误的方式来处理数据。例如,可能会尝试将文本内容当作其他类型的文件进行解析,导致显示乱码或无法正常显示。此外,浏览器的一些安全机制也可能会受到不正确Content - Type的影响,从而对页面的加载和显示产生限制。
缓存问题:内容类型对于缓存机制也很重要。不同的内容类型可能有不同的缓存策略。如果Content - Type设置不正确,可能会导致缓存行为异常,例如缓存被不正确地设置或无法被有效利用,从而影响性能。
综上所述,default_type text/plain;这个配置项在这种情况下是很重要的,它确保了 Nginx 正确地设置响应的内容类型,使客户端能够正确地处理和显示返回的文本信息。
重启nginx
在修改配置文件后,需要检查配置文件的语法是否正确:
nginx -t
如果语法检查通过,重启 Nginx 服务使配置生效:
systemctl restart nginx
验证是否符合要求
[root@localhost conf.d]# curl www.haha.com:8080/custom/index.html
你可以访问[root@localhostcurl www.haha.com:8080/requir/index.html
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost conf.d]# curl -u zhangsan:123456 www.haha.com:8080/requir/index.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost conf.d]# echo '你已经通过账号密码进入'>/opt/haha/requir/index.html
[root@localhost conf.d]# curl -u zhangsan:123456 www.haha.com:8080/requir/index.html
你已经通过账号密码进入