官方指導文檔:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
應用場景:企業在實際工作要求我們爲網站設置訪問賬號和密碼權限,這樣操作後,只有擁有賬號密碼的用戶纔可以訪問網站內容。
location / { auth_basic "closed site"; auth_basic_user_file conf/htpasswd; }
關鍵詞解析:
auth_basic
Synatx: auth_basic string|off;
# 輸入密碼之前的提示,默認關,
Default::auth_basic off;
放置標籤:http
, server
, location
, limit_except
auth_basic_user_file file
Synatx: auth_basic_user_file file;
# 指向存放用戶名密碼的文件
Default: —
Context: http, server, location, limit_except
Specifies a file that keeps user names and passwords, in the following format:
# comment
name1:password1
name2:password2:comment
name3:password3
實戰演練
在server虛擬主機配置
設置密碼文本【本次僅做演示,所以操作用文本操作】
補充:httpasswd爲apache自帶包,安裝apache之後就安裝了httpasswd包
此處 -c 爲指定文件 -b爲非交互輸入
效果展示