Nginx實現訪問控制

2021-10-09 20:27:13 字數 2326 閱讀 7086

二、基於ip的訪問控制實戰

三、基於basic auth認證的訪問控制實戰

實現nginx的訪問控制有兩種方式:基於ip的訪問控制和基於basic auth 認證的訪問控制。

(1)編輯nginx配置檔案

檢視一下我宿主機ip。

}實現功能:

需要注意:

[root@zrs conf]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@zrs conf]# /usr/local/nginx/sbin/nginx -s reload

(2)瀏覽器端訪問

可見許可權被拒,返回403狀態碼。

如果你想拒絕某個指定url位址的所有請求,只需要在 location 塊中配置 deny all 指令即可。

server 

}

[root@zrs conf]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@zrs conf]# /usr/local/nginx/sbin/nginx -s reload

有時我們會有這麼一種需求,就是你的**的某些頁面不希望公開,我們希望的是某些特定的客戶端可以訪問。那麼我們可以在訪問時要求進行身份認證,就如給你自己的家門加一把鎖,以拒絕那些不速之客。

基於使用者的信任登入模組:http_auth_basic_module

(1)編輯配置檔案/usr/local/nginx/conf/nginx.conf

server 

}

auth_basic不為off,開啟登入驗證功能,auth_basic_user_file載入賬號密碼檔案。

(2)建立口令檔案

htpasswd 是開源 http 伺服器 apache httpd 的乙個命令工具,用於生成 http 基本認證的密碼檔案。

[root@zrs ~]# htpasswd -cm /etc/nginx/auth_conf user1 #-c:建立解密檔案;-m:md5加密

[root@zrs ~]# htpasswd -m /etc/nginx/auth_conf user2

[root@zrs ~]# cat /etc/nginx/auth_conf

user1:$apr1$rkposilw$bu2gk7lehqvjka/5kslht/

user2:$apr1$w.//6e7a$7ce/.p4fceowmrlltw/tq/

(3)瀏覽器端訪問

以上操作均實現了訪問控制的效果,這對於我們的運維安全是由一定的實際效果的。

Nginx訪問控制

配置nginx,禁止訪問指定目錄下的指定程式 location images php php5 sh pl py nginx下配置禁止訪問 txt和 doc檔案 location txt doc location txt doc 禁止訪問單個目錄 location static 禁止訪問多個目錄 l...

Nginx訪問控制

基於ip的訪問控制 http access module 基於使用者的信任登入 http auth basic module 語法1 允許那些ip可以訪問 語法2 不允許那些ip訪問 1.配置不允許指定ip訪問,其他ip都可以訪問 location admin.html測試一下 使用其他ip訪問,訪...

Nginx服務訪問控制

在企業中有時候需要為 進行進行許可權控制。配置示例 location auth basic引數 設定認證提示資訊 語法 auth basic 字串 off 預設值是off。auth basic user file引數 設定密碼檔案位置 語法 auth basic user file 檔案 使用位置 ...