openresty安装nginx和lua以及lua调用
用过nginx的人都知道,nginx用起来比较顺手也有很多的特性。但是在nginx上开发却成了一个很大的难点,因为Nginx模块的开发涉及到C语言,而且必须符合一系列复杂的规则,这就要求开发者必须非常熟悉Nginx的源代码,而且开发的C模块必须与源代码完美契合,这使得许多开发者望而生畏。openresty的产生为我们解决了这个难题,openresty允许我们借助lua来开发nginx,这使得开发nginx的难度大大降低。接下来给大家介绍一下openresty的安装以及怎样通过lua开发nginx。
安装pcre
mkdir /usr/local/openresty
cd /usr/local/openresty
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
注:(没有wget的要先安装wget)yum install wget
tar -zxvf pcre-8.35.tar.gz
cd /usr/local/openresty/pcre-8.35
yum install -y gcc gcc-c++
./configure
make && make install
cd /usr/local/openresty
安装openresty
yum install -y readline-devel pcre-devel openssl-devel gcc
wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -zxvf ngx_openresty-1.7.7.2.tar.gz
cd /usr/local/openresty/ngx_openresty-1.7.7.2/bundle/LuaJIT-2.1-20150120/
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
cd /usr/local/openresty/ngx_openresty-1.7.7.2/bundle/
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -zxvf 2.3.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz
cd /usr/local/openresty/ngx_openresty-1.7.7.2
./configure --prefix=/usr/local/openresty --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 --with-http_iconv_module --with-http_postgres_module --with-pcre-jit --with-pcre=../pcre-8.35
问题:
./configure: error: ngx_postgres addon was unable to detect version of the libpq library
解决:
yum install postgresql-devel
gmake && gmake install
cd /usr/local/openresty 可以查看到bin luajit lualib nginx等
部署实例一
cd /usr
mkdir -p /usr/hello/lua
cd /usr/hello/lua
vi hello.lua
ngx.say("hello world,this is slave1");
调用lua方式一
cd /usr/hello
vi hello.conf
server {
listen 80;
server_name 192.168.9.22; #自己的IP或域名
location /hello {
default_type 'text/html';
content_by_lua_file /usr/hello/lua/hello.lua;
}
}
cd /usr/local/openresty/nginx/conf
vi nginx.conf
在 http{
}中加入
lua_package_path “/usr/hello/lualib/?.lau;;”;
lua_package_cpath “/usr/hello/lualib/?.so;;”;
include /usr/hello/hello.conf;
cp -rf /usr/local/openresty/lualib /usr/hello/
调用lua方式二
cd /usr/local/openresty/nginx/conf
vi nginx.conf
server{
listen 80;
server_name 192.168.9.22; #自己的IP或域名
# charset koi8-r;
# access_log logs/host.access.log main;
location /test{
default_type 'text/html';
# lua_code_cache off;
content_by_lua_file / usr / hello / lua / hell.lua;
}
error_page 500 502 503 504 / 50x.html;
location = / 50x.html{
root html;
}
}
cd /usr/local/openresty/nginx/sbin
./nginx 回车
更改nginx配置后需要重新加载,命令为 ./nginx -s reload
部署实例二
调用lua的cjson库
编写测试lua test_cjson.lua
cd /usr/hello/lua/
vi lua test_cjson.lua
local cjson = require("cjson")
--lua对象到字符串
local obj = {
id = 1,
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
}
local str = cjson.encode(obj)
ngx.say(str, "<br/>")
--字符串到lua对象
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj = cjson.decode(str)
ngx.say(obj.age, "<br/>")
ngx.say(obj.age == nil, "<br/>")
ngx.say(obj.age == cjson.null, "<br/>")
ngx.say(obj.hobby[1], "<br/>")
--循环引用
obj = {
id = 1
}
obj.obj = obj
-- Cannot serialise, excessive nesting
--ngx.say(cjson.encode(obj), "<br/>")
local cjson_safe = require("cjson.safe")
--nil
ngx.say(cjson_safe.encode(obj), "<br/>")
cd /usr/local/openresty/nginx/conf
vi nginx.conf
server{
listen 80;
server_name 192.168.9.22; #自己的IP或域名
# charset koi8-r;
# access_log logs/host.access.log main;
location / test{
default_type 'text/html';
# lua_code_cache off;
content_by_lua_file / usr / hello / lua / test_cjson.lua;
}
error_page 500 502 503 504 / 50x.html;
location = / 50x.html{
root html;
}
}
./nginx -s reload
浏览器中输入:http://192.168.9.22:80/test 查看
注:该篇文章借鉴了其他两位博主的文章的基础上对存在一些比较模糊的地方予以修改。目的一是为后期自己使用方便,二是帮助需要的人,如有更好的方式或问题请留言。
原链接:https://blog.csdn.net/simba_1986/article/details/78452808
原链接:https://blog.csdn.net/u013412772/article/details/78996849 关于lua 的JSON库、编码转换、字符串处理的该博客比较详细