一,http基本驗證的作用:
1,http基本身份驗證會從瀏覽器彈出登錄窗口,
簡單明了,容易理解,
對於面向終端用戶的前台來說,不夠友好,
但對於內部員工操作的后台還是很有用,通常作為一層安全措施應用。
2,
在這里為例子,我們新建一個站:
域名: admin.lhdtest.com 登錄名: admin 密碼: 12345678
這里僅作為演示,生產環境不能使用這種極簡單的密碼
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: 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