openresty 入门

官网文档链接

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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值