首页 » PHP教程 » lua联合php技巧_nginxlua实现nginx高级应用

lua联合php技巧_nginxlua实现nginx高级应用

访客 2024-12-12 0

扫一扫用手机浏览

文章目录 [+]

Nginx+Lua上风 - 充分的结合nginx的并发处理epool上风和Lua的轻量实现大略的功能高并发的场景

linux lua安装

lua联合php技巧_nginxlua实现nginx高级应用

yum install lua

运行lua

lua联合php技巧_nginxlua实现nginx高级应用
(图片来自网络侵删)

[root@gofor]# luaLua 5.1.4 Copyright (C) 1994-2008Lua.org, PUC-Rio> print("hello world")hello world

Nginx编译安装Lua模块

参考Nginx编译安装Lua模块【http://www.imooc.com/article/19597】

nginx调用lua模块指令

set_by_lua 设置nginx变量,可以实现繁芜的复制逻辑

set_by_lua_file

access_by_lua 要求访问阶段处理,用于访问掌握

access_by_lua_file

content_by_lua 内容处理器,吸收要求处理并输出相应

content_by_lua_file

nginx lua api

ngx.var nginx变量

ngx.req.get_headers 获取要求头

ngx.req.get_uri_args 获取URL要求参数

ngx.redirect 重定向

ngx.print 输出相应内容体

ngx.say 同ngx.print,但是会在末了输出一个换行符

ngx.header 输出相应头

第一个nginx lua脚本

location /lua {default_type text/plain;content_by_lua 'ngx.say("hello world")';}

访问xxxx.com/lua,浏览器将输出hello world

用lua脚本实现IP访问限定

location @client{proxy_pass http://www.gofor.com;}location ~ /test {default_type text/html;access_by_lua 'if ngx.var.remote_addr == "101.2.20.110" thenngx.exit(ngx.HTTP_FORBIDDEN)endif ngx.var.remote_addr == "10.2.20.112" thenngx.exec("@client")end';}

掌握经由access_by_lua 访问权限判断之后,才能访问

用户访问xxxx.com/test时,如果客户端IP地址是101.2.20.110直接提示禁止访问;

如果客户端IP地址10.2.20.112时访问@client的location定义的处理

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)';}

访问网站之提高行auth认证,并且作相应处理

利用lua做nginx的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'; }lua redirect的利用

return ngx.redirect("/foo")return ngx.redirect("http://localhost:1984/foo", ngx.HTTP_MOVED_TEMPORARILY)return ngx.redirect("/foo", 301)

返回302临时重定向 地址栏会显示跳转后的地址

php7进阶到架构师干系阅读

https://www.kancloud.cn/gofor/gofor

末了,欢迎大家留言补充,谈论~~~
标签:

相关文章