Nginx与LUA(5)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

您好我是湘王这是我的CSDN博客欢迎您来欢迎您再来~


Nginx诞生以来就获赞无数反向代理、负载均衡、流量限制与流量扩展都是它的拿手好戏。基本上是互联网应用的主流入口和计算、即时通讯、存储一样是一种基础且通用的组件而且对性能和稳定性有很高的要求。

对业务来讲Nginx又是业务直接和外部交流的接入点对二次开发和动态变化又有着强烈的以及一些定制化的需求因此出现了LUA这种嵌入式的脚本语言它能揉和以及处理各种不同的Nginx上游输出proxy、log等。这就像Java的成功依赖于丰富的生态应用一样OpenResty就是建立在Nginx + LUA之上的一种「生态」一个基于Nginx与Lua的高性能Web平台其内部集成了大量的Lua库、第三方模块以及大多数的依赖项。

通过揉和、拼接众多设计良好的Nginx模块OpenResty有效地把Nginx转变成一个强大的Web应用服务器它可以随心所欲地做复杂的访问控制、业务处理和安全检测。

基于OpenResty开发人员可以使用Lua脚本语言对Nginx核心以及现有的各种Nginx模块进行脚本编程构建出可以处理一万以上并发请求的超高性能的Web应用可以随意操控、分发、复制、限制、缓存响应头、Cookie及外部存储中的数据信息。有了OpenRestyNginx不再是一个简单的HTTP服务器而是可以扩展成为全功能的Web应用服务器了。

安装也很简单以CentOS为例

yum install -y readline-devel pcre-devel openssl-devel gcc

cd ~

wget https://openresty.org/download/openresty-1.17.8.2.tar.gz

tar -xvzf openresty-1.17.8.2.tar.gz

cd openresty-1.17.8.2

./configure

gmake && gmake install

先停止之前的nginx服务

/usr/local/nginx/sbin/nginx -s stop

将默认启动的原生nginx改为openresty

vi /etc/rc.local

将/usr/local/nginx/sbin/nginx改为/usr/local/openresty/nginx/sbin/nginx

启动OpenResty

/usr/local/nginx/sbin/nginx -p /usr/local/openresty/nginx/

访问页面http://服务器IP/

如果成功就会出现欢迎页面

如果不加-p /usr/local/openresty/nginx/参数是无法启动和停止OpenResty的

/usr/local/nginx/sbin/nginx -p /usr/local/openresty/nginx/

/usr/local/nginx/sbin/nginx -s stop -p /usr/local/openresty/nginx/

访问http://服务器IP/正常

访问http://服务器IP/test?username=test1出现404错误

这是因为此时的配置文件启用的是openresty中的配置文件所以需要修改一下配置

拷贝/usr/local/nginx/conf/nginx.conf中的内容到/usr/local/openresty/nginx/conf/nginx.conf

打开端口日志

tail -f /home/work/logs/nginx/8080.log

tail -f /home/work/logs/nginx/9090.log

访问http://服务器IP/test?username=test1

发现之前的流量复制功能也能正常工作说明OpenResty配置完全正常。

也可以直接使用openresty自带的nginx启动后续服务都以这种方式启动

启动/usr/local/openresty/nginx/sbin/nginx

重启/usr/local/openresty/nginx/sbin/nginx -s reload

停止/usr/local/openresty/nginx/sbin/nginx -s stop

编辑lua配置文件先停止单一的nginx服务

/usr/local/nginx/sbin/nginx -s stop -p /usr/local/openresty/nginx/

编辑nginx.conf文件

vi /usr/local/openresty/nginx/conf/nginx.conf

在http部分添加配置

lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #lua 模块

lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; #c 模块

用openresty启动

/usr/local/openresty/nginx/sbin/nginx

在/usr/local/openresty/nginx/conf目录下创建一个lua.conf

echo > /usr/local/openresty/nginx/conf/lua.conf

编辑lua.conf文件

vi /usr/local/openresty/nginx/conf/lua.conf

给lua.conf增加内容

server {

listen 80;

server_name _;

}

在nginx.conf的http中包含lua.conf

重启openresty并测试是否正常

/usr/local/openresty/nginx/sbin/nginx -s reload

/usr/local/openresty/nginx/sbin/nginx -t

测试用lua打印出「hello world」就要先在lua.conf中server部分添加配置

location /lua {

default_type 'text/html';

content_by_lua 'ngx.say("hello world")';

}

测试配置是否正确

/usr/local/openresty/nginx/sbin/nginx -t

重启服务

/usr/local/openresty/nginx/sbin/nginx -s reload

访问地址就可以看到打印出来的内容

