人机验证

1. 功能描述

人机验证功能是为了当某个location 被恶意的频繁访问时,可以根据配置,对访问频率超过一定次数的client_ip,动态的弹出认证页面。

2. 依赖模块

人机验证功能依赖模块:

load_module modules/njt_http_captcha_module.so;  

3. 指令说明

Syntax: captcha
Default: 开启captcha 功能。
Context: location
Syntax: captcha_zone
Default: captcha_zone 设置共享内存。 例如:captcha_zone $binary_remote_addr zone=captcha_zone:10m;
Context: http, server,location
Syntax: captcha_limit_rate
Default: 设置每秒请求最大值。超过后弹出captcha 页面。 captcha_zone 为使用的,共享内存。 例如:captcha_limit_rate captcha_zone 1;
Context: http, server,location
Syntax: captcha_expire
Default: 设置超时时间。
Context: http, server,location
Syntax: captcha_secret
Default: 生成cookie 的密钥。
Context: http, server,location
Syntax: captcha_redirect_uri_file
Default: captcha 页面模板
Context: http, server,location
Syntax: captcha_session_zone
Default: captcha session zone 设置。 captcha_session_zone session_zone 10m;
Context: http, server,location

4. 配置说明

helper broker modules/njt_helper_broker_module.so conf/mqtt.conf; 
helper ctrl modules/njt_helper_ctrl_module.so conf/ctrl.conf;

load_module modules/njt_http_captcha_module.so;  


user  root;
worker_processes auto;

cluster_name helper;
node_name node-u01;

error_log  logs/error.log debug;
pid        logs/njet.pid;

events {
    worker_connections  1024;
}


http {
    dyn_kv_conf conf/iot-work.conf;
    include       mime.types;
    default_type  application/octet-stream;
    variables_hash_max_size   4096;   
    access_log  logs/access.log;

    upstream backend1 { 
         zone backend1_zone 128k;
         server 127.0.0.1:8001;
    }
    upstream backend2{
         zone backend2_zone 128k;
         server 127.0.0.1:8002;
    }
    upstream backend3{
         zone backend2_zone 128k;
         server 127.0.0.1:8003;
    }


   captcha_zone $binary_remote_addr zone=captcha_zone:10m;
   captcha_session_zone  session_zone  10m;
   #captcha_redirect_uri_file  html/app.html;
   server {
          listen  0.0.0.0:8001; 
          location / {
                default_type text/html;
                return 200 "8001 $request_uri";
          }
        }
   server {
         listen  0.0.0.0:90; 
         server_name  server-90;
         captcha_secret  "abc";
         location /upstream {
                captcha_limit_rate  captcha_zone  1;
                default_type text/html;
                proxy_pass http://backend1;
         }


         location =/captcha {
                 default_type text/html;
                 captcha;
         }
      }
}

5. 调用样例

5.1 配置并开启Captcha后,多次访问location,触发captcha并输入验证码

img img

img img