Nginx与Lua脚本结合使用示例

1,安装OpenResty

这边以CentOS6.3为例,通过前端工具进行安装,源码安装可参考官网

1.1 添加OpenResty仓库

sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

如果遇到以下SSL连接错误信息,可临时把 https 改成 http

adding repo from: https://openresty.org/package/centos/openresty.repo
grabbing file https://openresty.org/package/centos/openresty.repo to /etc/yum.repos.d/openresty.repo
https://openresty.org/package/centos/openresty.repo: [Errno 14] problem making ssl connection
Trying other mirror.
Could not fetch/save url https://openresty.org/package/centos/openresty.repo to file /etc/yum.repos.d/openresty.repo: [Errno 14] problem making ssl connection

1.2 安装OpenResty包

sudo yum install openresty

如果遇到以下SSL连接错误信息,

Loaded plugins: aliases, downloadonly, fastestmirror, priorities, security
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
https://openresty.org/package/centos/6/x86_64/repodata/repomd.xml: [Errno 14] problem making ssl connection
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: openresty. Please verify its path and try again

可修改 /etc/yum.repos.d/openresty.repo 文件,

[openresty]
name=Official OpenResty Open Source Repository for CentOS
#baseurl=https://openresty.org/package/centos/$releasever/$basearch
baseurl=http://openresty.org/package/centos/$releasever/$basearch
skip_if_unavailable=False
gpgcheck=1
repo_gpgcheck=0
#gpgkey=https://openresty.org/package/pubkey.gpg
gpgkey=http://openresty.org/package/pubkey.gpg
enabled=1
enabled_metadata=1

把其中https临时改成http,再进行安装。输出

openresty                                                                                                                                                                            | 2.9 kB     00:00
openresty/primary_db                                                                                                                                                                 |  49 kB     00:01
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package openresty.x86_64 0:1.19.3.1-1.el6 will be installed
--> Processing Dependency: openresty-zlib >= 1.2.11-3 for package: openresty-1.19.3.1-1.el6.x86_64
--> Processing Dependency: openresty-pcre >= 8.44-1 for package: openresty-1.19.3.1-1.el6.x86_64
--> Processing Dependency: openresty-openssl111 >= 1.1.1h-1 for package: openresty-1.19.3.1-1.el6.x86_64
--> Running transaction check
---> Package openresty-openssl111.x86_64 0:1.1.1k-1.el6 will be installed
---> Package openresty-pcre.x86_64 0:8.44-1.el6 will be installed
---> Package openresty-zlib.x86_64 0:1.2.11-3.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
.
.
.

Installed:
  openresty.x86_64 0:1.19.3.1-1.el6

Dependency Installed:
  openresty-openssl111.x86_64 0:1.1.1k-1.el6    openresty-pcre.x86_64 0:8.44-1.el6     openresty-zlib.x86_64 0:1.2.11-3.el6

Complete!

至此,OpenResty安装完成。

2 准备Lua脚本和Nginx配置

在任一目录下新建一个Lua脚本

--用于接收前端数据的对象
local args = nil
--获取前端的请求方式 并获取传递的参数
local request_method = ngx.var.request_method
--判断是get请求还是post请求并分别拿出相应的数据
if "GET" == request_method then
        args = ngx.req.get_uri_args()
elseif "POST" == request_method then
        ngx.req.read_body()
        args = ngx.req.get_post_args()
        --兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug
        if (args == nil or args.data == null) then
                args = ngx.req.get_uri_args()
        end
end

--获取前端传递的name值
local name = args.name
--响应前端
if (name == nil) then
        ngx.say("nginx-Lua say hello world!")
else
        ngx.say("nginx-Lua say hello:"..name)
end

比如我们放在 /home/work/test/testLua.lua中,然后在Nginx的任一vhost的*.conf中做以下配置:

server {
    listen              8080;
    server_name         www.test.com;

    root                /some/path/root;
 
    .
    .
    .

    location /testlua {
         default_type text/html;
         #这里的lua文件的路径为绝对路径,请根据自己安装的实际路径填写
         content_by_lua_file /home/work/test/testLua.lua;
    }
}

重启Nginx,使配置生效。

3 测试

3.1 功能测试

我们在浏览器中输入在Nginx中配置好的地址

不带参数

 

带参数

可以看到工作正常。

3.2 性能测试

我们用AB测试工具对这个Lua脚本进行一下性能压测,源码安装Apache可参照此文

/usr/local/apache2/bin/ab -c 1000 -n 10000 127.0.0.1:8080/testlua

-c 1000表示1000的并发量,-n 10000表示总共10000的请求量。结果

/usr/local/apache2/bin/ab -c 1000 -n 10000 127.0.0.1:8080/testlua

This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /testlua
Document Length:        16 bytes

Concurrency Level:      1000
Time taken for tests:   1.495 seconds
Complete requests:      10000
Failed requests:        862
   (Connect: 0, Receive: 0, Length: 862, Exceptions: 0)
Non-2xx responses:      10000
Total transferred:      4211542 bytes
HTML transferred:       294472 bytes
Requests per second:    6690.51 [#/sec] (mean)
Time per request:       149.465 [ms] (mean)
Time per request:       0.149 [ms] (mean, across all concurrent requests)
Transfer rate:          2751.70 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   70 249.9      0    1006
Processing:     0   30  49.1     17     405
Waiting:        0   30  49.2     16     405
Total:          0   99 289.4     17    1408

Percentage of the requests served within a certain time (ms)
  50%     17
  66%     19
  75%     23
  80%     27
  90%     60
  95%   1028
  98%   1217
  99%   1220
 100%   1408 (longest request)

16GB的内存,RPS 6690,还行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章