OpenResty安装、配置与使用

OpenResty简介

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

Lua简介

Lua是一个简洁、轻量、可扩展的程序设计语言,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。

OpenResty安装

1.安装依赖库

yum install readline-devel pcre-devel openssl-devel gcc

2.下载及安装OpenResty

wget https://openresty.org/download/openresty-1.9.15.1.tar.gz
tar xvf openresty-1.9.15.1.tar.gz
cd openresty-1.9.15.1
./configure --with-luajit && make && make install

激活LuaJIT

组件被用于构建 OpenResty。所有的组件可以被激活或禁止。 大部组件默认是激活的,也有部件不是。 LuaJIT、 DrizzleNginxModule、PostgresNginxModule和IconvNginxModule 默认是没有激活的。您需要通过以下选项在编译 OpenResty的时候将它们各自激活, –with-luajit、 –with-http_drizzle_module、 –with-http_postgres_module和 –with-http_iconv_module 。

安装好的OpenResty

《OpenResty安装、配置与使用》 IMG_20160606_2

从上图可以看到,openresty在/usr/local目录下

OpenResty启动

通过下述方式启动Nginx。如果没有任何输出,说明启动成功,-p 指定我们的项目目录,-c 指定配置文件。

/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
/usr/local/openresty/nginx/sbin/nginx -p 'pwd' -c /usr/local/openresty/nginx/conf/nginx.conf

为openresty下的nginx建立软链(非必需)

ln -s /usr/local/openresty/nginx/sbin/nginx  /usr/sbin/nginx

则可使用如下方式启动

/usr/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf

在浏览器中访问:

《OpenResty安装、配置与使用》 IMG_20160606_1

OpenResty配置Lua

由于原生的Nginx日志没有resp_body这一选项,通过在nginx.conf中添加Lua脚本的方式定义resp_body。

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format log_resp_body  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '$request_time $bytes_sent $request_length "$request_body" "$resp_body"';

    access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        access_log  logs/access.index.log  log_resp_body;

        lua_need_request_body on;

        set $resp_body "";

        body_filter_by_lua '
            local resp_body = string.sub(ngx.arg[1], 1, 1000)
            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
            if ngx.arg[2] then
                ngx.var.resp_body = ngx.ctx.buffered
            end
        ';

        location / {
            root   html;
            index  index.html index.htm;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

检测Nginx配置是否正确

/usr/sbin/nginx -t

重启Nginx

/usr/sbin/nginx -s reload

验证Lua配置是否成功

tail -f access.log

《OpenResty安装、配置与使用》 IMG_20160606_3

tail -f access.index.log

《OpenResty安装、配置与使用》 IMG_20160606_4

参考资料:

OpenResty
OpenResty中文站
nginx-lua
lua-nginx-module

20160615更正:

实践证明,上面body_filter_by_lua中的代码存在bug,可通过如下方式更正:

body_filter_by_lua '
    local maxlen = 1000
    ngx.ctx.buffered = ngx.ctx.buffered or ""
    if #ngx.ctx.buffered < maxlen then
        ngx.ctx.buffered = ngx.ctx.buffered .. string.sub(ngx.arg[1], 1, maxlen - #ngx.ctx.buffered)
    end
    if ngx.arg[2] then
        ngx.var.resp_body = ngx.ctx.buffered
    end
';

感谢OpenResty 中文邮件列表

    原文作者:八宝粥BBZ
    原文地址: https://www.jianshu.com/p/a1fb8153dd2d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