Home > Enterprise >  Nginx lua scripting with nginx code excecution
Nginx lua scripting with nginx code excecution

Time:07-07

hi is it possible to include files with lua to nginx.

for example lua connects to database if the value for brotli is on then it enables brotli and imports its config file.

if (brotli = true) {
  brotli on;
  include /etc/djsfh/ll.conf
}

CodePudding user response:

No. Nginx config isn't an executable script but a declarative configuration file (well, maybe except the rewrite request processing phases NGX_HTTP_SERVER_REWRITE/NGX_HTTP_REWRITE and execution of the directives from the rewrite module). An interpretation of the configuration occurs only once during the nginx startup. Depending on your configuration, one of the possible workarounds can be conditional jump to one of two named locations, first using brotli configuration and second not using it:

rewrite_by_lua_block {
    local use_brotli = ... -- get the true/false value from database
    local jumpto = use_brotli and '@use_brotli' or '@dont_use_brotli'
    ngx.exec(jumpto)
}
location @use_brotli {
    brotli on;
    include /etc/djsfh/ll.conf
    ...
}
location @dont_use_brotli {
    ...
}

Depending on the configuration you are actually using, this example may require additional modifications.

  • Related