+22分钟:初尝¶
整起来先! - 嗯嗯嗯,现在可以计时了: 00:00
安装¶
参考: 官方安装说明
走起
$ brew install pcre
$ axel http://agentzh.org/misc/nginx/ngx_openresty-1.0.8.26.tar.gz
$ tar xzvf ngx_openresty-1.0.8.26.tar.gz
$ cd ngx_openresty-1.0.8.26
$ ./configure --with-cc-opt="-I/usr/local/Cellar/pcre/8.21/include" \
--with-ld-opt="-L/usr/local/Cellar/pcre/8.21/lib"
$ make
$ make install
$ $ /usr/local/openresty/nginx/sbin/nginx -v
nginx: nginx version: ngx_openresty/1.0.10.48
跑¶
玩 OpenResty 就是要反复调整内置nginx 的配置,为了方便起见
- 推荐 openresty.server.
- 是简化版的: Nginx-init-ubuntu
- 兼容 Linux/Unix/MAC 系统
$ /opt/sbin/openresty.server
usage: /opt/sbin/openresty.server {check|start|term|stop|reload|restart|upgrade}
建议部署在 /opt/sbin 目录 然后就可以随时:
- /opt/sbin/openresty.server check 检验配置脚本是否正确
- /opt/sbin/openresty.server start 启动 Nginx
- /opt/sbin/openresty.server reload 热加载新的配置文件
- 测试是否在跑? 使用 ps aux | grep nginx
玩¶
那么开始 OpenResty 的编程吧!
理解重要的目录结构:
/usr/local/openresty/
+- luajit Lua实时编译组件
+- lualib Lua预装原生库
+- nginx openresty 狠狠定制过的稳定版本 Nginx
+- log 默认的日志,pid 存放目录
+- conf 默认的配置文件目录
+- nginx.conf
那么,要作是只是:
修订 主配置文件,加入我的专用配置文件包含:
http { include my_openresty.conf; ...
创建 my_openresty.conf 并写入:
server { listen 9090; server_name localhost; error_log logs/error.my_openresty.log info; location / { content_by_lua "ngx.say('Hello,world!')"; } }
然后享受成果
$ /opt/sbin/openresty.server reload $ curl localhost:9090 Hello,world!
改¶
每次都要修改和各种 web 服务配置杂在一起的 nginx 配置,而且再重启 nginx 才能见到成果?!
当然有好招 - 引入独立 Lua 脚本,到指定 url 路由 - 增补 my_openresty.conf :
server {
listen 9090;
server_name localhost;
error_log logs/error.my_openresty.log info;
location / {
content_by_lua "ngx.say('Hello,world!')";
}
location /readme {
lua_code_cache off; # <<< 关键技巧!
content_by_lua_file conf/lua/readme.lua;
}
}
- 创建业务脚本: /usr/local/openresty/nginx/conf/lua/readme.lua
-- readme for my openresty app.
ngx.say("Hallo World!")
- 再重启 nginx, curl localhost:9090/readme 获得同样的 “Hallo World!”
- 要是看到:
nginx: [warn] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/my_openresty.conf:23
- 别怕,正常的提醒,說没有 Lua 脚本的缓存,整个性能会下载,没关系,测试开发阶段,俺们不论运行性能,先关注开发效率!
- 然后,增补 readme.lua
-- readme for my openresty app.
VERTION="URIsAok4openresty v12.03.6"
ngx.say(VERTION
,"\n\tusage:"
,"$crul -d 'uri=http://sina.com' locahost:9090/=/chk"
)
- 不用重启, 再次 curl 就发现返回已经变化了!
$ curl localhost:9090/readme
URIsAok4openresty v12.03.6
usage:$crul -d 'uri=http://sina.com' 127.0.0.1:9090/=/chk