跳转到内容

User:Ebayboy

出自维基百科,自由个百科全书

用lua脚本做nginx的访问的限制

第一步: 配置文件http server部分的内容:

server {
               server_name va;
               listen 192.168.12.238:80 backlog=8192;
               client_header_timeout 60;
               location / {
                       set $sino_url $scheme://$host$request_uri;
                       set $TRUE 1;
                       proxy_http_version 1.1;
                       proxy_set_header Connection "Keep-Alive";
                       proxy_set_header Host $http_host;
                       proxy_pass http://va-pa;
                       client_body_timeout 60;
                       keepalive_timeout 75;
                       lingering_timeout 5;
                       proxy_connect_timeout 60;
                       proxy_read_timeout 60;
                       proxy_send_timeout 60;
                       client_max_body_size 1m;
               }
               location /lua {
                       default_type text/plain;
                       content_by_lua 'ngx.say("hello world")';
               }
               location @client{
                       #proxy_pass  http://www.baidu.com
                       proxy_pass  http://2.3.4.151:8080;
               }
               location ~  /test {
                       default_type  text/html;
                       content_by_lua 'ngx.say("this is  ruifengyun.com!")';
                       access_by_lua '
                               ngx.log(ngx.ERR, "111111111111");
                               if ngx.var.remote_addr == "192.168.12.240" then
                                       ngx.log(ngx.ERR, "22222222222222");
                                       ngx.exit(ngx.HTTP_FORBIDDEN)
                           end
                               if ngx.var.remote_addr == "192.168.12.231" then
                                       ngx.log(ngx.ERR, "3333333333333");
                                       ngx.exec("@client")
                               end
                       ';
               }
       }

第二步: 2.3.4.151上的test.html内容如下:

test.html 2.3.4.151

第三步: 在client(192.168.12.231)上访问vserver http://192.168.12.238/test.html

第四步: 返回的HTML页面显示: test.html 2.3.4.151

第五步: 查看NG日志文件smartl7.log, 也同样可以证明请求的走向。 2016/02/24 18:11:04 [error] 1692#0: *22 [lua] access_by_lua(smartl7.conf:79):2: 111111111111, client: 192.168.12.231, server: va, request: "GET /test.html HTTP/1.1", host: "192.168.12.238" 2016/02/24 18:11:04 [error] 1692#0: *22 [lua] access_by_lua(smartl7.conf:79):8: 3333333333333, client: 192.168.12.231, server: va, request: "GET /test.html HTTP/1.1", host: "192.168.12.238"

第六步: 使用client(192.168.12.240)访问VS 192.168.12.238

第七步:(可见访问被禁止了) 返回的页面是: [root@localhost conf]# wget http://192.168.12.238/test.html --2016-02-27 14:24:30-- http://192.168.12.238/test.html Connecting to 192.168.12.238:80... connected. HTTP request sent, awaiting response... 403 Forbidden 2016-02-27 14:24:30 ERROR 403: Forbidden.

第八步: 查看日志分析 2016/02/24 18:14:11 [error] 1692#0: *25 [lua] access_by_lua(smartl7.conf:79):2: 111111111111, client: 192.168.12.240, server: va, request: "GET /test.html HTTP/1.0", host: "192.168.12.238" 2016/02/24 18:14:11 [error] 1692#0: *25 [lua] access_by_lua(smartl7.conf:79):4: 22222222222222, client: 192.168.12.240, server: va, request: "GET /test.html HTTP/1.0", host: "192.168.12.238"

打出2222222, 可以了解到lua脚本确实启动对192.168.12.240的访问进行了禁止。