1. 程式人生 > 實用技巧 >nginx配置網頁密碼

nginx配置網頁密碼

環境:

nginx 1.16
httpd Apache/2.4.6

yum安裝方式:(yum沒有此軟體時,需要配置yum源)

yum install nginx -y   
yum install httpd -y

啟動nginx 、httpd

systemctl start nginx
systemctl  start httpd

建立密文

[root@243-ceshi download]# htpasswd -cb /dwz/htpasswd kaifa2  vrgv25.      
Adding password for user kaifa2 [root@
243-ceshi download]# cat /dwz/htpasswd kaifa2:$apr1$UJrSJ10s$asQLF/ImBymWnB1zk5U6S0

注:/dwz目錄需要存在,要不會報錯

nginx配置檔案:


[root@243-ceshi download]#vim /etc/nginx/nginx.conf.d/download.conf
server {
        listen       30000;                                          #埠號自定義
        root         /usr/share/nginx/html;                           
        auth_basic 
"This is input password"; #網頁輸入密碼提示語 auth_basic_user_file /dwz/htpasswd; #密文存放位置 # Load configuration files for the default server block. location /download { #下面是自定義下載引導位置,當訪問ip:30000/download後會轉到伺服器的/download/目錄下,提供下載 charset utf
-8; #支援中文 alias /download/; #轉的下載目錄 index index.html; autoindex on; autoindex_exact_size off; } }

注:download.conf配置檔案是自定義建立的,並被nginx管理

重新啟動nginx

systemctl restart nginx

開啟網址:

完成