使用lua实现nginx rewrite
来源:互联网 发布:three.js pdf 编辑:程序博客网 时间:2024/05/16 09:28
上一次上线只是简单的把手机端的流量切换手机端的首页,有点粗糙,现在需要把电脑端相应的页面切换到电脑端的页面,直接用nginx自带的rewrite不太好做,所以用lua做
下面开始代码
nginx 配置
location / { rewrite_by_lua_file lua/mobile.lua; ... # 反向代理}
mobile.lua代码
-- 手机访问重定向到移动版local agent = ngx.var.http_user_agentif agent ~= nil then local m, ret = ngx.re.match(agent, "Android|webOS|iPhone|iPod|BlackBerry") if m ~= nil then local queryString = ngx.var.args if queryString == nil then queryString = "" end local rewrite_urls = {} rewrite_urls["/"] = "/app/index.html" rewrite_urls["/coin/index.html"] = "/app/coin.html" rewrite_urls["/equity/index.html"] = "/app/coin.html" rewrite_urls["/guide/index.html"] = "/app/guide.html?" .. queryString rewrite_urls["/market/trade.html"] = "/app/trade.html?" .. queryString rewrite_urls["/account/fund.html"] = "/app/account.html?" .. queryString rewrite_urls["/guide/article.html"] = "/app/guide-details.html?" .. queryString local redirect_uri = rewrite_urls[ngx.var.uri] if redirect_uri then ngx.redirect(redirect_uri, ngx.HTTP_MOVED_TEMPORARILY) end endend
其实逻辑也很简单,主要在nginx rewrite 阶段完成页面跳转,判断是否手机端,然后通过一个table把对应的url映射好,直接以当前uri作为key取table的值,如果取到说明有映射关系,直接把值当成新的url重定向过去
queryString是get参数,有的需要有的不需要,不带参数的话,有点页面会有问题,比如文章详情,是需要文章ID的
通过lua可以写很复杂的服务端处理逻辑,应用程序只需要做好自己的事情就好了
0 0
- 使用lua实现nginx rewrite
- nginx--rewrite指令使用
- nginx 如何使用rewrite
- 使用nginx的rewrite实现代理指定目录命令方法
- 使用nginx的rewrite实现代理指定目录命令方法
- nginx rewrite 实现URL跳转
- 使用Nginx+Lua实现的WAF
- apache/nginx 开启rewrite模块,实现链接的跳转/nginx上使用TP框架
- nginx实现url重写-rewrite实例参考
- Nginx php下实现rewrite和pathinfo
- nginx rewrite
- nginx rewrite
- nginx rewrite
- Nginx rewrite
- Nginx rewrite
- nginx:rewrite
- nginx rewrite
- Nginx rewrite
- 【github】项目收集合集(持续补充)
- 判别模型(Discriminative Model)和生成模型(Generative Model)
- easyui二级,三级联动问题
- JavaScript(一)javascript计时器
- 数组输出
- 使用lua实现nginx rewrite
- 数据类型的重要性
- 位运算用法总结
- Oracle Database之序列(Sequence)
- Gradle文件操作基础
- 《美人鱼》观后感
- HTTP状态码梳理
- iframe自适应、iframe跨域自适应
- ubuntu14.04+CPU环境下搭建caffe,python接口以及运行fast-rcnn实例