现在的位置: 首页Nginx>正文
FreeBSD下nginx安装lua_nginx_module模块 [原创]
2011年03月30日 Nginx FreeBSD下nginx安装lua_nginx_module模块 [原创]已关闭评论 ⁄ 被围观 13,179 次+

1.先安装lua

  1. fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
  2. tar zxvf lua-5.1.4.tar.gz
  3. cd lua-5.1.4
  4. make freebsd
  5. make install
  6. cd ..

2.安装lua_nginx_module

  1. fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
  2. fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
  3. tar zxvf chaoslawful-lua-nginx-module-v0.1.6rc2-0-gccaf132.zip
  4. mv chaoslawful-lua-nginx-module-ccaf132 nginx-0.8.54/lua_nginx_module
  5. tar zxvf simpl-ngx_devel_kit-v0.2.17-0-gbc97eea.zip
  6. mv simpl-ngx_devel_kit-bc97eea nginx-0.8.54/ngx_devel_kit
  7. cd nginx-0.8.54
  8. ./configure --prefix=/data/soft/nginx0854 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_dav_module --with-http_flv_module --with-http_addition_module --with-http_sub_module --with-pcre=./pcre-8.10 --add-module=./nginx-accesskey-2.0.3 --add-module=./substitutions_filter --add-module=./ngx_devel_kit --add-module=./lua-nginx-module
  9. make && make install

lua_nginx_module模块的使用

  1. # set search paths for pure Lua external libraries (';;' is the default path):
  2. lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';
  3. # set search paths for Lua external libraries written in C (can also use ';;'):
  4. lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
  5. server {
  6. location /inline_concat {
  7. # MIME type determined by default_type:
  8. default_type 'text/plain';
  9. set $a "hello";
  10. set $b "world";
  11. # inline lua script
  12. set_by_lua $res "return ngx.arg[1]..ngx.arg[2]" $a $b;
  13. echo $res;
  14. }
  15. location /rel_file_concat {
  16. set $a "foo";
  17. set $b "bar";
  18. # script path relative to nginx prefix
  19. # $ngx_prefix/conf/concat.lua contents:
  20. #
  21. # return ngx.arg[1]..ngx.arg[2]
  22. #
  23. set_by_lua_file $res conf/concat.lua $a $b;
  24. echo $res;
  25. }
  26. location /abs_file_concat {
  27. set $a "fee";
  28. set $b "baz";
  29. # absolute script path not modified
  30. set_by_lua_file $res /usr/nginx/conf/concat.lua $a $b;
  31. echo $res;
  32. }
  33. location /lua_content {
  34. # MIME type determined by default_type:
  35. default_type 'text/plain';
  36. content_by_lua "ngx.say('Hello,world!')"
  37. }
  38. location /nginx_var {
  39. # MIME type determined by default_type:
  40. default_type 'text/plain';
  41. # try access /nginx_var?a=hello,world
  42. content_by_lua "ngx.print(ngx.var['arg_a'], '\\n')";
  43. }
  44. location /request_body {
  45. # force reading request body (default off)
  46. lua_need_request_body on;
  47. content_by_lua 'ngx.print(ngx.var.request_body)';
  48. }
  49. # transparent non-blocking I/O in Lua via subrequests
  50. location /lua {
  51. # MIME type determined by default_type:
  52. default_type 'text/plain';
  53. content_by_lua '
  54. local res = ngx.location.capture("/some_other_location")
  55. if res.status == 200 then
  56. ngx.print(res.body)
  57. end';
  58. }
  59. # GET /recur?num=5
  60. location /recur {
  61. # MIME type determined by default_type:
  62. default_type 'text/plain';
  63. content_by_lua '
  64. local num = tonumber(ngx.var.arg_num) or 0
  65. ngx.say("num is: ", num)
  66. if num > 0 then
  67. res = ngx.location.capture("/recur?num=" .. tostring(num - 1))
  68. ngx.print("status=", res.status, " ")
  69. ngx.print("body=", res.body)
  70. else
  71. ngx.say("end")
  72. end
  73. ';
  74. }
  75. location /foo {
  76. rewrite_by_lua '
  77. res = ngx.location.capture("/memc",
  78. { args = { cmd = 'incr', key = ngx.var.uri } }
  79. )
  80. ';
  81. proxy_pass http://blah.blah.com;
  82. }
  83. location /blah {
  84. access_by_lua '
  85. local res = ngx.location.capture("/auth")
  86. if res.status == ngx.HTTP_OK then
  87. return
  88. end
  89. if res.status == ngx.HTTP_FORBIDDEN then
  90. ngx.exit(res.status)
  91. end
  92. ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  93. ';
  94. # proxy_pass/fastcgi_pass/postgres_pass/...
  95. }
  96. location /mixed {
  97. rewrite_by_lua_file /path/to/rewrite.lua;
  98. access_by_lua_file /path/to/access.lua;
  99. content_by_lua_file /path/to/content.lua;
  100. }
  101. # use nginx var in code path
  102. # WARN: contents in nginx var must be carefully filtered,
  103. # otherwise there'll be great security risk!
  104. location ~ ^/app/(.+) {
  105. content_by_lua_file /path/to/lua/app/root/$1.lua;
  106. }
  107. location / {
  108. lua_need_request_body on;
  109. client_max_body_size 100k;
  110. client_body_in_single_buffer on;
  111. access_by_lua '
  112. -- check the client IP addr is in our black list
  113. if ngx.var.remote_addr == "132.5.72.3" then
  114. ngx.exit(ngx.HTTP_FORBIDDEN)
  115. end
  116. -- check if the request body contains bad words
  117. if ngx.var.request_body and
  118. string.match(ngx.var.request_body, "fsck")
  119. then
  120. return ngx.redirect("/terms_of_use.html")
  121. end
  122. -- tests passed
  123. ';
  124. # proxy_pass/fastcgi_pass/etc settings
  125. }
  126. }
本文地址:http://www.92csz.com/45/104.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
 

抱歉!评论已关闭.