有時候我們通過nginx搭建了一臺文件服務(wù)器, 一般來講是公開的, 但我們又希望該服務(wù)器不讓他人看到, 有人可能會搭建一個登錄系統(tǒng), 但是太麻煩, 也沒太大必要, 比較簡單的做法是配置Basic Auth登錄認(rèn)證
1. 確定你安裝了httpd-tools
yum install httpd-tools -y
2. 創(chuàng)建授權(quán)用戶和密碼
htpasswd -c -d /usr/local/openresty/nginx/conf/pass_file magina
這個配置文件存放路徑可以隨意指定, 這里我指定的是nginx配置文件目錄, 其中magina是指允許登錄的用戶名, 這個可以自定義
3. 配置Nginx
大致配置如下:
server {
listen 80;
server_name res.yinnote.com;
auth_basic "登錄認(rèn)證";
auth_basic_user_file /usr/local/openresty/nginx/conf/pass_file;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
root /mnt/html/resource;
index index.html index.php;
}
其中 auth_basic 和 auth_basic_user_file 是認(rèn)證的配置, 注意密碼文件的路徑一定是上面生成的
4. 使用
# 瀏覽器中使用
直接在瀏覽器中輸入地址, 會彈出用戶密碼輸入框, 輸入即可訪問
# 使用 wget
wget --http-user=magina --http-passwd=123456 http://res.yinnote.com/xxx.zip
# 使用 curl
curl -u magina:123456 -O http://res.yinnote.com/xxx.zip
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。