天天看點

nginx限制ip通路_nginx的通路控制

基于ip的通路控制 - http-access_module

Syntax: allow address | CIDR | unix: |all;
Default --;
Context: http,server,location,limit_except;

Syntax: deny address | CIDR | unix: |all;
Default --;
Context: http,server,location,limit_except;
           

限制固定ip進行通路實戰

location / {
         root   /usr/share/nginx/html;
         deny 115.194.182.43;
         allow all;
}
           

通路結果

nginx限制ip通路_nginx的通路控制

局限性:預設情況下remote_addr用戶端ip但是以目前的網際網路情況有可能用戶端使用代理ip或者其他方式進行ip僞裝

nginx限制ip通路_nginx的通路控制

解決方式

  • 采用别的http頭資訊控制通路,如httpx_forwarded_for
  • 結合geo子產品
  • 通過http自定義變量傳遞

基于使用者信任登入 - httpauth_basic_module

Syntax: auth_basic string off;
Default auth_basic off;
Context: http,server,location,limit_except;


Syntax: auth_basic_user_file file;
Default auth_basic off;
Context: http,server,location,limit_except;
           

Module ngx_http_auth_basic_module​nginx.org

name2:password2:comment
           

實戰示範密碼登入

[[email protected]_159_140_centos nginx]# yum install -y httpd-tools

[[email protected]_159_140_centos nginx]# htpasswd -c ./auth_conf zhiqiang
New password: 123456
Re-type new password: 123456
Adding password for user zhiqiang

[[email protected]_159_140_centos nginx]# more auth_conf 
zhiqiang:$apr1$JZSNY4/Y$tD/NGTE4oA7EjIdTOBjFR.

[[email protected]_159_140_centos nginx]# pwd
/www/nginx
           

配置檔案配置格式

location / {
        root   /usr/share/nginx/html;
        auth_basic "input you username and password!";
        auth_basic_user_file /www/nginx/auth_conf;
    }
           

最終效果

nginx限制ip通路_nginx的通路控制

局限性

  • 使用者依賴檔案方式
  • 操作管理機械,效率低下

解決方案

nginx結合LUA實作高效驗證

nginx和LDAP打通,利用nginx-auth-idap子產品