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

 

运行结果:

用header_filter_by_lua设置自定义HTTP头

 

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

相关文章:

  • 2021-04-17
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-04-05
猜你喜欢
  • 2021-12-20
  • 2021-07-20
  • 2022-12-23
  • 2021-07-09
  • 2021-11-28
  • 2022-12-23
  • 2021-11-28
相关资源
相似解决方案