[ Nginx ] 開放特定ip & 帳號密碼登入
17 Jul 2018Nginx
帳號/密碼 登入
這個方法是透過 HTTP Basic Authentication 驗證登入
而驗證的帳號密碼是透過 Nginx 中的 auth_basic_user_file
去讀檔案
xxx.conf
假設要在你的預設目錄下要限制帳號密碼登入要這樣寫
location / {
auth_basic "You're not allow.";
auth_basic_user_file conf/htpasswd;
}
假設全站都需要帳號密碼保護要寫在 Nginx_path/site-avalible/default
的 server
裡面
但是你其中一支 Reverse proxy location /api/
不需要做驗證,可以關掉
server {
auth_basic "You're not allow";
auth_basic_user_file conf/htpasswd;
location /api/ {
auth_basic off;
}
}
這樣使用者在瀏覽 domain/api 的時候不需要密碼,其他不同的網址才要
在 auth_basic_user_file
裡面是這樣寫
# 註解
user1:passwd1
user2:passwd2:註解
user3:passwd3
其中 conf/htpasswd
裡 passwd
需要經過加密
所以這時候要透過 htpasswd
或是 openssl
作轉換
這裡我使用 htpasswd
做加密
sudo htpasswd -cb /etc/nginx/conf/htpasswd user1
Adding password for user1
New password:
Re-type new password:
再到 /etc/nginx/conf/htpasswd
確認即可
限制可以讀取的IP位址
Nginx 只要 allow
與 deny
就可以做到
xxx.conf
同意 127.0.0.1、123.123.123.123 並拒絕其他人的連線
location / {
allow 127.0.0.1;
allow 123.123.123.123;
deny all;
}
就像防火牆般,看規則的方法使由上而下,所以 deny all
請放在最後
xxx.conf
同意 172.23.15.0 整個網段並拒絕 172.23.15.10 的連線
location / {
deny 172.23.15.10;
allow 172.23.15.0/24;
deny all;
}
有特定單獨的IP才抓出來放在同意的前面
完成!