nginx安全: 配置http基本验证(Basic Auth)(nginx 1.18.0)

一,http基本验证的作用:

1,http基本身份验证会从浏览器弹出登录窗口,

   简单明了,容易理解,

   对于面向终端用户的前台来说,不够友好,

   但对于内部员工操作的后台还是很有用,通常作为一层安全措施应用。

2,

在这里为例子,我们新建一个站:

域名:     admin.lhdtest.com
登录名:    admin
密码:     12345678

这里仅作为演示,生产环境不能使用这种极简单的密码

 

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/22/nginx-pei-zhi-http-ji-ben-yan-zheng-basicauthnginx1180/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,生成供测试的站内文件

1,生成网站的目录

[root@centos8 ~]# mkdir -p /data/site/admin/html

 

2,生成网站的index页面

[root@centos8 ~]# vi /data/site/admin/html/index.html

 

页面html代码:

<html>
<head>
</head>
<body>
welcome to admin.lhdtest.com
</body>
</html>

 

三,配置nginx的http基本验证

1,在nginx.conf中检查是否有对conf.d目录的支持

    如不存在,添加它

include /usr/local/soft/nginx-1.18.0/conf/conf.d/*.conf;

说明:生产环境中,为了管理方便,会把每个server放到专用的conf文件中,

           不要混在一起而全写到nginx.conf中,修改和查找都不方便

 

2,创建网站的server文件

[root@centos8 conf.d]# vi admin.conf

内容:

server {
    auth_basic "lhdtest.com admin";
    auth_basic_user_file /usr/local/soft/nginx-1.18.0/conf/conf.d/admin.pwd;

    listen       80;
    server_name  admin.lhdtest.com;
    root         /data/site/admin/html;
    index        index.html index.shtml  index.htm;
    access_log      /data/nginx/logs/admin.access_log;
    error_log       /data/nginx/logs/admin.error_log;
}

 

2,生成密码:

如果找不到htpasswd命令,

可以用dnf安装这个包:

[root@centos8 conf.d]# dnf install httpd-tools

 

生成密码:

#-n:Don't update file; display results on stdout

#-b:Use the password from the command line

#-m:Force MD5 encryption of the password

[root@centos8 conf.d]# htpasswd -nbm admin 12345678
admin:$apr1$nkxLxBPa$EGa.u5yKuQ08m6g/8bGb9.

写入到密码文件

[root@centos8 conf.d]# vi admin.pwd

内容:

admin:$apr1$nkxLxBPa$EGa.u5yKuQ08m6g/8bGb9.

 

3,重启nginx

[root@centos8 conf.d]# systemctl stop nginx
[root@centos8 conf.d]# systemctl start nginx

 

4,测试效果

看截图,输入正确的用户名和密码即可进入网站

 

四,查看nginx的版本

[root@centos8 soft]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v
nginx version: nginx/1.18.0

 

posted @ 2020-04-25 09:53  刘宏缔的架构森林  阅读(2110)  评论(0编辑  收藏  举报