OpenResty是一个基于Nginx与Lua的高性能Web平台,可以用于Web服务动态网关等,能够处理极高的并发。所以对于我们来说学好OpenResty是很有必要的。
OpenResty由四个核心组件构成,如下表所示:
组件名称 | 说明 |
---|---|
Nginx | Web服务器 |
LuaJIT | Lua语言解释器 |
ngx_lua | 处理HTTP协议,让Lua嵌入在Nginx中运行 |
stream_lua | 也是让Lua在Nginx中运行,负责处理TCP/UDP协议 |
源码编译安装
使用的服务器信息:
系统 | Ubuntu22.04 |
---|---|
服务器 | IP 172.16.183.131 |
安装OpenResty:
# 安装编译OpenResty必要的工具
apt-get update -y
apt-get install -y \
libpcre3-dev \
libssl-dev \
perl \
make \
build-essential \
curl \
zlib1g-dev
# 安装软件包说明
libpcre3-dev:PCRE是 ‘Perl Compatible Regular Expressions’(Perl兼容的正则表达式)的缩写。PCRE库由一系列函数组成,实现了与Perl5相同的语法、语义的正则表达式匹配功能。
zlib1g-dev: zlib 是实现 deflate 压缩算法的一个库,该算法是 gzip 和 PKZIP 的基础。该软件包包括了开发支持文件。
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
# 解压下载的源码包
tar -xf openresty-1.21.4.1.tar.gz
# 进入目录
cd openresty-1.21.4.1/
# 编译前配置
./configure
# 编译
gmake -j 2
# 安装
gmake install
# 执行完安装命令后,OpenResty会默认安装在/usr/local/openresty目录
# 查看安装目录文件
root@ubuntu:~/openresty-1.21.4.1# ls /usr/local/openresty/
bin COPYRIGHT luajit lualib nginx pod resty.index site
root@ubuntu:~/openresty-1.21.4.1#
# 为了方便使用,将目录添加到环境变量中
# 编辑家目录的.bashrc文件
vim ~/.bashrc
# 添加如下内容
export PATH=/usr/local/openresty/bin:$PATH
# 刷新当前shell环境,使刚才修改生效
source .bashrc
1.3.3. 使用OpenResty
1.3.3.1. 启动与停止
# 启动OpenResty
/usr/local/openresty/bin/openresty
# 启动后监听在80端口,使用curl命令访问该端口
root@ubuntu:/usr/local/openresty# curl -vo /dev/null 127.0.0.1
* Trying 127.0.0.1:80...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: openresty/1.21.4.1 # 可以看到服务器是OpenResty
< Date: Sat, 24 Jun 2023 06:03:08 GMT
< Content-Type: text/html
< Content-Length: 1097
< Last-Modified: Sat, 24 Jun 2023 05:44:50 GMT
< Connection: keep-alive
< ETag: "649682d2-449"
< Accept-Ranges: bytes
<
{ [1097 bytes data]
100 1097 100 1097 0 0 71049 0 --:--:-- --:--:-- --:--:-- 78357
* Connection #0 to host 127.0.0.1 left intact
# 停止OpenResty
/usr/local/openresty/bin/openresty -s stop
使用OpenResty
# 启动OpenResty
/usr/local/openresty/bin/openresty
# 启动后监听在80端口,使用curl命令访问该端口
root@ubuntu:/usr/local/openresty# curl -vo /dev/null 127.0.0.1
* Trying 127.0.0.1:80...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: openresty/1.21.4.1 # 可以看到服务器是OpenResty
< Date: Sat, 24 Jun 2023 06:03:08 GMT
< Content-Type: text/html
< Content-Length: 1097
< Last-Modified: Sat, 24 Jun 2023 05:44:50 GMT
< Connection: keep-alive
< ETag: "649682d2-449"
< Accept-Ranges: bytes
<
{ [1097 bytes data]
100 1097 100 1097 0 0 71049 0 --:--:-- --:--:-- --:--:-- 78357
* Connection #0 to host 127.0.0.1 left intact
# 停止OpenResty
/usr/local/openresty/bin/openresty -s stop
如下图所示以OpenResty为核心的架构图:
可以看到OpenResty之强大,可以作为反向代理,也能够直接访问Redis等数据库。
# 文件名为hello.conf
worker_processes 1;
events {
worker_connections 128;
}
http {
server {
listen 80;
server_name *.*;
location / {
content_by_lua_block { # 使用OpenResty的专用指令,可以产生响应内容
ngx.print("Hello, OpenResty\n") # 输出内容
}
}
}
}
# 使用刚才创建的配置文件启动OpenResty
/usr/local/openresty/bin/openresty -c "`pwd`/hello.conf"
# 可以看到打印了刚才的内容
root@ubuntu:/usr/local/openresty/my_nginx_conf# curl 127.0.0.1
Hello, OpenResty
root@ubuntu:/usr/local/openresty/my_nginx_conf#
只支持GET和POST方法 |
---|
只支持HTTP 1.1/2 协议 |
只允许某些用户访问服务 |
GET方法获取当前时间,以HTTP时间格式输出 |
POST方法在请求体里传入时间戳,服务器转换为http时间格式输出 |
可以使用URI参数 “need_encode=1”,输出会做Base64编码 |
worker_processes 1;
events {
worker_connections 128;
}
http {
server {
listen 80;
server_name *.*;
location / {
content_by_lua_block {
ngx.print("Hello, OpenResty\n")
}
}
location = /example {
rewrite_by_lua_file service/http/rewrite_example.lua;
access_by_lua_file service/http/access_example.lua;
content_by_lua_file service/http/content_example.lua;
body_filter_by_lua_file service/http/body_filter_example.lua;
}
}
}
-- service/http/rewrite_example.lua
local method = ngx.req.get_method() -- 获取请求方法
if method ~= 'GET' and -- 请求方法必须是GET或者POST
method ~= 'POST' then
ngx.header['Allow'] = 'GET, POST' -- 如果错误的话返回Allow头
ngx.exit(405) -- 响应状态码405,结束请求
end
local version = ngx.req.http_version() -- 获取HTTP请求版本号
if version < 1.1 then -- 如果版本小的话直接退出
ngx.exit(400)
end
ngx.ctx.encode = ngx.var.arg_need_encode -- 在ngx.ctx里存储编码标志量
ngx.header.content_length = nil -- 删除长度头,避免客户端接受数据错误
-- service/http/access_example.lua
local white_list = {["127.0.0.1"] = true} -- ip名单地址
local ip = ngx.var.remote_addr -- 获取客户端地址
if not white_list[ip] then -- 检查客户端地址是否在白名单内,不在的话返回403状态码
ngx.log(ngx.ERR, ip, " is blocked")
ngx.exit(403)
end
-- service/http/content_example.lua
local function action_get() -- 处理GET请求
ngx.req.discard_body() -- 丢掉请求体
local t = ngx.time() -- 获取当前时间戳
ngx.say(ngx.http_time(t)) -- 转换为http格式输出
end
local function action_post() -- 处理POST请求
ngx.req.read_body() -- 读取请求体
local data = ngx.req.get_body_data() -- 获取请求体数据
local num = tonumber(data) -- 转换为数字
if not num then
ngx.log(ngx.ERR, "xxx") -- 记录错误日志
ngx.exit(400)
end
ngx.say(ngx.http_time(num)) -- 转换为http格式输出
end
local actions = {
GET = action_get,
POST = action_post
}
local method = ngx.req.get_method()
actions[method]()
-- service/http/body_filter_example.lua
if ngx.status ~= ngx.HTTP_OK then
return
end
if ngx.ctx.encode then
ngx.arg[1] = ngx.encode_base64(ngx.arg[1]) -- ngx.arg[1] 代表响应体内容
end
# Lua文件所在文件夹
root@ubuntu:/usr/local/openresty/nginx/service/http# pwd
/usr/local/openresty/nginx/service/http
root@ubuntu:/usr/local/openresty/nginx/service/http# ls
access_example.lua body_filter_example.lua content_example.lua rewrite_example.lua
# 重启OpenResty
/usr/local/openresty/bin/openresty -s reload -c "`pwd`/hello.conf"
# 发出GET请求
root@ubuntu:/usr/local/openresty/my_nginx_conf# curl 127.0.0.1/example
Sat, 24 Jun 2023 12:49:00 GMT
# 发出POST请求
root@ubuntu:/usr/local/openresty/my_nginx_conf# curl 127.0.0.1/example -d "1687610991"
Sat, 24 Jun 2023 12:49:51 GMT
# 发起DELETE请求
root@ubuntu:/usr/local/openresty/my_nginx_conf# curl 127.0.0.1/example -X DELETE
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>openresty/1.21.4.1</center>
</body>
</html>
# 响应base64编码
root@ubuntu:/usr/local/openresty/my_nginx_conf# curl 127.0.0.1/example?need_encode=1
R0VUCg==U2F0LCAyNCBKdW4gMjAyMyAxMjo1ODoxMiBHTVQKroot@ubuntu:/usr/local/openresty/my_nginx_conf#
可以看到OpenResty可以提供强大的Web服务器能力,而且OpenResty的功能还远不止这些,其他的功能等着我们学习和探索。
本文分享自 FreeSWITCH中文社区 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!