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,還行。

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