标签 - LUA

LUA Openresty    2019-02-27 12:01:44    86    0    0

本文的一个例子主要介绍如何利用redis实现对请求头中User-Agent修改已达到路由到两个不同的http后台的目的。这个例子使用了这些openresty组件: Redis2 Nginx Module, Lua Nginx Module, Lua Redis Parser Library, 和 Set Misc Nginx Module。
1. 配置nginx
下面是完整的nginx.conf

worker_processes  1;
error_log logs/error.log info;

events {
    worker_connections 1024;
}

http {
    upstream apache.org {
        server apache.org;
    }

        upstream nginx.org {
        server nginx.org;
    }

    server {
        listen 8080;

        location = /redis {
            internal;
            set_unescape_uri $key $arg_key;
            redis2_query get $key;
            redis2_pass 127.0.0.1:6379;
        }

        location / {
            set $target '';
            access_by_lua '
            local key = ngx.var.http_user_agent
            local res = ngx.location.capture(
            "/redis", {
                args = { key = key }
            }
        )

        print("key: ", key)

        if res.status ~= 200 then
            ngx.log(ngx.ERR, "redis server re