Guest User

Untitled

a guest
Sep 5th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. default.conf
  2.  
  3. location /proxy {
  4.  
  5. proxy_pass http://127.0.0.1:8001/;
  6. rewrite_by_lua_file ./lua/set_by_file.lua;
  7. }
  8.  
  9. ######### lua/set_by_file.lua
  10. local pretty = require "resty.prettycjson"
  11.  
  12. function getval(v, def)
  13. if v == nil then
  14. return def
  15. end
  16. return v
  17. end
  18.  
  19. local data = {request={}, response={}}
  20.  
  21. local req = data["request"]
  22. local resp = data["response"]
  23. req["host"] = ngx.var.host
  24. req["uri"] = ngx.var.uri
  25. req["headers"] = ngx.req.get_headers()
  26. req["time"] = ngx.req.start_time()
  27. req["method"] = ngx.req.get_method()
  28. req["get_args"] = ngx.req.get_uri_args()
  29.  
  30. req["body"] = ngx.var.request_body
  31.  
  32. content_type = getval(ngx.var.CONTENT_TYPE, "")
  33.  
  34.  
  35. resp["headers"] = ngx.resp.get_headers()
  36. resp["status"] = ngx.status
  37. resp["duration"] = ngx.var.upstream_response_time
  38. resp["time"] = ngx.now()
  39. resp["body"] = ngx.var.response_body
  40.  
  41. local hasilnya = pretty({data})
  42.  
  43.  
  44.  
  45. local redis = require "resty.redis"
  46. local red = redis:new()
  47.  
  48. red:set_timeout(1000) -- 1 sec
  49.  
  50. -- or connect to a unix domain socket file listened
  51. -- by a redis server:
  52. -- local ok, err = red:connect("unix:/path/to/redis.sock")
  53.  
  54. local ok, err = red:connect("127.0.0.1", 6379)
  55. if not ok then
  56. ngx.say("failed to connect: ", err)
  57. return
  58. end
  59.  
  60. ok, err = red:set("dog", pretty({data}))
  61. if not ok then
  62. ngx.say("failed to set dog: ", err)
  63. return
  64. end
  65.  
  66. ngx.say("set result: ", ok)
Advertisement
Add Comment
Please, Sign In to add comment