訪問nginx需要先輸入密碼認(rèn)證

當(dāng)別人訪問你的網(wǎng)站或者自己搭建的博客時(shí),想要先認(rèn)證一下賬號(hào)密碼才允許訪問?
nginx簡(jiǎn)單配置即可搞定!

版本信息 (我是centos8 但是就目前這個(gè)功能來講,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

最簡(jiǎn)化nginx配置文件

egrep -v "^$|#" /etc/nginx/nginx.conf.default  > /etc/nginx/nginx.conf

啟動(dòng)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 訪問時(shí)需要輸入的用戶名
#123456 訪問時(shí)需要輸入的密碼
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";                     #新增,訪問時(shí)提示
       auth_basic_user_file /etc/nginx/conf.d/htpasswd;     #新增密碼文件路徑
     }
   }
}

重啟nginx

nginx -t
nginx -s reload

再次訪問

image.png

此時(shí)輸入設(shè)置的用戶名和密碼即可訪問!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容