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

环境准备

  • docker-compose 文件
  1. version: "3"
  2. services:
  3. router:
  4. build: ./
  5. volumes:
  6. - "./nginx_lua/:/opt/app/"
  7. - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  8. ports:
  9. - "8080:80"
  • nginx.conf
    包含了template 以及router 的使用
  1. worker_processes 1;
  2. user root;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. lua_code_cache off;
  12. lua_need_request_body on;
  13. gzip on;
  14. resolver 127.0.0.11 ipv6=off;
  15. real_ip_header X-Forwarded-For;
  16. real_ip_recursive on;
  17. lua_package_path '/opt/app/?.lua;;';
  18. init_by_lua_block {
  19. template = require "resty.template"
  20. }
  21. server {
  22. listen 80;
  23. server_name localhost;
  24. charset utf-8;
  25. default_type text/html;
  26. location / {
  27. default_type text/plain;
  28. content_by_lua_block {
  29. require("web/init")()
  30. }
  31. }
  32. location /userlogin {
  33. set $template_root /opt/app/static;
  34. content_by_lua_block {
  35. require("web/jquery")();
  36. }
  37. }
  38. location /usercom {
  39. set $template_root /opt/app/static;
  40. content_by_lua_block {
  41. require("web/com")();
  42. }
  43. }
  44. location = /favicon.ico {
  45. root /opt/app/static;
  46. }
  47. error_page 500 502 503 504 /50x.html;
  48. location = /50x.html {
  49. root html;
  50. }
  51. }
  52. }
  • dockerfile
    安装lua 包
  1. FROM openresty/openresty:alpine-fat
  2. LABEL author="1141591465@qq.com"
  3. RUN /usr/local/openresty/luajit/bin/luarocks install luacheck
  4. RUN /usr/local/openresty/luajit/bin/luarocks install busted
  5. RUN /usr/local/openresty/luajit/bin/luarocks install luacov
  6. RUN /usr/local/openresty/luajit/bin/luarocks install luacov-coveralls
  7. RUN /usr/local/openresty/luajit/bin/luarocks install router
  8. RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-template
  9. EXPOSE 80

router && template 代码

  • router
    nginx_lua/web/init.lua router 的路由编写还是很清晰的,按照http verb 很清晰
  1. local router = require 'router'
  2. function init()
  3. local r = router.new()
  4. r:match({
  5. GET = {
  6. ["/hello"] = function(params) ngx.print("someone said hello") end,
  7. ["/hello/:name"] = function(params) ngx.print("hello, " .. params.name) end,
  8. ["/"] = function(params) ngx.say([[demo app rong]]) end
  9. },
  10. POST = {
  11. ["/app/:id/comments"] = function(params)
  12. ngx.print("comment " .. params.comment .. " created on app " .. params.id)
  13. end
  14. }
  15. })
  16. local ok, errmsg = r:execute(
  17. ngx.var.request_method,
  18. ngx.var.request_uri,
  19. ngx.req.get_uri_args(), -- all these parameters
  20. ngx.req.get_post_args(), -- will be merged in order
  21. {other_arg = 1}) -- into a single "params" table
  22. if ok then
  23. ngx.status = 200
  24. else
  25. ngx.status = 404
  26. ngx.print("Not found!")
  27. ngx.log(ngx.ERROR, "some wrong")
  28. end
  29. end
  30. return init

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

  1. location /userlogin {
  2. set $template_root /opt/app/static;
  3. content_by_lua_block {
  4. require("web/jquery")();
  5. }
  6. }

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

  1. init_by_lua_block {
  2. template = require "resty.template"
  3. }

render 处理
nginx_lua/web/juqey.lua

  1. function init()
  2. template.render(
  3. "index.html",
  4. {
  5. message = "Hello, World!",
  6. jquery = '<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>'
  7. }
  8. )
  9. end
  10. return init

view 模板
nginx_lua/static/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>index page demo</title>
  8. </head>
  9. <body>
  10. {{message}}
  11. {*jquery*}
  12. <script >
  13. $(document).ready(function(){
  14. alert("is ok")
  15. })
  16. </script>
  17. </body>
  18. </html>

运行效果

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

view template:

  1. curl -i http://localhost:8080/userlogin
  2. HTTP/1.1 200 OK
  3. Server: openresty/1.13.6.2
  4. Date: Thu, 24 Jan 2019 00:30:19 GMT
  5. Content-Type: text/html; charset=utf-8
  6. Transfer-Encoding: chunked
  7. Connection: keep-alive
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  14. <title>index page demo</title>
  15. </head>
  16. <body>
  17. Hello, World!
  18. <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
  19. <script >
  20. $(document).ready(function(){
  21. alert("is ok")
  22. })
  23. </script>
  24. </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. JavaScript 操作DOM对象

    1)JavaScript  操作DOM對象 1.DOM:是Document  Object  Model 的缩写,及文档对象模型 2.DOM通常分为三类:DOM Core(核心).HTML-DOM 和 ...

  2. 顺便谈谈对于Java程序猿学习当中各个阶段的建议

    引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...

  3. Linux如何产看系统信息

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

  4. delete和delete[] 区别

    // DeleteAndDelete[].cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> ...

  5. JS日期工具类(转)

    javascript Date format(js日期格式化) https://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.htm ...

  6. 安装google 框架

    使用  root exporer很方便   su cp /sdcard/google/busybox /data/local/tmp chmod 0755 /data/local/tmp/busybo ...

  7. jquery转换json对象为字符串

    jquery转换json对象为字符串 JSON.stringify(jsonObject),可用于单个JSON对象,也可用于JSON数组 alert(JSON.stringify(jsonObject ...

  8. ubantu 安装mysql 5.7 解决安装不提示设置密码问题

    1.安装Mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install l ...

  9. python之pandas简单介绍及使用(一)

    python之pandas简单介绍及使用(一) 一. Pandas简介1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据 ...

  10. 【Python】练习题

    练习1:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中 import os file1_path="e:\\test3\\2.t ...