Nginx: unknown directive “ngx.flush(true)”

luanginx

I'm having some trouble by using the nginx devel (ndk) and lua-module. I compiled nginx-rpm using the following configuration:

./configure \
        --prefix=%{_sysconfdir}/nginx/ \
        --sbin-path=%{_sbindir}/nginx \
        --conf-path=%{_sysconfdir}/nginx/nginx.conf \
        --error-log-path=%{_localstatedir}/log/nginx/error.log \
        --http-log-path=%{_localstatedir}/log/nginx/access.log \
        --pid-path=%{_localstatedir}/run/nginx.pid \
        --lock-path=%{_localstatedir}/run/nginx.lock \
        --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
        --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
        --user=%{nginx_user} \
        --group=%{nginx_group} \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gzip_static_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_stub_status_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-file-aio \
        --with-ipv6 \
        --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
        --add-module=%{_builddir}/nginx-%{version}/mods/upload_progress \
    --add-module=%{_builddir}/nginx-%{version}/mods/ngx_devel_kit \
    --add-module=%{_builddir}/nginx-%{version}/mods/lua-nginx-module \

Calling nginx -V after the installation also shows that ngx and lua seems to be installed/activated. But… when I do the following:

location /abc/ {
    # For demonstration purposes only...
    ngx.flush(true);
    expires 30d;
}

I always get the following error:

nginx: [emerg] unknown directive "ngx.flush(true)" in /etc/nginx/conf.d/default.conf:53
nginx: configuration file /etc/nginx/nginx.conf test failed

What's wrong with my configuration? Is there anything I need to activate to use ngx-directives in the conf-file?

Thanks in advance for any suggestion!

Best Answer

ngx.flush() is one of NginxLuaModule's Lua functions, not an nginx configuration directive.
To achieve the behaviour you seem to be desiring (just flush the content), do this:

location /abc/ {
    content_by_lua '
        ngx.flush(true);
    ';
    expires 30d;
}

You have to wrap Lua code into one of the *_by_lua directives, or load the code from a file using one of the *_by_lua_file directives in your nginx config.
Lua code can be executed in different contexts, e.g. to set a variable (set_by_lua), at rewrite state (rewrite_by_lua), to serve content (content_by_lua), or others.

You should have a look at the nginx wiki page.

Note, that every context of executing Lua code is designed to perform different tasks and runs at different times while processing and serving a request.
This makes it practically impossible to explain code behaviour, code requirements, or available functions in a generic way