通过Lua解释器来扩展丰富nginx功能,实现复杂业务的处理

Nginx 很强

Lua 很强

所以,他俩和一起也一定很强。。。。(多么霸道的逻辑呀~)

lua_nginx_module 可以一步步的安装,虚拟主机,也可以直接用淘宝的OpenResty

Centos和debian的安装就简单了。。

这里说下freebsd的安装:

fetch tar zxvf lua-5.1.4.tar.gzcd lua-5.1.4make freebsdmake installcd ..fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2tar zxvf v0.1.6rc2mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_moduletar zxvf v0.2.17rc2mv simpl-ngx_devel_kit-bc97eea ngx_devel_kittar zxvf pcre-8.12.tar.gztar zxvf nginx-1.0.3.tar.gzcd nginx-1.0.3./configure –prefix=/data/soft/nginx –with-pcre=../pcre-8.12 –add-module=../ngx_devel_kit –add-module=../lua_nginx_modulemake && make install

ok后,说下lua这个强大的玩意。。。

简单的输出一句话。。。。

ngx.say 是打印的打印输出的意思。。。

location /echo {default_type text/plain;echo hello lua;}location /lua {default_type text/plain;content_by_lua ‘ngx.say(“hello world”)’;}

访问的限制…

location @client{proxy_pass ;}location ~ /test {default_type text/html;content_by_lua ‘ngx.say(“this is ruifengyun.com!”)’;access_by_lua ‘if ngx.var.remote_addr == “10.2.20.110” thenngx.exit(ngx.HTTP_FORBIDDEN)endif ngx.var.remote_addr == “10.2.20.112” thenngx.exec(“@client”)end’;}

控制经过判断之后,才能访问

location / {access_by_lua ‘local res = ngx.location.capture(“/auth”)if res.status == ngx.HTTP_OK thenreturnendif res.status == ngx.HTTP_FORBIDDEN thenngx.exit(res.status)endngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)’;# proxy_pass/fastcgi_pass/postgres_pass/…}

rewrite跳转

这个是先判断 check-pam接口的return的内容是不是spam,是的话,转跳到其他的页面

location / {rewrite_by_lua ‘local res = ngx.location.capture(“/check-spam”)if res.body == “spam” thenngx.redirect(“/terms-of-use.html”)end’; fastcgi_pass …;}

一个根据ip做匹配

location / {content_by_lua ‘myIP = ngx.req.get_headers()[“X-Real-IP”]if myIP == nil thenmyIP = ngx.req.get_headers()[“x_forwarded_for”]endif myIP == nil thenmyIP = ngx.var.remote_addrendif myIP == “” thenngx.exec(“@client”)elsengx.exec(“@client_test”)end’;}

redirect的使用

return ngx.redirect(“/foo”)return ngx.redirect(“http://localhost:1984/foo”, ngx.HTTP_MOVED_TEMPORARILY)return ngx.redirect(“/foo”, 301)返回302临时重定向 地址栏会显示跳转后的地址rewrite ^ /foo? redirect; # nginx configreturn ngx.redirect(‘/foo’); — lua code

过滤post过来的参数

location = /test {content_by_lua ‘ngx.req.read_body()local args = ngx.req.get_post_args()for key, val in pairs(args) doif type(val) == “table” thenngx.say(key, “: “, table.concat(val, “, “))elsengx.say(key, “: “, val)endend’;}

一个Lua的例子:

#!/usr/bin/env luangx.say(‘aaaaaa </br>’)local url = ngx.var.uringx.say(‘<br>’,url,'<br/>’)ngx.print(‘这次访问的header头是 ‘,ngx.req.raw_header())ngx.print(‘<meta http-equiv=”content-type” content=”text/html;charset=utf-8″>’)ngx.print(‘<h1> 这个是 h1 </h1>’)ngx.print(‘这次访问的是 get 还是 post 呀 ‘,ngx.req.get_Method())local args = ngx.req.get_uri_args()ngx.print(args)local res = ngx.location.capture(“/”)ngx.print(‘<br>http code <br>‘,res.status)

一个mysql的例子,from 春哥

worker_processes 2;error_log logs/error.log warn;events {worker_connections 1024;}http {upstream backend {drizzle_server 127.0.0.1:3306 protocol=mysqldbname=ngx_test user=ngx_test password=ngx_test;drizzle_keepalive max=10 overflow=ignore mode=single;}server {listen 8080;location @cats-by-name {set_unescape_uri $name $arg_name;set_quote_sql_str $name;drizzle_query ‘select * from cats where name=$name’;drizzle_pass backend;rds_json on;}location @cats-by-id {set_quote_sql_str $id $arg_id;drizzle_query ‘select * from cats where id=$id’;drizzle_pass backend;rds_json on;}location = /cats {access_by_lua ‘if ngx.var.arg_name thenreturn ngx.exec(“@cats-by-name”)endif ngx.var.arg_id thenreturn ngx.exec(“@cats-by-id”)end’;rds_json_ret 400 “expecting \”name\” or \”id\” query arguments”;}}}

改改密码就能用啦~

获取url中的参数

location = /adder {set_by_lua $res “local a = tonumber(ngx.arg[1])local b = tonumber(ngx.arg[2])return a + b” $arg_a $arg_b;echo $res;}

ngx.req.set_uri

nginx里面的配置是:

location /test {rewrite ^/test/(.*) /$1 break;proxy_pass ;}

lua里面的配置是:

location /test {rewrite_by_lua ‘local uri = ngx.re.sub(ngx.var.uri, “^/test/(.*)”, “$1”, “o”)ngx.req.set_uri(uri)’;proxy_pass ;}

我想大家看这个对照,已经知道是啥意思了.

通过lua获取nginx的内置变量,香港虚拟主机,通过这些变量做些逻辑的处理~

好像有头大象在吸水。然后再去了芦笛岩,

通过Lua解释器来扩展丰富nginx功能,实现复杂业务的处理

相关文章:

你感兴趣的文章:

标签云: