Nginx не может разрешить IP-адрес с помощью плагина lua

Я настроил nginx в качестве прокси-сервера. Он должен в основном пересылать HTTP-URL на определенный IP-адрес Ниже моя конфигурация

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
server {
        listen 8080;
        location ~ ^/db/([-_a-zA-Z0-9/]+)/series {
set $token $1 ;
            set $upstream1 " ";
            content_by_lua 'length = string.len(ngx.var.token)
                if length < 8 then 
                    ngx.say("Invalid token (less than 8 characters)")
                    return
                end
                local count = 0
                for i=1,8 do 
                    count = count + string.byte(ngx.var.token,i)
                end
              in_server = {
                    [0] = "10.0.0.1:8086",
                    [1] = "10.0.0.2:8086",
                    [2] = "10.0.0.3:8086",
                    [3] = "10.0.0.4:8086",
                    [4] = "10.0.0.5:8086",
                    [5] = "10.0.0.6:8086",
                    [6] = "10.0.0.7:8086",
                    [7] = "10.0.0.8:8086" 
            }
            ngx.var.upstream1 = in_server[count%7]
';
            proxy_pass http://$upstream1;
        }

    }
}

Переменная восходящего потока устанавливается на IP-адрес в зависимости от типа токена. Логика звуковая, я тестировал в lua отдельно. Но каждый раз, когда я запрашиваю сервер nginx, я получаю следующую ошибку:

2016/05/09 17:20:20 [error] 32680#0: *1 no resolver defined to resolve  , client: 127.0.0.1, server: , request: "GET /db/rustytoken/series?&q=select%20%2A%20from%20foo%20limit%201 HTTP/1.1", host: "localhost:8080"

Я не уверен, зачем ему нужен распознаватель, если я посылаю прямой IP-адрес. В любом случае, я добавил следующее в директиву местоположения

resolver 127.0.0.1 

И установил dnsmasq для разрешения доменных имен. Это все еще не могло. Вместо этого я получаю следующую ошибку.

2016/05/09 17:14:22 [error] 32030#0: *1   could not be resolved (3: Host not found), client: 127.0.0.1, server: , request: "GET /db/rustytoken/series?q=select%20%2A%20from%20foo%20limit%201 HTTP/1.1", host: "localhost:8080"

2 ответа

Вы должны использовать "rewrite_by_lua" вместо "content_by_lua", потому что вы установили $upstream1 " ", поэтому вам нужно переписать его.

You shoud use https://github.com/openresty/lua-nginx-module instead of content_by_lua. See some examples here https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/balancer.md

Nginx config is not executed linearly. Here is the best tutorial I know - http://openresty.org/download/agentzh-nginx-tutorials-en.html

Обновить

Другое возможное решение:

set_by_lua_block $upstream1 {
       length = string.len(ngx.var.token)
       if length < 8 then 
             -- cannot use this API here, you should add error processing
             --ngx.say("Invalid token (less than 8 characters)")
             return
       end
       local count = 0
       for i=1,8 do 
            count = count + string.byte(ngx.var.token,i)
       end
       in_server = {
            [0] = "10.0.0.1:8086",
            [1] = "10.0.0.2:8086",
            [2] = "10.0.0.3:8086",
            [3] = "10.0.0.4:8086",
            [4] = "10.0.0.5:8086",
            [5] = "10.0.0.6:8086",
            [6] = "10.0.0.7:8086",
            [7] = "10.0.0.8:8086" 
       }
       return in_server[count%7]
}
Другие вопросы по тегам