Openresty初次使用

441次阅读
没有评论

共计 5421 个字符,预计需要花费 14 分钟才能阅读完成。

Openresty初次使用

博主是在2019年时才接触到openresty,当时通勤路上看到一篇关于动态更新nginx配置的微信文章,遂有感而搜,搜到大量相关的知识点:有用confd动态渲染配置,也有微博开源的模块upstream-sync。。。。无意间看到有个openresty的文章,所以就有了这篇文章。知识就是这样,看着看着,搜着搜着,奇奇怪怪的技能就增加了。之前从来没在项目中用过,虽然现在也是,一年多的时间重新浏览了微信收藏文章时,再次倒腾倒腾记录下

Openresty官方简介

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

OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

openresty安装

安装依赖包

[root@nginx-cluster ~]# yum install pcre-devel openssl-devel gcc curl

编译安装

[root@nginx-cluster src]# wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
[root@nginx-cluster src]# tar -zvxf openresty-1.17.8.2.tar.gz
[root@nginx-cluster openresty-1.17.8.2]# cd openresty-1.17.8.2/
[root@nginx-cluster openresty-1.17.8.2]# ./configure
[root@nginx-cluster openresty-1.17.8.2]# make -j 4 && make isntall
[root@nginx-cluster openresty-1.17.8.2]# ll /usr/local/openresty/
total 256
drwxr-xr-x  2 root root    123 Dec 19 17:21 bin
-rw-r--r--  1 root root  22924 Dec 19 17:21 COPYRIGHT
drwxr-xr-x  6 root root     56 Dec 19 17:21 luajit
drwxr-xr-x  6 root root    116 Dec 19 17:21 lualib
drwxr-xr-x 11 root root    151 Dec 19 17:26 nginx
drwxr-xr-x 47 root root   4096 Dec 19 17:21 pod
-rw-r--r--  1 root root 229856 Dec 19 17:21 resty.index
drwxr-xr-x  5 root root     47 Dec 19 17:21 site

# 查看服务版本以及配置
[root@nginx-cluster ~]# /usr/local/openresty/nginx/sbin/nginx -V
nginx version: openresty/1.17.8.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=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.1 --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=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.17 --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=../redis2-nginx-module-0.15 --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.8 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module

配置systemd启动文件

[root@nginx-cluster ~]# systemctl cat openresty.service
# /usr/lib/systemd/system/openresty.service
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 重载systemd服务
[root@nginx-cluster ~]# systemctl daemon-reload

调整openresty配置

# 新创建配置目录
[root@nginx-cluster ~]# mkdir /usr/local/openresty/nginx/conf/conf.d

# 修改主配置文件
[root@nginx-cluster ~]# egrep -v '^$|^#|[[:space:]]+#' /usr/local/openresty/nginx/conf/nginx.conf
worker_processes  4;
events {
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    include conf.d/*;
}

# 创建新的配置
[root@nginx-cluster ~]# cat /usr/local/openresty/nginx/conf/conf.d/openresty.xadocker.cn.conf
server {
    listen       80;
    server_name  openresty.xadocker.cn;

    location /lua{
        default_type 'text/html';
        content_by_lua 'ngx.say("<h1>HELLO: XADOCKER,Greating From OpenResty </h1>")';
    }
}


# 校验配置语法
[root@nginx-cluster ~]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

测试服务

# 开启服务
[root@nginx-cluster ~]# systemctl start openresty
[root@nginx-cluster ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11426/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      989/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1124/master
tcp6       0      0 :::22                   :::*                    LISTEN      989/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1124/master
udp        0      0 127.0.0.1:323           0.0.0.0:*                           735/chronyd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           792/dhclient
udp6       0      0 ::1:323                 :::*                                735/chronyd

# 访问测试
[root@nginx-cluster ~]# curl openresty.xadocker.cn/lua
<h1>HELLO: XADOCKER,Greating From OpenResty </h1>

demo样例

获取请求header

    location /getheader {
        default_type 'text/html';
        content_by_lua_block{
        if ngx.var.request_method == "GET" then
            local headers = ngx.req.get_headers()
            for k,v in pairs(headers) do
                ngx.say("[header] name:", k, " v:", v)
                ngx.say("<br>")
            end
        end
        }
    }

获取GET请求参数

    location /getargs {
        default_type 'text/html';
        content_by_lua_block{
        if ngx.var.request_method == "GET" then
            local _a = ngx.var.arg_a
            ngx.log(ngx.ERR,"args:a".._a)

            local g_args = ngx.req.get_uri_args()
            local _args = {}
            for k,v in pairs(g_args) do
                if type(v) == "table" then
                    table.insert(_args,k.."="..table.concat(v,"|"))
                else
                    table.insert(_args,k.."="..v)
                end
            end
        end
        ngx.log(ngx.ERR,"args:"..table.concat(_args,"&"))
        }
    }

获取post请求参数

    location /postargs {
        default_type 'text/html';
        content_by_lua_block{
        if ngx.var.request_method == "POST" then
            ngx.req.read_body()
            local postargs=ngx.req.get_post_args()
            for k,v in pairs(postargs)
            do
                ngx.say("POST key: ",k," value:",v)
                ngx.say("<br>")
            end
        end
        }
    }

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2021-03-20发表,共计5421字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)