使用Docker安装OpenResty并使用示例学习
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
开始使用
The default virtual host configuration has the original OpenResty configuration and is copied to /etc/nginx/conf.d/default.conf
.
创建 conf.d/nginx.conf
配置文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
nginx.vh.default.conf -- docker-openresty
#
# This file is installed to:
# `/etc/nginx/conf.d/default.conf`
#
# It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
#
# This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root /usr/local/openresty/nginx/html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
|
启动OpenResty服务
1
|
docker run -v ./conf.d:/etc/nginx/conf.d -p 80:80 --name openresty -d openresty/openresty:1.25.3.2-alpine-aarch64
|
修改配置如下,增加新的路由配置如下,并重新加载配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
worker_processes 1; #nginx worker 数量
error_log logs/error.log; #指定错误日志文件路径
events {
worker_connections 1024;
}
http {
server {
#监听端口,若你的80端口已经被占用,则需要修改
listen 80;
location /hi {
default_type text/html;
content_by_lua_block {
ngx.say("Hello World!")
}
}
}
}
|
执行如下命令重新加载配置
1
2
3
4
5
6
|
docker exec -it openresty nginx -s reload
# or
~ docker exec openresty /usr/local/openresty/nginx/sbin/nginx -s reload
2025/07/27 09:27:40 [notice] 9#9: signal process started
|
通过请求头动态路由
- 创建html目录,并启动OpenResty服务
1
2
3
4
5
|
docker run -v ./conf.d:/etc/nginx/conf.d \
-v ./html:/usr/local/openresty/nginx/html/:rw \
-v ./logs:/usr/local/openresty/nginx/logs/:rw \
-v ./lua:/usr/local/openresty/nginx/lua/:rw \
-p 80:80 --name openresty -d openresty/openresty:1.25.3.2-alpine-aarch64
|
或者可以直接在容器中添加html文件和修改配置。
- 在html目录下创建两个html文件,分别为index_v1.html, index_v2.html文件,内容可以随意填写。
复制到容器中
1
2
3
|
docker cp html/index_v1.html openresty:/usr/local/openresty/nginx/html
docker cp html/index_v2.html openresty:/usr/local/openresty/nginx/html
|
- 创建lua/router.lua文件配置,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-- router.lua
local Router = {
V1 = "http://127.0.0.1:80/index_v1.html",
V2 = "http://127.0.0.1:80/index_v2.html",
}
local target = "V1" // default v1
local specific_header = ngx.req.get_headers()["X-Version"]
if specific_header then
target = specific_header
end
ngx.var.target = Router[target]
|
- 修改conf/nginx.html文件,添加lua文件路径配置,内容如下:
直接修改容器配置
1
|
docker exec -it openresty vi /usr/local/openresty/nginx/conf/nginx.conf
|
添加如下内容
1
2
3
4
5
6
|
http {
include mime.types;
default_type application/octet-stream;
# 添加的行
lua_package_path "/usr/local/openresty/nginx/lua/?.lua;;";
|
- 增加新web服务配置
1
2
3
4
5
6
7
8
9
10
|
server {
listen 8080;
set $target '';
location / {
access_by_lua_file /etc/nginx/conf.d/router.lua;
proxy_pass $target;
}
}
|
- 加载配置后访问
1
2
3
|
curl -H "X-Version: V1" http://127.0.0.1:8080
curl -H "X-Version: V2" http://127.0.0.1:8080
|
参考