Fail to execute require "ngx.balancer"

268 views
Skip to first unread message

Peng Liu

unread,
Aug 24, 2016, 4:22:41 AM8/24/16
to openresty-en
Hi,

I try to use balancer_by_lua and https://github.com/agentzh/lua-resty-balancer to implement my own load balancer with session sticky enabled. In my nginx.conf, the lua part is as follow:

    lua_package_path "/home/admin/nginx_src/lua-resty-lrucache/lib/?.lua;/home/admin/nginx_src/lua-resty-core/lib/?.lua;/home/admin/nginx_src/lua-resty-ba    lancer/lib/?.lua;;";
    lua_package_cpath "/home/admin/nginx_src/lua-resty-balancer/?.so;;";

    balancer_by_lua_block {
         local b = require "ngx.balancer"
         ***************
    }

Then I get the error message in error.log:

2016/08/24 16:00:36 [error] 4438#4438: *1 failed to run balancer_by_lua*: balancer_by_lua:2: module 'ngx.balancer' not found:
        no field package.preload['ngx.balancer']
        no file '/home/admin/nginx_src/lua-resty-lrucache/lib/ngx/balancer.lua'
        no file '/home/admin/nginx_src/lua-resty-core/lib/ngx/balancer.lua'
        no file '/home/admin/nginx_src/lua-resty-balancer/lib/ngx/balancer.lua'
        no file './ngx/balancer.lua'
        no file '/usr/local/share/luajit-2.1.0-beta2/ngx/balancer.lua'
        no file '/usr/local/share/lua/5.1/ngx/balancer.lua'
        no file '/usr/local/share/lua/5.1/ngx/balancer/init.lua'
        no file '/home/admin/nginx_src/lua-resty-balancer/ngx/balancer.so'
        no file './ngx/balancer.so'
        no file '/usr/local/lib/lua/5.1/ngx/balancer.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file '/home/admin/nginx_src/lua-resty-balancer/ngx.so'
        no file './ngx.so'
        no file '/usr/local/lib/lua/5.1/ngx.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        balancer_by_lua:2: in function <balancer_by_lua:1> while connecting to upstream, client: 15.107.52.43, server: 16.165.217.31, request: "POST /SM/ui HTTP/1.1", host: "16.165.217.31"

And I am sure that file '/home/admin/nginx_src/lua-resty-core/lib/ngx/balancer.lua' exists.

What do I miss?

Thanks

Yichun Zhang (agentzh)

unread,
Aug 24, 2016, 6:06:58 PM8/24/16
to openresty-en
Hello!

On Wed, Aug 24, 2016 at 1:22 AM, Peng Liu wrote:
> And I am sure that file
> '/home/admin/nginx_src/lua-resty-core/lib/ngx/balancer.lua' exists.
>
> What do I miss?

Are you sure that file path is accessible via the system account used
by your nginx worker processes (which is usually "nobody" by default)?
BTW, not only that file needs enough permission, or the directories in
that path should be executable for the nginx workers' system account.

One quick way to getting out of any permission problems is to add the
following lines to your nginx.conf:

init_by_lua_block {
-- preload any Lua modules in the nginx master process during
configuration loading:
require "ngx.balancer"
}

The nginx master process is usually run by a privileged system account
like root :)

Regards,
-agentzh

Peng Liu

unread,
Aug 25, 2016, 12:28:17 AM8/25/16
to openresty-en
Hi, agentzh,

Thanks you. That works. I miss the fact that the Nginx master and worker processes are run by different system account.

Peng Liu

unread,
Aug 25, 2016, 12:55:13 AM8/25/16
to openresty-en
Hi, agentzh,

By the way, does all the lua code is executed by worker process? Or some by master and some by worker process?


On Thursday, August 25, 2016 at 6:06:58 AM UTC+8, agentzh wrote:

Thibault Charbonnier

unread,
Aug 25, 2016, 2:21:56 PM8/25/16
to openre...@googlegroups.com

> By the way, does all the lua code is executed by worker process? Or
> some by master and some by worker process?

By the worker processes, to the exception of the init_by_lua* context.

Cheers,
Thibault
Reply all
Reply to author
Forward
0 new messages