1.先安装lua
- fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
- tar zxvf lua-5.1.4.tar.gz
- cd lua-5.1.4
- make freebsd
- make install
- cd ..
2.安装lua_nginx_module
- fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
- fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
- tar zxvf chaoslawful-lua-nginx-module-v0.1.6rc2-0-gccaf132.zip
- mv chaoslawful-lua-nginx-module-ccaf132 nginx-0.8.54/lua_nginx_module
- tar zxvf simpl-ngx_devel_kit-v0.2.17-0-gbc97eea.zip
- mv simpl-ngx_devel_kit-bc97eea nginx-0.8.54/ngx_devel_kit
- cd nginx-0.8.54
- ./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
- make && make install
lua_nginx_module模块的使用
- # set search paths for pure Lua external libraries (';;' is the default path):
- lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';
- # set search paths for Lua external libraries written in C (can also use ';;'):
- lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
- server {
- location /inline_concat {
- # MIME type determined by default_type:
- default_type 'text/plain';
- set $a "hello";
- set $b "world";
- # inline lua script
- set_by_lua $res "return ngx.arg[1]..ngx.arg[2]" $a $b;
- echo $res;
- }
- location /rel_file_concat {
- set $a "foo";
- set $b "bar";
- # script path relative to nginx prefix
- # $ngx_prefix/conf/concat.lua contents:
- #
- # return ngx.arg[1]..ngx.arg[2]
- #
- set_by_lua_file $res conf/concat.lua $a $b;
- echo $res;
- }
- location /abs_file_concat {
- set $a "fee";
- set $b "baz";
- # absolute script path not modified
- set_by_lua_file $res /usr/nginx/conf/concat.lua $a $b;
- echo $res;
- }
- location /lua_content {
- # MIME type determined by default_type:
- default_type 'text/plain';
- content_by_lua "ngx.say('Hello,world!')"
- }
- location /nginx_var {
- # MIME type determined by default_type:
- default_type 'text/plain';
- # try access /nginx_var?a=hello,world
- content_by_lua "ngx.print(ngx.var['arg_a'], '\\n')";
- }
- location /request_body {
- # force reading request body (default off)
- lua_need_request_body on;
- content_by_lua 'ngx.print(ngx.var.request_body)';
- }
- # transparent non-blocking I/O in Lua via subrequests
- location /lua {
- # MIME type determined by default_type:
- default_type 'text/plain';
- content_by_lua '
- local res = ngx.location.capture("/some_other_location")
- if res.status == 200 then
- ngx.print(res.body)
- end';
- }
- # GET /recur?num=5
- location /recur {
- # MIME type determined by default_type:
- default_type 'text/plain';
- content_by_lua '
- local num = tonumber(ngx.var.arg_num) or 0
- ngx.say("num is: ", num)
- if num > 0 then
- res = ngx.location.capture("/recur?num=" .. tostring(num - 1))
- ngx.print("status=", res.status, " ")
- ngx.print("body=", res.body)
- else
- ngx.say("end")
- end
- ';
- }
- location /foo {
- rewrite_by_lua '
- res = ngx.location.capture("/memc",
- { args = { cmd = 'incr', key = ngx.var.uri } }
- )
- ';
- proxy_pass http://blah.blah.com;
- }
- location /blah {
- access_by_lua '
- local res = ngx.location.capture("/auth")
- if res.status == ngx.HTTP_OK then
- return
- end
- if res.status == ngx.HTTP_FORBIDDEN then
- ngx.exit(res.status)
- end
- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
- ';
- # proxy_pass/fastcgi_pass/postgres_pass/...
- }
- location /mixed {
- rewrite_by_lua_file /path/to/rewrite.lua;
- access_by_lua_file /path/to/access.lua;
- content_by_lua_file /path/to/content.lua;
- }
- # use nginx var in code path
- # WARN: contents in nginx var must be carefully filtered,
- # otherwise there'll be great security risk!
- location ~ ^/app/(.+) {
- content_by_lua_file /path/to/lua/app/root/$1.lua;
- }
- location / {
- lua_need_request_body on;
- client_max_body_size 100k;
- client_body_in_single_buffer on;
- access_by_lua '
- -- check the client IP addr is in our black list
- if ngx.var.remote_addr == "132.5.72.3" then
- ngx.exit(ngx.HTTP_FORBIDDEN)
- end
- -- check if the request body contains bad words
- if ngx.var.request_body and
- string.match(ngx.var.request_body, "fsck")
- then
- return ngx.redirect("/terms_of_use.html")
- end
- -- tests passed
- ';
- # proxy_pass/fastcgi_pass/etc settings
- }
- }
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog