• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

用header_filter_by_lua设置自定义HTTP头

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

header_filter_by_lua的说明:

 

 
  1. header_filter_by_lua

  2.  
  3. syntax: header_filter_by_lua <lua-script-str>

  4.  
  5. context: http, server, location, location if

  6.  
  7. phase: output-header-filter

  8.  
  9. WARNING Since the v0.9.17 release, use of this directive is discouraged; use the new header_filter_by_lua_block directive instead.

  10.  
  11. Uses Lua code specified in <lua-script-str> to define an output header filter.

  12.  
  13. Note that the following API functions are currently disabled within this context:

  14.  
  15. Output API functions (e.g., ngx.say and ngx.send_headers)

  16. Control API functions (e.g., ngx.redirect and ngx.exec)

  17. Subrequest API functions (e.g., ngx.location.capture and ngx.location.capture_multi)

  18. Cosocket API functions (e.g., ngx.socket.tcp and ngx.req.socket).

  19. Here is an example of overriding a response header (or adding one if absent) in our Lua header filter:

  20.  
  21. location / {

  22. proxy_pass http://mybackend;

  23. header_filter_by_lua 'ngx.header.Foo = "blah"';

  24. }

  25. This directive was first introduced in the v0.2.1rc20 release.

 

官网:https://github.com/openresty/lua-nginx-module#header_filter_by_lua

 

作用:

a)新增自定义的HTTP头

b)重新设置upstream返回的HTTP头,也就是起到覆盖作用

 

使用示范源码:nginx.conf

 

 
  1. worker_processes 1;

  2. error_log logs/error.log;

  3. events {

  4. worker_connections 1024;

  5. }

  6.  
  7. http {

  8.  
  9. log_format main '$msec $status $request $request_time '

  10. '$http_referer $remote_addr [ $time_local ] '

  11. '$upstream_response_time $host $bytes_sent '

  12. '$request_length $upstream_addr';

  13.  
  14. access_log logs/access.log main buffer=32k flush=1s;

  15.  
  16. upstream remote_world {

  17. server 127.0.0.1:8080;

  18. }

  19.  
  20. server {

  21. listen 80;

  22.  
  23. location /exec {

  24. content_by_lua '

  25. local cjson = require "cjson"

  26. local headers = {

  27. ["Etag"] = "663b92165e216225df78fbbd47c9c5ba",

  28. ["Last-Modified"] = "Fri, 12 May 2016 18:53:33 GMT",

  29. }

  30. ngx.var.my_headers = cjson.encode(headers)

  31. ngx.var.my_upstream = "remote_world"

  32. ngx.var.my_uri = "/world"

  33. ngx.exec("/upstream")

  34. ';

  35. }

  36.  
  37. location /upstream {

  38. internal;

  39.  
  40. set $my_headers $my_headers;

  41. set $my_upstream $my_upstream;

  42. set $my_uri $my_uri;

  43. proxy_pass http://$my_upstream$my_uri;

  44.  
  45. header_filter_by_lua '

  46. local cjson = require "cjson"

  47. headers = cjson.decode(ngx.var.my_headers)

  48. for k, v in pairs(headers) do

  49. ngx.header[k] = v

  50. end

  51. ';

  52. }

  53. }

  54.  
  55.  
  56. server {

  57. listen 8080;

  58.  
  59. location /world {

  60. echo "hello world";

  61. }

  62. }

  63. }

 

 

运行:openresty -p `pwd` -c conf/nginx.conf

 

运行结果:

 

原文出自:http://blog.csdn.net/daiyudong2020/article/details/53149242


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cocos2d-xlua加密发布时间:2022-07-22
下一篇:
lua脚本简介发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap