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 ..
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 v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12--add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install
安装完成后,我们体验一下lua
location @client{
proxy_pass http://www.ruifengyun.com;}
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" then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
if ngx.var.remote_addr == "10.2.20.112" then
ngx.exec("@client")
end
';}
控制经过判断之后,才能访问
location /{
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/...} 使用lua做nginx的rewrite跳转
这个是先判断 check-pam接口的return的内容是不是spam,是的话,转跳到其他的页面
location /{
rewrite_by_lua '
local res = ngx.location.capture("/check-spam")
if res.body == "spam" then
ngx.redirect("/terms-of-use.html")
end
'; fastcgi_pass ...;} 根据ip做不同的响应
location /{
content_by_lua '
myIP = ngx.req.get_headers()["X-Real-IP"]
if myIP == nil then
myIP = ngx.req.get_headers()["x_forwarded_for"]
end
if myIP == nil then
myIP = ngx.var.remote_addr
end
if myIP == "" then
ngx.exec("@client")
else
ngx.exec("@client_test")
end
';}
redirect的使用
location =/test {
content_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
';}
一个Lua的例子:
worker_processes 2;
error_log logs/error.log warn;
events {
worker_connections 1024;}
http {
upstream backend {
drizzle_server 127.0.0.1:3306 protocol=mysql
dbname=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 then
return ngx.exec("@cats-by-name")
end
if ngx.var.arg_id then
return ngx.exec("@cats-by-id")
end
';
rds_json_ret 400"expecting \"name\" or \"id\" query arguments";}}}
改改密码就能用啦~
lua获取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里面的配置是: