Kong / kong

🦍 The Cloud-Native API Gateway and AI Gateway.

Home Page:https://konghq.com/install/#kong-community

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I configure the lua_shared_dict to the nginx-kong.conf

svnshing opened this issue · comments

I want to use the lua_shared_dict in my plugin,but I can not add lua_shared_dict profile in the nginx-kong.conf~How can I do it?

https://github.com/Kong/kong/blob/master/kong/templates/nginx_kong.lua#L31 All these are the declared shared dictionaries kong has by default. You can add as many as you want in that file prior to starting up Kong.

Not sure if Kong has introduced a parameter in the conf yet to define custom shared memory zones.

commented

You can use Nginx directive injection feature to inject a shared dict into the Nginx conf file of Kong:

nginx_http_lua_shared_dict=my_dict_name 1m

Hello @hbagdi.

Is it possible to inject Nginx directives from a plugin ? Asking because of this.
I am developing a public plugin and I would like to not make the user edit their nginx directives.

I found an ugly workaround using environment variables : export KONG_NGINX_HTTP_LUA_SHARED_DICT="this_dict 20k", but that is still an extra step for the plugin users to do.

BTW, I love your work, guys !

commented

I found an ugly workaround using environment variables : export KONG_NGINX_HTTP_LUA_SHARED_DICT="this_dict 20k", but that is still an extra step for the plugin users to do.

@Ilshidur As of today, this is the only way to add a shared dict. Agree that it requires an extra step but should be required only once while setting up configuration for Kong.

I'm going to close this as the original question is answered.
Please re-open if needed.

nginx_http_lua_shared_dict=my_dict_name 1m

Hi,
How'd one share more than one lua_shared_dict?

commented

You can include a file with the include directive, which has a number of shared dicts.

You can set this env var KONG_NGINX_HTTP_LUA_SHARED_DICT: introspection 10m

Adding for reference; recently saw someone add multiple dicts like this;

nginx_http_lua_shared_dict=my_dict_name 1m; lua_shared_dict your_dict_name 1m

EDIT: fixed as correctly identified by @bforbis below

@Tieske I am unable to configure kong with multiple shared dicts that way:

My docker-compose:

environment:
  KONG_NGINX_HTTP_LUA_SHARED_DICT: "discovery 1m; jwks 1m; introspection 1m;"

The output I get is:

kong_1     | Error: could not prepare Kong prefix at /usr/local/kong: nginx configuration is invalid (exit code 1):
kong_1     | nginx: [emerg] unknown directive "jwks" in /usr/local/kong/nginx-kong.conf:36
kong_1     | nginx: configuration file /usr/local/kong/nginx.conf test failed

It looks like this env variable will inject the directive into nginx conf as:

lua_shared_dict discovery 1m; jwks 1m; introspection 1m;;

So a hacky workaround to this would be to do the following:

environment:
  KONG_NGINX_HTTP_LUA_SHARED_DICT: "discovery 1m; lua_shared_dict jwks 1m; lua_shared_dict introspection 1m"

which will then generate the following nginx.conf line

lua_shared_dict discovery 1m; lua_shared_dict jwks 1m; lua_shared_dict introspection 1m;

👍 @bforbis thx for the feedback, I updated my post above

@Tieske, to be extra clear, you need to add lua_shared_dict directive before each additional shared dict name. The way you have it right now, the generated directive would be:

lua_shared_dict my_dict_name 1m; your_dict_name 1m

This does not compile, since "your_dict_name" is not a known nginx directive.