lua resty template && openresty 使用

1. 安装

  1. luarocks install lua-resty-template

2. 使用

配置模板页面位置

有多种方式:

a.  直接使用root 目录

代码如下:

  1. location /{
  2. root html;
  3. content_by_lua ‘
  4. local template = require "resty.template"
  5. template.render("view.html",{ message ="Hello, World!"})
  6. ‘;
  7. }

view.html

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h1>{{message}}</h1>
  5. </body>
  6. </html>

b.  set $template_root

代码如下:

  1. http {
  2. server {
  3. set $template_root /usr/local/openresty/nginx/html/templates;
  4. location /{
  5. root html;
  6. content_by_lua ‘
  7. local template = require "resty.template"
  8. template.render("view.html",{ message ="Hello, World!"})
  9. ‘;
  10. }
  11. }
  12. }

c.  set  template_location(原理:ngx.location.capture from /templates)

代码如下:

  1. http {
  2. server {
  3. set $template_location /templates;
  4. location /{
  5. root html;
  6. content_by_lua ‘
  7. local template = require "resty.template"
  8. template.render("view.html",{ message ="Hello, World!"})
  9. ‘;
  10. }
  11. location /templates {
  12. internal;
  13. alias html/templates/;
  14. }
  15. }

3. 参考文档

  1. https://github.com/bungle/lua-resty-template
时间: 2024-08-08 09:40:31

lua resty template && openresty 使用的相关文章

LUA+resty 搭建验证码服务器

使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就能控制.生成图片. 环境说明: 操作系统:RHEL6.4 RHEL系统默认已安装RPM包的Lua-5.1.4,但其只具有Lua基本功能,不提供 lua.h 等,但 Lua-GD 编译需要用到 lua.h,故 Lua 需要编译安装. Lua-GD... Lua下有个Lua-GD图形库,通过简单的Lua

使用Nginx+Lua(OpenResty)开发高性能Web应用

在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开发主要是以C/C++模块的形式进行,整体学习和开发成本偏高:如果有一种简单的语言来实现Web应用的开发,那么Nginx绝对是把好的瑞士军刀:目前Nginx团队也开始意识到这个问题,开发了nginxScript:可以在Nginx中使用JavaScript进行动态配置一些变量和动态脚本执行:而目前市面上

Nginx+Lua(OpenResty)开发高性能Web应用

使用Nginx+Lua(OpenResty)开发高性能Web应用 博客分类: 跟我学Nginx+Lua开发 架构 ngx_luaopenresty 在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开发主要是以C/C++模块的形式进行,整体学习和开发成本偏高:如果有一种简单的语言来实现Web应用的开发,那么Nginx绝对是把好的瑞士军刀:目前Ngin

OpenResty之resty.limit.count 模块介绍

resty.limit.count 模块介绍: resty.limit.count 模块就是限制接口单位时间的请求数,This module depends on lua-resty-core模块,所以要在openresty 的http标签端添加 nginxinit_by_lua_block {require "resty.core"} 同时resty.limit.count模块是在OpenResty 1.13.6.1+ 引入的 openresty下开启resty.limit.coun

高频访问IP限制 --Openresty(nginx + lua) [反爬虫之旅]

嗯-.本人是从写爬虫开始编程的,不过后面做web写网站去了,好了,最近web要搞反爬虫了,哈哈哈,总算有机会把之以前做爬虫时候见识过的反爬一点点给现在的网站用上了~ 做爬虫的同志,有怪莫怪喽~还有求别打死 > < 首先要提一下AJAX,现在普天下网页几乎都是往特定的数据接口请求数据了,除了什么首屏渲染这种服务端渲染好html以外,几乎没有什么静态网页了.我看了有一些帖子说AJAX让爬虫难做,可是我觉得结合一些工具(比如chrome的开发者工具),找到AJAX所请求的后端数据接口一点也不难,而且

验证 Openresty+Lua+GraphicsMagick

1 环境准备 1.1 CentOS 安装 Openresty Openresty 的下载及安装请参考:http://openresty.org/cn/. 1.2 安装 GraphicsMagick 源码安装,源码下载地址:https://sourceforge.net/projects/graphicsmagick/. 源码下载后解压,切换到源码目录: ./configure make make install 2 测试代码 代码参考:(linsir/ngx-lua-images)[https:

openresty开发系列24--openresty中lua的引入及使用

openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   --->字符串编写方式  2) xxx_by_lua_block ---->代码块方式  3) xxx_by_lua_file  ---->直接引用一个lua脚本文件 我们案例中使用内容处理阶段,用content_by_lua演示 -----------------编辑nginx.conf----------------------- 第一种:content_by_lua l

openresty lua 调试 (图文死磕)

疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] 前言 Crazy-SpringCloud 微服务脚手架 &视频介绍: Crazy-SpringCloud 微服务脚手架,是为 Java 微服务开发 入门者 准备的 学习和开发脚手架.并配有一系列的使用教程和视频,大致如下: 高并发 环境搭建 图文教程和演示视频,陆续上线: 中间件 链接地址 Linux Redis

openresty && hashids&& redis 生成短链接

1. 原理 a. 从redis 获取需要表示的短链接的id( redis incr) b. hashids 编码 id c. openresty  conteent_by_lua_block 阶段显示数据 2. 安装以来的插件 a. lua hashdis  使用  luarocks 注意需要先安装lua 开发包 b. copy hashids lua 包 到 openresty 的lualib  方便调用 c. redis 安装 luarocks install hashids 3. 代码 n