1. 程式人生 > 實用技巧 >【隨手記錄】關於nginx的驗證指令auth_basic

【隨手記錄】關於nginx的驗證指令auth_basic

1、有些軟體本身沒有自帶使用者體系,我們可以通過nginx的ngx_http_auth_basic_module模組(nginx預設安裝了這個模組)帶的auth_basic、auth_basic_user_file兩個指令實現簡單的使用者驗證!

語法:       auth_basic string | off;
預設值:     auth_basic off;
配置段:     http, server, location, limit_except
預設表示不開啟認證,後面如果跟上字元,這些字元會在彈窗中顯示。

語法:       auth_basic_user_file file;
預設值:     —
配置段:     http, server, location, limit_except

這裡需要注意auth_basic_user_file 指令用來配置密碼儲存的檔案,檔案路徑需要絕對路徑,如果是相對路徑就會一直報403 forbidden!

2、密碼檔案生成

可以通過htpasswd或者openssl生成密碼檔案,以htpasswd為例:

htpasswd --help
Usage:
    htpasswd [-cimBdpsDv] [-C cost] passwordfile username
    htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password

    htpasswd -n[imBdps] [-C cost] username
    htpasswd 
-nb[mBdps] [-C cost] username password -c Create a new file. -n Don't update file; display results on stdout. -b Use the password from the command line rather than prompting for it. -i Read password from stdin without verification (for script usage). -m Force MD5 encryption of the password (default
). -B Force bcrypt encryption of the password (very secure). -C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 17). -d Force CRYPT encryption of the password (8 chars max, insecure). -s Force SHA encryption of the password (insecure). -p Do not encrypt the password (plaintext, insecure). -D Delete the specified user. -v Verify password for the specified user. On other systems than Windows and NetWare the '-p' flag will probably not work. The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.

常用的引數:

-c建立一個新檔案

-m使用MD5加密,預設

-p密碼不加密

對於指令auth_basic_user_file不支援plaintext密碼,會一直報錯 密碼不對!

3、正常操作