| 
 
 官网文档链接  
openresty 二进制安装文档链接  openresty 指令文档  openresty api文档  
安装 
openresty-1.19.3.2.tar.gz安装包下载  
wget "https://openresty.org/download/openresty-1.19.3.2.tar.gz"
  
安装前先安装依赖包  
yum install pcre-devel openssl-devel gcc curl postgresql-devel
cd openresty-1.19.3.2
./configure --help 查看编译选项
./configure --prefix=/opt/openresty \
            --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module \
            --with-http_postgres_module
  
成功后使用下面的命令来编译  
make -j4  -j选项指定使用多核CPU  我的机器是4核就-j4
  
如果前面的步骤都没有问题的话,您可以使用下面的命令安装 OpenResty 到您的系统中  
make install
  
添加nginx环境变量  
export NGINX_HOME=/opt/openresty/nginx
export PATH=$PATH:$NGINX_HOME/sbin
  
查看nginx安装的状态  
[root@VM-52-124-centos nginx]# nginx -V
nginx version: openresty/1.19.3.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../ngx_postgres-1.0 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.19 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.9 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module
  
Nginx常用lua指令解析 
content_by_lua/content_by_lua_file  context: location,location if  
作用:内容处理器,接收请求处理并输出响应  
header_filter_by_lua/header_filter_by_lua_file  context: http,server,location,location if  
作用:设置header和cookie  
body_filter_by_lua/body_filter_by_lua_file  context: http,server,location,location if  作用:log阶段处理,比如记录访问量/统计平均响应时间  
init_by_lua_block/init_by_lua_file  context: http  作用:nginx Master进程加载配置时执行; 通常用于初始化全局配置/预加载Lua模块  
set_by_lua/set_by_lua_file  context: server,server if,location,location if  作用:设置nginx变量,可以实现复杂的赋值逻辑;此处是阻塞的,Lua代码要做到非常快  
rewrite_by_lua/rewrite_by_lua_file  context: http,server,location,location if  
作用:rewrite阶段处理,可以实现复杂的转发/重定向逻辑  
access_by_lua/access_by_lua_file  context: http,server,location,location if  
作用:请求访问阶段处理,用于访问控制  
NGINX常用LUA api解析 
ngx.var.scheme  –获取请求协议(http,https)  ngx.var.remote_addr  –获取请求客户端地址  ngx.var.remote_port  –获取请求客户端端口  ngx.var.server_name  –获取请求的域名  ngx.var.request_method  –获取请求方法(GET,POST)  ngx.req.get_uri_args()  –获取请求参数  ngx.req.read_body()  –读取post请求中的请求体  ngx.req.get_body_data()  –获取post请求参数  ngx.req.get_post_args()  –获取post请求得到一个table  
示例 
hello, world示例  
location / {
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
输出结果:
<p>hello, world</p>
  
获取GET请求参数  
location / {
                default_type text/html;
                content_by_lua_block {
                      local args = ngx.req.get_uri_args()
            		    ngx.say(ngx.var.args)
         	        ngx.say(args["id"])
            }
        }
curl  http://127.0.0.1/?id=555 输出结果:
id=555
555
  
获取post请求参数  
location / {
                default_type text/html;
                content_by_lua_block {
                    ngx.req.read_body()
                    local args = ngx.req.get_body_data()
                    ngx.say(args)
            }
        }
curl -d "id=777" http://127.0.0.1 输出结果:
id=777
  
获取post参数得到table数据类型  
location / {
                default_type text/html;
                content_by_lua_block {
                    ngx.req.read_body()
                    local args = ngx.req.get_post_args()
                    for key, value in pairs(args) do
                        ngx.say("key="..key..";value="..value..";")
                    end
            }
        }
curl -d "id=777" http://127.0.0.1 输出结果:
key=id;value=777;
 
                
                
                
        
    
 
 |