title: openresty使用MVC模板
urlname: 22_openresty使用MVC模板
author: vsyour
language: zh-CN
date: 2017-05-04 03:03:03
tags: [openresty,lua]
categories: 网络安全
openresty使用MVC模板
我是用默认路径安装了openresty.
就是直接用.configure&&make&&make install
安装完成的.其他的什么都不改.
官方说明:https://github.com/bungle/lua-resty-template
lua-resty-template
下载安装
LuaRocks安装
luarocks install lua-resty-template
如果没有luarocks可以用yum或者apt安装一个
配置nginx
设置两个变量就行了:
template_root
放在server {下面
set $template_root /usr/local/openresty/nginx/html/templates
template_location
放到location / {下面
set $template_location /templates
配置方式一
用Using document_root的方法配置:
http {
server {
location / {
root html;
content_by_lua '
local template = require "resty.template"
template.render("view.html", { message = "Hello, World!" })
';
}
}
}
配置方式二(推荐)
用Using template_root 的方法配置
http {
server {
set $template_root /usr/local/openresty/nginx/html/templates;
location / {
root html;
content_by_lua '
local template = require "resty.template"
template.render("view.html", { message = "Hello, World!" })
';
}
}
}
配置方式三
用Using template_location的方法配
http {
server {
set $template_location /templates;
location / {
root html;
content_by_lua '
local template = require "resty.template"
template.render("view.html", { message = "Hello, World!" })
';
}
location /templates {
internal;
alias html/templates/;
}
}
}
mkdir -p /usr/local/openresty/nginx/html/templates
cd /usr/local/openresty/nginx/html/templates
vi view.html
文件内容如下:
<!DOCTYPE html>
<html>
<body>
<h1>{{message}}</h1>
</body>
</html>
这样就可以用模板操作了.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ou6AqbIX-1680156412325)(null)]