Can't access ngx.var in rewrite_by_lua_block

Viewed 1307

I'm trying to set the value to nginx variable, but getting an error:

[error] 47#47: *382 lua entry thread aborted: runtime error: rewrite_by_lua(myconf.conf:23):2: attempt to index field 'var' (a nil value)
stack traceback:
coroutine 0:
        rewrite_by_lua(myconf.conf:23): in function <rewrite_by_lua(myconf.conf:23):1>, client: ip, server: *.host, request: "GET / HTTP/1.1", host: "host"

The part of my config:

        set $upstream_new '';
        rewrite_by_lua_block {
        ngx.var.upstream_new = "8.8.8.8";
}

I'm running nginx in docker if it matters. Compiled with:

 --prefix=/var/www/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin --modules-path=/usr/lib/nginx/modules --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/tmp/nginx.pid --http-client-body-temp-path=/var/cache/nginx/body --http-fastcgi-temp-path=/var/cache/nginx/fastcgi --http-proxy-temp-path=/var/cache/nginx/proxy --http-scgi-temp-path=/var/cache/nginx/scgi --http-uwsgi-temp-path=/var/cache/nginx/uwsgi --with-pcre --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_degradation_module --with-http_v2_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_slice_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-mail --with-mail_ssl_module --with-threads --with-file-aio --with-compat --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --add-dynamic-module=/usr/src/nginx/ngx_devel_kit-0.3.1 --add-module=/usr/src/nginx/ngx_cache_purge-2.3 --add-dynamic-module=/usr/src/nginx/nginx_upstream_check_module-master --add-dynamic-module=/usr/src/nginx/lua-nginx-module-0.10.16rc5 --add-dynamic-module=/usr/src/nginx/headers-more-nginx-module-0.33 --add-dynamic-module=/usr/src/nginx/ngx_brotli --add-dynamic-module=/usr/src/nginx/ngx_http_auth_pam_module-1.5.1 --add-dynamic-module=/usr/src/nginx/echo-nginx-module-0.62rc1 --add-dynamic-module=/usr/src/nginx/ngx-fancyindex-0.4.4 --add-dynamic-module=/usr/src/nginx/nginx-dav-ext-module-3.0.0 --add-dynamic-module=/usr/src/nginx/ngx_http_substitutions_filter_module-0.6.4 --add-dynamic-module=/usr/src/nginx/nginx-upload-progress-module-0.9.2 --add-dynamic-module=/usr/src/nginx/nchan-1.2.7
1 Answers
  1. It is possible that the compilation was not properly done. Try this post and compile accordingly again with adding all your modules.
  2. If you're using Docker then this is OpenResty with Naxsi module compilation. You can safely add your modules and remove unwanted ones.
  3. Check using nginx -V for all the compilation flags and if you see all the Lua modules.

I tried with following blocks on OpenResty and working properly.

set $upstream_new '';
rewrite_by_lua_block {
    ngx.var.upstream_new = "8.8.8.8";
}

and

set $upstream_new '';
access_by_lua '
    ngx.var.upstream_new = "8.8.8.8";
';

agentzh's Nginx Tutorials might be helpful if you're new.

Related