nginx配置訪問密碼,輸入使用者名稱和密碼才能訪問

2022-06-11 22:00:17 字數 2090 閱讀 6486

使用nginx搭建的站點,如果不想讓所有人都能正常訪問,那麼可以設定訪問認證,只有使用者輸入正確的使用者名稱和密碼才能正常訪問。

在 nginx 下,提供了 ngx_http_auth_basic_module 模組實現讓使用者只有輸入正確的使用者名稱密碼才允許訪問web內容。預設情況下,nginx 已經安裝了該模組。所以整體的乙個過程就是先用第三方工具( htpasswd,或者使用 openssl)設定使用者名稱、密碼(其中密碼已經加過密),然後儲存到檔案中,接著在 nginx 配置檔案中根據之前事先儲存的檔案開啟訪問驗證。

[sandu@bogon conf]$ sudo yum install httpd-tools
設定使用者名稱和密碼,並把使用者名稱、密碼儲存到指定檔案中:

[sandu@bogon conf]$ sudo mkdir passwd

[sandu@bogon conf]$ sudo htpasswd -c passwd/passwd sandu

new password:

re-type new password:

adding password for user sandu

[sandu@bogon conf]$ cat passwd/passwd

sandu:$apr1$j5sg0fqd$kdm3oypj8wf9477phdiza0

注意:上面的 passwd/passwd 是生成密碼檔案的路徑,絕對路徑是/usr/local/openresty/nginx/conf/passwd/passwd ,然後sandu是使用者名稱,你可以根據需要自行設定成其它使用者名稱。執行命令後,會要求你連續輸入兩次密碼。輸入成功後,會提示已經為sandu這個使用者新增了密碼。

檢視下生成的密碼檔案的內容:

[sandu@bogon conf]$ cat passwd/passwd 

sandu:$apr1$j5sg0fqd$kdm3oypj8wf9477phdiza0

其中使用者名稱就是sandu,分號後面就是密碼(已經加過密)。

找到 nginx 配置檔案,因為我們要對整個站點開啟驗證,所以在配置檔案中的第乙個server修改如下:

server
然後重啟 nginx:

[sandu@bogon conf]$ sudo systemctl reload openresty.service
以上都配置無誤後,你重新訪問你的站點,如果出現需要身份驗證的彈窗就說明修改成功了。

htpasswd選項引數 :

htpasswd [-cmdpsd] passwordfile username

htpasswd -b[cmdpsd] passwordfile username password

htpasswd -n[mdps] username

htpasswd -nb[mdps] username password

htpasswd命令選項引數說明:

如何利用htpasswd命令新增使用者?

htpasswd -bc ./.passwd sandu pass

在當前目錄下生成乙個.passwd檔案,使用者名稱sandu,密碼:pass,預設採用md5加密方式

如何在原有密碼檔案中增加下乙個使用者?

htpasswd -b ./.passwd sandu1 pass

去掉c選項,即可在第乙個使用者之後新增第二個使用者,依此類推

如何不更新密碼檔案,只顯示加密後的使用者名稱和密碼?

htpasswd -nb sandu pass

不更新.passwd檔案,只在螢幕上輸出使用者名稱和經過加密後的密碼

如何利用htpasswd命令刪除使用者名稱和密碼?

htpasswd -d .passwd sandu

如何利用 htpasswd 命令修改密碼?

htpasswd -d .passwd sandu

htpasswd -b .passwd sandu pass

Nginx使用者訪問及密碼驗證

需求 訪問web頁面需要進行使用者認證 使用者名為 tom,密碼為 123456 配置nginx配置檔案 root proxy vim usr local nginx conf nginx.conf server 使用htpasswd生成密碼檔案 root web01 extra which htp...

nginx配置站點密碼訪問辦法

阿里雲上面放了個nginx,但是不希望別人訪問,除了吧url搞得非常長並且奇怪 這樣只有你告訴別人才知道位址。環境 ubuntu linux下都差不多,可能某些配置檔案已經命令有微小區別 但是這樣還是不夠嚴謹,所以還是配置下賬號密碼。nginx配置檔案中加入 location 密碼檔案不能簡單的在檔...

訪問的站點提示輸入使用者名稱密碼

問題 訪問的站點提示輸入使用者名稱密碼 解決方案 對於這種訪問彈框的站點,有兩種可能性造成的 1.站點的iuser密碼被修改了,一般都是通過站點程式或系統管理員修改的 這種情況下在使用者管理 使用者 修改密碼,然後在iis 相應的站點 屬性 目錄安全性 身份驗證和訪問控制,將密碼進行同步即可 2.站...