http://服务器IP/lua

把lua代码放在nginx中会随着代码的变更导致维护困难因此应该把lua代码移到外部文件中存储nginx.conf ⊂ lua.conf ⊂ code.lua⊂是数学符号中的「包含」的意思这个的意思是让nginx配置文件包含lua配置文件让lua配置文件再引用实际的lua代码脚本。

创建保存lua代码的目录

mkdir /usr/local/openresty/nginx/conf/lua

增加lua代码文件

vi /usr/local/openresty/nginx/conf/lua/helloworld.lua

增加一行最外层没有单引号''

ngx.say("hello world lua")

修改lua.conf

content_by_lua_file conf/lua/helloworld.lua; # conf是相对于nginx安装目录而言

lua_code_cache off; # 关闭缓存便于开发调试线上生产环境一般都需要打开

关闭缓存重启后收看到报警

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:7

重启服务

/usr/local/openresty/nginx/sbin/nginx -s reload

再次访问同样能看到结果

http://服务器IP/lua

lua可以做很多包括但不限于

1、HTTP请求都需要接收请求、处理并输出响应

2、请求包含请求参数、请求头、Body体等信息

3、服务器通过对请求的处理输出客户端需要的内容

4、响应内容包含响应状态码、响应头和响应内容体

例如输出请求的lua脚本可以这样写

vi /usr/local/openresty/nginx/conf/lua.conf

server {

listen 80;

server_name _;

location /request {

default_type 'text/html';

lua_code_cache off;

# 设置nginx变量

set $a $host;

# nginx内容处理

content_by_lua_file conf/lua/request.lua;

# 内容体处理完成后调用

echo_after_body "ngx.var.a = $a";

}

}

创建创建request.lua

vi /usr/local/openresty/nginx/conf/lua/request.lua

-- 声明nginx变量

local var = ngx.var

ngx.say("ngx.var.a : ", var.a)

var.a = 2;

-- 处理请求头

local headers = ngx.req.get_headers()

ngx.say("=== headers begin ===")

ngx.say("Host : ", headers["Host"])

ngx.say("user-agent : ", headers["user-agent"])

ngx.say("user-agent : ", headers.user_agent)

for k, v in pairs(headers) do

ngx.say(k, ": ", v)

end

ngx.say("=== headers end ===")

-- 处理get参数

ngx.say("=== get args begin ===")

local uri_args = ngx.req.get_uri_args()

for k, v in pairs(uri_args) do

ngx.say(k, ": ", v)

end

ngx.say("=== get args end ===")

-- 处理post参数

ngx.req.read_body()

ngx.say("=== post args begin ===")

local post_args = ngx.req.get_post_args()

for k, v in pairs(post_args) do

ngx.say(k, ": ", v)

end

ngx.say("=== post args end ===")

-- http协议版本

ngx.say("ngx.req.http_version:", ngx.req.http_version())

-- 请求方法

ngx.say("ngx.req.get_method:", ngx.req.get_method())

-- 未解析的请求头

ngx.say("ngx.req.raw_header:", ngx.req.raw_header())

-- 未解析的请求body

ngx.say("ngx.req.get_body_data:", ngx.req.get_body_data())

可以测试一下看看结果

重启服务

/usr/local/openresty/nginx/sbin/nginx -s reload

执行curl命令看看效果

curl -XPOST 'http://服务器IP/request?a=1&b=2' -d 'c=3&d=4'

再比如输出响应的lua脚本可以这样写

vi /usr/local/openresty/nginx/conf/lua.conf

server {

listen 80;

server_name _;

location /request {

default_type 'text/html';

lua_code_cache off;

# 设置nginx变量

set $a $host;

# nginx内容处理

content_by_lua_file conf/lua/request.lua;

# 内容体处理完成后调用

echo_after_body "ngx.var.a = $a";

}

location /response {

default_type "text/html";

lua_code_cache off;

content_by_lua_block {

ngx.print("hello")

ngx.flush()

ngx.sleep(1)

ngx.say("world")

return ngx.exit(200)

}

}

}

重启服务

/usr/local/openresty/nginx/sbin/nginx -s reload

执行curl命令

curl 'http://服务器IP/response'

一些开发建议

OpenResty由于集成了大量的lua库和第三方库因此最好将这些依赖单独放到一次便于管理可以参考其他语言的开发习惯将代码分项目、分层、分包管理。例如

com.xiangwang.lua.mysql

com.xiangwang.lua.redis

com.xiangwang.lua.page

com.xiangwang.lua... ...


感谢您的大驾光临咨询技术、产品、运营和管理相关问题请关注后留言。欢迎骚扰不胜荣幸~

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginx