OpenResty安装

正常的编译安装

wget http://openresty.org/download/ngx_openresty-1.7.4.1.tar.gz
tar -xvf ngx_openresty-1.7.4.1.tar.gz
cd ngx_openresty-1.7.4.1
./configure --prefix=/opt/openresty
make && make install

这里注意,make的时候报错:undefined reference to ‘pcre_free_study’,猜测可能是pcre库找不到的问题,在https://sourceforge.net/projects/pcre/下载pcre,并安装。

安装后,在configure中增加参数–with-pcre=../pcre-8.35,其中注意修改自己pcre库的目录即可。

安装之后,测试下helloworld。例子来自官网。

mkdir ~/work
cd ~/work
mkdir logs/ conf/

新建配置文件nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8085;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

在linux环境变量path中,增加/opt/openresty/nginx/sbin

通过vi /etc/profile, 在文件中追加内容,source /etc/profile。

启动测试的nginx

nginx -p `pwd`/ -c conf/nginx.conf

测试

curl http://localhost:8085/

显示的结果应该是

<p>hello, world</p>

代码说明

content_by_lua    后续lua代码
content_by_lua_file  后续lua代码源文件
lua_code_cache off;     默认是on,关闭后,会取消对lua代码的缓存,实时加载代码的更新,影响性能

常用文档

https://www.nginx.com/resources/wiki/modules/lua/   
https://github.com/openresty/lua-nginx-module         

Lua的执行阶段

  1. init_by_lua http
  2. set_by_lua server, server if, location, location if
  3. rewrite_by_lua http, server, location, location if
  4. access_by_lua http, server, location, location if
  5. content_by_lua location, location if
  6. header_filter_by_lua http, server, location, location if
  7. body_filter_by_lua http, server, location, location if
  8. log_by_lua http, server, location, location if