Logging content_by_lua_block into file

438 views
Skip to first unread message

Rajeshwer rao

unread,
Aug 16, 2018, 1:22:44 AM8/16/18
to openresty-en
how do i log  content_by_lua_block into file

events {
    worker_connections 1024;
}

http {
    default_type 'text/plain';
    # maximum allowed size of the client request body. By default this is 1m.
    # Request with bigger bodies nginx will return error code 413.
    # http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
    client_max_body_size 10m;

    log_format main  '$remote_addr $http_x_forwarded_for - $remote_user '
                      '[$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $request_time';
    server {
        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log;
        # please check the benefits of reuseport https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1
        # basically instructs to create an individual listening socket for each worker process (using the SO_REUSEPORT
        # socket option), allowing a kernel to distribute incoming connections between worker processes.
        listen 8080 default_server reuseport;

        # Replace '_' with your hostname.
        server_name _;

        location / {
            lua_need_request_body on;
            content_by_lua_block {
                ngx.say("CLIENT VALUES:")
                ngx.say("client_address=", ngx.var.remote_addr)
                ngx.say("command=", ngx.req.get_method())
                ngx.say("real path=", ngx.var.request_uri)
                ngx.say("query=", ngx.var.query_string)
                ngx.say("request_version=", ngx.req.http_version())
                ngx.say("request_uri=", ngx.var.scheme.."://"..ngx.var.host..":"..ngx.var.server_port..ngx.var.request_uri)
                ngx.say("")

                ngx.say("SERVER VALUES:")
                ngx.say("server_version=", "nginx: "..ngx.var.nginx_version.." - lua: "..ngx.config.ngx_lua_version)
                ngx.say("")

                ngx.say("HEADERS RECEIVED:")
                local headers = ngx.req.get_headers()
                local keys = {}
                for key, val in pairs(headers) do
                    table.insert(keys, key)
                end

                table.sort(keys)
                for i, key in ipairs(keys) do
                    ngx.say(key, "=", headers[key])
                end

                ngx.say("BODY:")
                ngx.print(ngx.var.request_body or "-no body in request-")
            }
        }
    }
}

Kwanhur Huang

unread,
Aug 22, 2018, 9:33:43 PM8/22/18
to openre...@googlegroups.com
hello,

ngx.log can do it and cooperate with error_log directive.

or log into file with the I/O library .


Kwanhur Huang
TL;DR

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages