当别人访问你的网站或者自己搭建的博客时,想要先认证一下账号密码才允许访问?
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
网页访问
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
网友评论