當別人訪問你的網(wǎng)站或者自己搭建的博客時捣作,想要先認證一下賬號密碼才允許訪問衣摩?
nginx簡單配置即可搞定!
版本信息 (我是centos8 但是就目前這個功能來講戈擒,7攀圈、8無異)
[root@CentOS-8 ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
yum安裝nginx
[root@CentOS-8 ~]# yum install nginx -y
最簡化nginx配置文件
egrep -v "^$|#" /etc/nginx/nginx.conf.default > /etc/nginx/nginx.conf
啟動nginx
nginx -t
nginx
網(wǎng)頁訪問
image.png
安裝插件
yum install httpd-tools -y
htpasswd -bc /etc/nginx/conf.d/htpasswd long 123456
#/etc/nginx/conf.d/htpasswd 是生成密碼的存放路徑 建議放在nginx配置文件目錄下
#long 訪問時需要輸入的用戶名
#123456 訪問時需要輸入的密碼
chmod 644 /etc/nginx/conf.d/htpasswd
chown nginx /etc/nginx/conf.d/htpasswd
ll /etc/nginx/conf.d/htpasswd
-rw-r--r-- 1 nginx root 43 5月 28 12:32 /etc/nginx/conf.d/htpasswd
編輯nginx配置文件
vim /etc/nginx/nginx.conf #最原始nginx之后的配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
auth_basic "long training"; #新增,訪問時提示
auth_basic_user_file /etc/nginx/conf.d/htpasswd; #新增密碼文件路徑
}
}
}
重啟nginx
nginx -t
nginx -s reload
再次訪問
image.png