docker安装OpenResty

目录
一、拉取镜像
docker pull openresty/openresty
二、启动
docker run --name openresty -p 80:80 -d openresty/openresty
三、复制配置文件
1、创建宿主机目录
mkdir /usr/local/openresty
cd /usr/local/openresty
# 存放nginx的配置文件
mkdir conf
# 存放lua脚本
mkdir lua
2、拷贝容器中nginx配置文件到宿主机目录
docker cp openresty:/usr/local/openresty/nginx/conf/nginx.conf /usr/local/openresty/conf
# 拷贝lua库
docker cp openresty:/usr/local/openresty/lualib /usr/local/openresty/
四、删除容器,启动新容器
删除 openresty 容器
docker rm -f openresty
配置启动 openresty,配置自动启动
docker run -p 80:80 \
--name openresty --restart always \
-v /usr/local/openresty/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /etc/localtime:/etc/localtime \
openresty/openresty
# 或者修改启动端口,去掉自动启动,增加lua脚本映射目录
docker run --name openresty \
-v /usr/local/openresty/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /usr/local/openresty/lua/:/usr/local/openresty/nginx/lua \
-v /usr/local/openresty/lualib/:/usr/local/openresty/lualib \
-p 80:80 -d openresty/openresty
五、测试lua模块
1、新建item.lua
在/usr/local/openresty/lua/新建item.lua
-- 返回假数据,这里的ngx.say()函数,就是写数据到Response中
ngx.say('{"id":"10001","name":"SALSA"}')
2、修改配置文件nginx.conf
http {
# 隐藏版本号
server_tokens off;
include mime.types;
default_type application/octet-stream;
underscores_in_headers on;#表示如果header name中包含下划线,则不忽略
sendfile on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
#include /etc/nginx/conf.d/*.conf;
#lua 模块
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
#c模块
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
server {
listen 80;
server_name 127.0.0.1;
location /api/item {
# 默认的响应类型
default_type application/json;
# 响应结果有lua/item.lua文件来决定
content_by_lua_file lua/item.lua;
}
location / {
root html;
index index.html index.htm;
}
}
}