Pular para conteúdo

woothee: Implementação do módulo Lua-nginx-module-lua do Woothee

Instalação

Se você ainda não configurou a assinatura do repositório RPM, inscreva-se. Depois, você pode prosseguir com os seguintes passos.

CentOS/RHEL 7 ou Amazon Linux 2

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install lua-resty-woothee

CentOS/RHEL 8+, Fedora Linux, Amazon Linux 2023

dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-woothee

Para usar esta biblioteca Lua com o NGINX, certifique-se de que o nginx-module-lua esteja instalado.

Este documento descreve o lua-resty-woothee v1.12.0 lançado em 13 de outubro de 2021.


CircleCI

Woothee lua resty

A implementação Lua-Openresty do Projeto Woothee, que é um parser de strings de user-agent multilíngue.

https://github.com/woothee/woothee

Sinopse

Uso Básico

server {
    location /test {
        content_by_lua_block {
            local woothee = require "resty.woothee"

            -- parse
            local r = woothee.parse(ngx.var.http_user_agent)
            --  => {"name": "xxx", "category": "xxx", "os": "xxx", "version": "xxx", "vendor": "xxx"}

            -- crawler?
            local crawler = woothee.is_crawler(ngx.var.http_user_agent)
            --  => true

            ngx.header.content_type = "text/plain"
            ngx.say(r.name)
        }
    }
}

Incluir Log do Nginx

log_format woothee_format
                  '$remote_addr - $remote_user [$time_local] '
                  '"$request" $status $body_bytes_sent '
                  '"$http_referer" "$http_user_agent" '
                  '"$x_wt_name" "$x_wt_category" "$x_wt_os" "$x_wt_version" "$x_wt_vendor" "$x_wt_os_version"'
                  ;

server {

    access_log /var/log/nginx/nginx-access-woothee.log woothee_format;

    # set nginx valiables
    set $x_wt_name       '-';
    set $x_wt_category   '-';
    set $x_wt_os         '-';
    set $x_wt_version    '-';
    set $x_wt_vendor     '-';
    set $x_wt_os_version '-';

    location /test {
        content_by_lua_block {
            local woothee = require "resty.woothee"
            local r = woothee.parse(ngx.var.http_user_agent)
            -- set nginx valiables
            ngx.var.x_wt_name       = r.name
            ngx.var.x_wt_category   = r.category
            ngx.var.x_wt_os         = r.os
            ngx.var.x_wt_version    = r.version
            ngx.var.x_wt_vendor     = r.vendor
            ngx.var.x_wt_os_version = r.os_version

            ngx.header.content_type = "text/plain"
            ngx.say(r.name)
        }
    }
}

Encaminhar Servidor Backend

server {

    # set nginx valiables
    set $x_wt_name       '-';
    set $x_wt_category   '-';
    set $x_wt_os         '-';
    set $x_wt_version    '-';
    set $x_wt_vendor     '-';
    set $x_wt_os_version '-';

    location /test {
        rewrite_by_lua_block {
            local woothee = require "resty.woothee"
            local r = woothee.parse(ngx.var.http_user_agent)
            -- set nginx valiables
            ngx.var.x_wt_name       = r.name
            ngx.var.x_wt_category   = r.category
            ngx.var.x_wt_os         = r.os
            ngx.var.x_wt_version    = r.version
            ngx.var.x_wt_vendor     = r.vendor
            ngx.var.x_wt_os_version = r.os_version
        }

        proxy_pass http://backend-server/;
        # proxy set header
        proxy_set_header X-WT-NAME       $x_wt_name;
        proxy_set_header X-WT-CATEGORY   $x_wt_category;
        proxy_set_header X-WT-OS         $x_wt_os;
        proxy_set_header X-WT-VERSION    $x_wt_version;
        proxy_set_header X-WT-VENDOR     $x_wt_vendor;
        proxy_set_header X-WT-OS-VERSION $x_wt_os_version;
    }
}

Para Desenvolvedores (no Docker)

docker run & executar teste

make local-all

GitHub

Você pode encontrar dicas adicionais de configuração e documentação para este módulo no repositório GitHub do nginx-module-woothee.