nginx + lua环境搭建

来源:互联网 发布:炮火兰捏脸数据 编辑:程序博客网 时间:2024/06/04 19:15

Lua 是一个小巧的脚本语言。该语言的设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。
Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。
不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,Ini等文件格式,并且更容易理解和维护。


直接使用官方的nginx + lua_nginx_module 比较麻烦,本文介绍两种nginx+lua的环境搭建
系统环境:CentOS release 6.6 x64

CentOS上面需要先安装如下依赖:yum install openssl openssl-devel curl curl-devel zlib zlib-devel pcre pcre-devel lua lua-devel

CentOS6.6 默认安装的lua版本是:Lua 5.1.4


方法一:直接使用tengine

安装步骤如下:

wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz

tar -xvf tengine-2.1.2.tar.gz

cd tengine-2.1.2

./configure --prefix=/usr/local/tengine --with-http_lua_module

make

make install 

这样,就安装好了。

然后,vim /usr/local/tengine/conf/nginx.conf

加入如下代码

location /lua {       default_type text/plain;       content_by_lua 'ngx.say("hello world lua")';}
最后,启动tengine

访问http://127.0.0.1/lua    

页面出现hello world lua ,表示安装成功


方法二:使用OpenResty

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

官方网站:https://openresty.org/en/

安装文档:https://openresty.org/en/installation.html

安装步骤:

wget https://openresty.org/download/ngx_openresty-1.9.7.2.tar.gz

tar -xvf ngx_openresty-1.9.7.2.tar.gz

cd ngx_openresty-1.9.7.2

./configure --prefix=/usr/local/openresty  (默认集成了lua环境)

gmake

gmake install

这样,就安装好了。

然后 vim /usr/local/openresty/nginx/conf/nginx.conf

加入如下代码:

location /lua {       default_type text/plain;       content_by_lua 'ngx.say("hello world lua")';}
最后,执行 /usr/local/openresty/nginx/sbin/nginx 启动nginx

访问http://127.0.0.1/lua    

页面出现hello world lua ,表示安装成功


我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。

nginx + lua简单使用示例

1:vim /lua/test.lua

内容如下:

ngx.say("hello world");

修改nginx.conf,添加如下内容

location /luafile {      default_type 'text/html';      content_by_lua_file /lua/test.lua;}
然后访问/luafile 即可看到效果

默认情况下lua_code_cache  是开启的,即缓存lua代码,即每次lua代码变更必须reload nginx才生效,如果在开发阶段可以通过lua_code_cache  off;关闭缓存,这样调试时每次修改lua代码不需要reload nginx;

开启后reload nginx会看到如下报警
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:54

下面是一个使用lua获取http请求数据的例子 vim /lua/info.lua

--请求头  local headers = ngx.req.get_headers()ngx.say("<p>请求头信息</p>")ngx.say("<p>Host : ", headers["Host"], "</p>")ngx.say("<p>user-agent : ", headers["user-agent"], "</p>")ngx.say("<p>user-agent2 : ", headers.user_agent, "</p>")--GET请求参数local uri_args = ngx.req.get_uri_args()ngx.say("<p>GET请求参数</p>")ngx.say("<p>id : ", uri_args["id"], "</p>")ngx.say("<p>type : ", uri_args["type"], "</p>")--POST请求参数  ngx.req.read_body()local post_args = ngx.req.get_post_args()ngx.say("<p>POST请求参数</p>")ngx.say("<p>username : ", post_args["username"], "</p>")ngx.say("<p>password : ", post_args["password"], "</p>")--获取其他请求信息ngx.say("<p>获取其他请求信息</p>")ngx.say("<p>request_uri : ", ngx.var.request_uri, "</p>")ngx.say("<p>请求的http协议版本 : ", ngx.req.http_version(), "</p>")ngx.say("<p>请求方法  : ", ngx.req.get_method(), "</p>")ngx.say("<p>原始的请求头内容 : ",  ngx.req.raw_header(), "</p>")ngx.say("<p>请求的body内容体 : ", ngx.req.get_body_data(), "</p>")

0 0