nginx服务设置训练

[复制链接]
发表于 昨天 06:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
标题:

利用基于账号访问的设置,来设置通过 www.haha.com:8080/custom/index.html 访问时表现“你可以访问”,假如是 www.haha.com:8080/requir/index.html 则提示必要用户名和暗码才华访问。
创建身份认证文件

  1. [root@localhost conf.d]# htpasswd -cb passwdfile zhangsan 123456
  2. Adding password for user zhangsan
  3. [root@localhost conf.d]# chmod 600 passwdfile
  4. [root@localhost conf.d]# chown nginx:root passwdfile
  5. [root@localhost conf.d]# ll passwdfile
  6. -rw-------. 1 nginx root 47 Mar 24 19:20 passwdfile
  7. 以下是这样设置的好处,在这个实验中不这样设置也没有什么影响,为了安全和专业还是养成习惯。
  8. 权限设置为 600
  9. 安全性:权限600表示文件所有者有读和写的权限,而其他用户和组没有任何权限。这样可以确保只有所有者(即nginx用户)能够访问和修改该文件,防止其他用户或进程意外或恶意地读取或修改密码文件,从而保护用户认证信息的安全性,避免敏感信息泄露。
  10. 所有者设置为 nginx
  11. 进程运行身份:Nginx 通常以nginx用户身份运行进程。将passwdfile的所有者设置为nginx,是为了让 Nginx 进程能够以其运行身份(nginx用户)对该文件具有适当的访问权限,以便在需要进行用户身份验证时能够读取密码文件中的信息,而不会因为权限不足导致身份验证失败。如果文件所有者不是nginx,可能会出现 Nginx 进程无法访问密码文件的情况,进而无法正常进行基于账号访问的身份验证功能
复制代码
誊写设置文件

在 /etc/nginx/conf.d 目次下创建或编辑一个设置文件,如 haha.conf 
第一种

  1. server {
  2.     listen 8080;
  3.     server_name www.haha.com;
  4.     root /opt/haha;
  5.     location /custom/index.html {
  6.         index index.html;
  7.     }
  8.     location /requir/index.html {
  9.         auth_basic "请输入用户名和密码";
  10.         auth_basic_user_file /etc/nginx/conf.d/passwdfile;
  11.     }
  12. }
  13. 第一种
  14. [root@localhost conf.d]# echo '你可以进入。'>/opt/haha/custom/index.html
  15. 通过html文件来显示你可以进入。
  16. [root@localhost conf.d]# systemctl restart nginx
  17. [root@localhost conf.d]# curl www.haha.com:8080/custom/index.html
  18. 你可以进入。
复制代码
 第二种

  1. server {
  2.     listen 8080;
  3.     server_name www.haha.com;
  4.     root /opt/haha;
  5.     location /custom/index.html {
  6.         default_type text/plain;
  7.         return 200 "你可以访问";
  8.     }
  9.     location /requir/index.html {
  10.         auth_basic "请输入用户名和密码";
  11.         auth_basic_user_file /etc/nginx/conf.d/passwdfile;
  12.     }
  13. }
  14. location /custom/index.html:匹配 /custom/index.html 的请求,使用 default_type text/plain; 指定返回内容的类型为纯文本,return 200 "你可以访问"; 返回状态码 200 和文本 “你可以访问”。
  15. 200不能省略
  16. 在 Nginx 配置中,如果location /custom/index.html块中没有default_type text/plain;这一行配置,可能会产生以下影响:
  17. 内容类型识别问题:Nginx 默认会根据文件的扩展名来确定响应的Content - Type头信息。但在这个配置中,由于使用return直接返回文本内容,而不是返回一个实际的文件,Nginx 没有文件扩展名可以参考来确定内容类型。因此,缺少default_type配置可能导致 Nginx 无法正确设置Content - Type头信息,或者将其设置为默认的application/octet - stream(表示二进制流)。这可能会使客户端(如浏览器)在解析和显示内容时出现问题,例如,浏览器可能会将其当作二进制文件下载而不是直接显示文本内容。
  18. 浏览器行为异常:浏览器根据Content - Type来决定如何处理接收到的数据。如果Content - Type不正确,浏览器可能会采取错误的方式来处理数据。例如,可能会尝试将文本内容当作其他类型的文件进行解析,导致显示乱码或无法正常显示。此外,浏览器的一些安全机制也可能会受到不正确Content - Type的影响,从而对页面的加载和显示产生限制。
  19. 缓存问题:内容类型对于缓存机制也很重要。不同的内容类型可能有不同的缓存策略。如果Content - Type设置不正确,可能会导致缓存行为异常,例如缓存被不正确地设置或无法被有效利用,从而影响性能
  20. 综上所述,default_type text/plain;这个配置项在这种情况下是很重要的,它确保了 Nginx 正确地设置响应的内容类型,使客户端能够正确地处理和显示返回的文本信息。
复制代码
重启nginx

  1. 在修改配置文件后,需要检查配置文件的语法是否正确:
  2. nginx -t
  3. 如果语法检查通过,重启 Nginx 服务使配置生效:
  4. systemctl restart nginx
复制代码
验证是否符合要求

  1. [root@localhost conf.d]# curl www.haha.com:8080/custom/index.html
  2. 你可以访问[root@localhostcurl www.haha.com:8080/requir/index.html
  3. <html>
  4. <head><title>401 Authorization Required</title></head>
  5. <body>
  6. <center><h1>401 Authorization Required</h1></center>
  7. <hr><center>nginx/1.20.1</center>
  8. </body>
  9. </html>
  10. [root@localhost conf.d]# curl -u zhangsan:123456 www.haha.com:8080/requir/index.html
  11. <html>
  12. <head><title>404 Not Found</title></head>
  13. <body>
  14. <center><h1>404 Not Found</h1></center>
  15. <hr><center>nginx/1.20.1</center>
  16. </body>
  17. </html>
  18. [root@localhost conf.d]# echo '你已经通过账号密码进入'>/opt/haha/requir/index.html
  19. [root@localhost conf.d]# curl -u zhangsan:123456 www.haha.com:8080/requir/index.html
  20. 你已经通过账号密码进入
复制代码






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表