lua_package_path '/etc/jsonpath/?.lua;;'; access_log /var/log/nginx_access.log; error_log /var/log/nginx_error.log info; init_by_lua_block { jp = require("jsonpath") jp.init("/etc/jsonpath/libjsonpath_lib.so") BOOK = "$..book" jp.compile(BOOK) COMMENT_COUNT = "$..commit[?(@.author.name == 'Thibault Charbonnier')]" jp.compile(COMMENT_COUNT) } server { listen 80; server_name localhost; gzip on; gzip_types text/plain application/json; #gzip_comp_level 6; #gzip_vary on; location ~ \.json { add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'; expires off; default_type 'text/plain'; root /etc/jsonpath/example; } location /1 { default_type text/plain; content_by_lua_file /etc/jsonpath/testa.lua; } location /2 { # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding proxy_set_header Accept-Encoding "*"; default_type 'text/plain'; proxy_pass 'http://localhost/example.json'; header_filter_by_lua_block { ngx.header.content_length = nil } body_filter_by_lua_block { local chunk, eof = ngx.arg[1], ngx.arg[2] local buf = ngx.ctx.buf if eof then if buf then local json = buf .. chunk local template = jp.compile(BOOK) local result = template(json) ngx.arg[1] = result return end return end if buf then ngx.ctx.buf = buf .. chunk else ngx.ctx.buf = chunk end ngx.arg[1] = nil } } location /3 { # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding proxy_set_header Accept-Encoding "*"; default_type 'text/plain'; proxy_pass 'https://api.github.com/repos/openresty/lua-nginx-module/commits'; header_filter_by_lua_block { ngx.header.content_length = nil } body_filter_by_lua_block { local chunk, eof = ngx.arg[1], ngx.arg[2] local buf = ngx.ctx.buf if eof then if buf then local json = buf .. chunk local template = jp.compile(COMMENT_COUNT) local result = template(json) ngx.arg[1] = result return end return end if buf then ngx.ctx.buf = buf .. chunk else ngx.ctx.buf = chunk end ngx.arg[1] = nil } } }