server { server_name {{ API_SERVER_NAME }}; # HTTP listen listen 0.0.0.0:{{ API_HTTP_PORT }}; listen 127.0.0.1:{{ API_HTTP_PORT }}; # maximum body size for API client_max_body_size 1G; # default mime type is JSON default_type 'application/json'; # check IP and do the API call access_by_lua_block { local api = require "api" local logger = require "logger" if not ngx.var.http_host or ngx.var.http_host ~= "{{ API_SERVER_NAME }}" then logger.log(ngx.WARN, "API", "Wrong Host header from IP " .. ngx.var.remote_addr) return ngx.exit(ngx.HTTP_CLOSE) end local ok, err = api:is_allowed_ip() if not ok then logger.log(ngx.WARN, "API", "Can't validate access from IP " .. ngx.var.remote_addr .. " : " .. err) return ngx.exit(ngx.HTTP_CLOSE) end logger.log(ngx.NOTICE, "API", "Validated access from IP " .. ngx.var.remote_addr) local ok, err, status, resp = api:do_api_call() if not ok then logger.log(ngx.WARN, "API", "Call from " .. ngx.var.remote_addr .. " on " .. ngx.var.uri .. " failed : " .. err) else logger.log(ngx.NOTICE, "API", "Successful call from " .. ngx.var.remote_addr .. " on " .. ngx.var.uri .. " : " .. err) end ngx.status = status ngx.say(resp) return ngx.exit(status) } }