router 是一个比较方便的 openresty 路由组件,我们可以用来编写灵活强大的 web 应用,类似的
lua-resty-route 也是很不错的,但是如果是比较简单的直接可以使用 lua-resty-template
备注: 测试环境使用docker-compose

环境准备

  • docker-compose 文件
version: "3"
services:
router:
build: ./
volumes:
- "./nginx_lua/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
ports:
- "8080:80"
  • nginx.conf
    包含了template 以及router 的使用
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
lua_need_request_body on;
gzip on;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
init_by_lua_block {
template = require "resty.template"
}
server {
listen 80;
server_name localhost;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
content_by_lua_block {
require("web/init")()
}
}
location /userlogin {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/jquery")();
}
}
location /usercom {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/com")();
}
}
location = /favicon.ico {
root /opt/app/static;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
}
  • dockerfile
    安装lua 包
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install luacheck
RUN /usr/local/openresty/luajit/bin/luarocks install busted
RUN /usr/local/openresty/luajit/bin/luarocks install luacov
RUN /usr/local/openresty/luajit/bin/luarocks install luacov-coveralls
RUN /usr/local/openresty/luajit/bin/luarocks install router
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-template
EXPOSE 80

router && template 代码

  • router
    nginx_lua/web/init.lua router 的路由编写还是很清晰的,按照http verb 很清晰
local router = require 'router'
function init()
local r = router.new()
r:match({
GET = {
["/hello"] = function(params) ngx.print("someone said hello") end,
["/hello/:name"] = function(params) ngx.print("hello, " .. params.name) end,
["/"] = function(params) ngx.say([[demo app rong]]) end
},
POST = {
["/app/:id/comments"] = function(params)
ngx.print("comment " .. params.comment .. " created on app " .. params.id)
end
}
}) local ok, errmsg = r:execute(
ngx.var.request_method,
ngx.var.request_uri,
ngx.req.get_uri_args(), -- all these parameters
ngx.req.get_post_args(), -- will be merged in order
{other_arg = 1}) -- into a single "params" table if ok then
ngx.status = 200
else
ngx.status = 404
ngx.print("Not found!")
ngx.log(ngx.ERROR, "some wrong")
end
end
return init

template 代码,这个主要是需要配置几变量,指定模板文件的位置,如下:

location /userlogin {
set $template_root /opt/app/static;
content_by_lua_block {
require("web/jquery")();
}
}

template render 代码,template 使用了全局变量,在init 阶段初始化

init_by_lua_block {
template = require "resty.template"
}

render 处理
nginx_lua/web/juqey.lua

function init()
template.render(
"index.html",
{
message = "Hello, World!",
jquery = '<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>'
}
)
end
return init

view 模板
nginx_lua/static/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index page demo</title>
</head>
<body>
{{message}}
{*jquery*}
<script >
$(document).ready(function(){
alert("is ok")
})
</script>
</body>
</html>

运行效果

  • 启动
docker-compose up -d
curl -i http://localhost:8080
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Thu, 24 Jan 2019 00:29:35 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive demo app rong

view template:

curl -i http://localhost:8080/userlogin
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Thu, 24 Jan 2019 00:30:19 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>index page demo</title>
</head>
<body>
Hello, World!
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script >
$(document).ready(function(){
alert("is ok")
})
</script>
</body>

说明

openresty 的开发还是很方便的,可以帮助我们解决好多实际中的问题。简单高效

参考资料

https://github.com/rongfengliang/openrety-router-docker-compose
https://github.com/bungle/lua-resty-route
https://github.com/APItools/router.lua
https://github.com/bungle/lua-resty-template

 
 
 
 

openresty router && template 试用的更多相关文章

  1. skipper http router 简单试用

    说明: 使用源码编译,注意需要FQ,以及golang版本的问题,新版使用的是go mod 进行依赖管理 环境准备 clone 代码 git clone https://github.com/zalan ...

  2. vue router路由(三)

    当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) no ...

  3. Vue Router的配置

    1.beforeEnter function requireAuth (route, redirect, next) { if (!auth.loggedIn()) { redirect({ path ...

  4. Vue全家桶(Vue-cli、Vue-route、vuex)

    摘要 学习本篇之前要具备一定的vue基础知识,可以先看一下Vue基础(环境配置.内部指令.全局API.选项.内置组件) 1.Vue-cli Vue-cli是vue官方出品的快速构建单页应用的脚手架,这 ...

  5. 重开Vue2.0

    目录: 内容: 一.Vue内部指令: 1.v-if v-else&v-show v-if与v-show都是选择性显示内容的指令,但是二者之间有区别: 1.v-if:判断是否加载,在需要的时候加 ...

  6. vue-cli 脚手架分析

    Vue-cli 一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和 ...

  7. vue-cli 搭建

    一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和版本情况.出现版 ...

  8. Vue.js学习笔记(2)vue-router

    vue中vue-router的使用:

  9. CloudStack全局参数

    {     "listconfigurationsresponse": {         "count": 305,         "config ...

随机推荐

  1. 《Python》网络编程之验证客户端连接的合法性、socketserver模块

    一.socket的更多方法介绍 # 服务端套接字函数 s.bind() # 绑定(主机,端口号)到套接字 s.listen() # 开始TCP监听 s.accept() # 被动接受TCP客户的连接, ...

  2. [整理]Kadane算法

    仅能操作一次时,需每次观察是否有为负情况置零.多次操作时,仅需判断是否后者大于前者. leetcode 53.121.122 [代码] class Solution { public int maxS ...

  3. VSTO:使用C#开发Excel、Word【8】

    office加载项Office开发中使用的第二种模式是加载项模式.本书涵盖了几种Office加载项.其中包括Outlook的VSTO加载项,Excel和Word的COM加载项以及Excel的自动化加载 ...

  4. C++基础知识:构造与析构

    1.构造函数的定义: C++中的类可以定义与类名相同的特殊成员函数这种与类名相同的成员函数叫做构造函数构造函数在定义时可以有参数,但是没有任何返回类型的声明 2.构造函数的调用: 一般情况下C++编译 ...

  5. Linux如何产看系统信息

    如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...

  6. python23的区别-日常记录

    1. xrange:python3 中取消了range函数,把python2中的xrange重新命名为range,所以在python3中直接用range就行. 2. print:python3中pri ...

  7. hdu3861 强连通分量缩点+二分图最最小路径覆盖

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. request 的下载文件

    前言:Content-Type类型为octets/stream,这种一般是文件类型了,比如有时候需要导出excel数据,下载excel这种场景如何用python来实现呢? 1.点击导出按钮 2.代码实 ...

  9. mysql'密码安全

    MYSQL数据库的安全配置 MYSQL密码的修改与恢复 MYSQL数据库密码的修改 Mysql5.7以下默认root登录密码为空,安装完成之后首先需要修改root的登录密码. # mysqladm – ...

  10. Java并发容器和框架

    ConcurrentHashMap 在多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率近100%.因为多线程会导致HashMap的Entry链表形成环形数据结构,一旦形成环 ...