简体   繁体   中英

Nginx lua scripting with nginx code excecution

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
}

No. Nginx config isn't an executable script but a declarative configuration file (well, maybe except the rewriterequest 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM