/usr/local/openresty
. You can adjust the paths in this sample according to the actual installation prefix of your OpenResty installation. If you haven't installed OpenResty yet, check out the Download and Installation pages.2.0.4.1
, and we'll use this version throughout this sample.wget http://luarocks.org/releases/luarocks-2.0.4.1.tar.gz tar -xzvf luarocks-2.0.4.1.tar.gz cd luarocks-2.0.4.1/ ./configure make sudo make install
sudo luarocks install md5
/usr/local/openresty/nginx/
:cd /usr/local/openresty/nginx/Next, edit the
conf/nginx.conf
file to the following contents with your favorite text editor (like vim or emacs):worker_processes 1; # we could enlarge this setting on a multi-core machine error_log logs/error.log warn; events { worker_connections 1024; } http { lua_package_path 'conf/?.lua;;'; server { listen 80; server_name localhost; location = /luarocks { content_by_lua ' local foo = require("foo") foo.say("hello, luarocks!") '; } } }Finally, create the following two Lua module files
conf/foo.lua
-- conf/foo.lua module("foo", package.seeall) local bar = require "bar" ngx.say("bar loaded") function say (var) bar.say(var) endand
conf/bar.lua
-- conf/bar.lua module("bar", package.seeall) local rocks = require "luarocks.loader" local md5 = require "md5" ngx.say("rocks and md5 loaded") function say (a) ngx.say(md5.sumhexa(a)) end
ulimit -n1024 # increase the maximal fd count limit per process ./sbin/nginxIf you have already started the Nginx server, then stop it before starting it:
./sbin/nginx -s stop
curl
utility or any HTTP compliant clients like a web browser:curl http://localhost/luarockswe could get the following outputs at the first run:
rocks and md5 loaded bar loaded 85e73df5c41378f830c031b81e4453d2then at the second run:
85e73df5c41378f830c031b81e4453d2The output changed because LuaNginxModule by default caches already loaded Lua modules and those outputing code run at Lua module loading time will no longer be run.
ab -c10 -n50000 http://127.0.0.1/luarocksOn my Thinkpad T400 laptop (Core2Duo T9600 CPU), it yields
Server Software: ngx_openresty/1.0.4.2rc10 Server Hostname: localhost Server Port: 80 Document Path: /luarocks Document Length: 33 bytes Concurrency Level: 10 Time taken for tests: 3.052 seconds Complete requests: 50000 Failed requests: 0 Write errors: 0 Total transferred: 9400188 bytes HTML transferred: 1650033 bytes Requests per second: 16380.48 [#/sec] (mean) Time per request: 0.610 [ms] (mean) Time per request: 0.061 [ms] (mean, across all concurrent requests) Transfer rate: 3007.41 [Kbytes/sec] receivedNote that the throughput is achieved by a single nginx worker process. While doing such benchmark on your own, just be careful about error log level settings in your nginx.conf and not to run out of dynamic port range on your local machine, or it'll be significantly slow after a short of period of time.
lua_code_cache
on will cause LuaRocks atop LuaNginxModule to throw out the following exception in error.log
:lua handler aborted: runtime error: stack overflowIf you're using any version of OpenResty before 1.0.4.2rc10, please consider upgrading.
或是邮件反馈可也:
askdama[AT]googlegroups.com
订阅 substack 体验古早写作:
关注公众号, 持续获得相关各种嗯哼: