本项目提供了 OpenResty 的两种使用方式:
docker build -t docker.cnb.cool/zhiqiangwang/openresty:latest .
docker run -d -p 80:80 --name openresty docker.cnb.cool/zhiqiangwang/openresty:latest
nginx.conf - 主 Nginx 配置文件,定义了全局配置和包含了 conf.d 目录下的所有配置文件conf.d/default.conf - 默认站点配置,监听 80 端口,根目录为 html| 环境变量 | 说明 | 默认值 |
|---|---|---|
NGINX_CONF | 自定义 Nginx 配置文件路径或 URL | 空 |
PATH | 系统路径,包含 OpenResty 相关目录 | 已配置 |
LUA_PATH | Lua 模块搜索路径 | 已配置 |
LUA_CPATH | Lua C 模块搜索路径 | 已配置 |
docker run -d -p 80:80 -v /path/to/nginx.conf:/etc/nginx/nginx.conf -e NGINX_CONF=/etc/nginx/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 -e NGINX_CONF=https://example.com/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 -v /path/to/html:/usr/local/openresty/nginx/html docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 -v /path/to/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf -v /path/to/conf.d:/usr/local/openresty/nginx/conf/conf.d docker.cnb.cool/zhiqiangwang/openresty:latest
本镜像包含了 apache2-utils 包,其中包含了 htpasswd 命令,可用于创建基本认证的密码文件。
# 在容器中创建密码文件
docker exec -it openresty htpasswd -bc /usr/local/openresty/nginx/conf/.htpasswd admin admin
# 或者在宿主机创建密码文件,然后挂载到容器
touch .htpasswd
htpasswd -bc .htpasswd admin admin
docker run -d -p 80:80 -v $(pwd)/.htpasswd:/usr/local/openresty/nginx/conf/.htpasswd docker.cnb.cool/zhiqiangwang/openresty:latest
在 Nginx 配置文件中添加以下配置,启用基本认证:
server { listen 80; server_name localhost; # 启用基本认证 auth_basic "Restricted Area"; auth_basic_user_file /usr/local/openresty/nginx/conf/.htpasswd; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
docker run -d -p 80:80 -v $(pwd)/.htpasswd:/usr/local/openresty/nginx/conf/.htpasswd -v $(pwd)/default.conf:/usr/local/openresty/nginx/conf/conf.d/default.conf docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 8080:80 docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 -v $(pwd)/html:/usr/local/openresty/nginx/html docker.cnb.cool/zhiqiangwang/openresty:latest
docker run -d -p 80:80 -e NGINX_CONF=https://raw.githubusercontent.com/example/nginx-config/master/nginx.conf docker.cnb.cool/zhiqiangwang/openresty:latest
支持一键安装 OpenResty,兼容 Ubuntu、Debian、CentOS 和 RHEL 系统。
curl -o- https://cnb.cool/zhiqiangwang/openresty/-/git/raw/main/install.sh | bash
| 系统 | 版本 | 架构 |
|---|---|---|
| Ubuntu | 18.04 及以上 | amd64, arm64 |
| Debian | 10 及以上 | amd64, arm64 |
| CentOS | 7 及以上 | amd64, arm64 |
| RHEL | 7 及以上 | amd64, arm64 |
安装脚本会自动完成以下操作:
如果您想先下载脚本再运行:
# 下载脚本
wget https://cnb.cool/zhiqiangwang/openresty/-/git/raw/main/install.sh
# 添加执行权限
chmod +x install.sh
# 运行脚本
sudo ./install.sh
# 查看 OpenResty 版本
openresty -v
# 启动 OpenResty
sudo systemctl start openresty
# 设置开机自启
sudo systemctl enable openresty
# 查看 OpenResty 状态
sudo systemctl status openresty
安装完成后,OpenResty 的主要目录结构:
| 目录 | 说明 |
|---|---|
/usr/local/openresty/ | OpenResty 安装根目录 |
/usr/local/openresty/nginx/ | Nginx 相关文件 |
/usr/local/openresty/nginx/conf/ | 配置文件目录 |
/usr/local/openresty/nginx/html/ | 默认网站根目录 |
/usr/local/openresty/nginx/logs/ | 日志文件目录 |
/usr/local/openresty/lualib/ | Lua 库目录 |
# 启动 OpenResty
sudo openresty
# 停止 OpenResty
sudo openresty -s stop
# 重启 OpenResty
sudo openresty -s reload
# 测试配置文件
sudo openresty -t
# 查看版本信息
openresty -v
1. 如果安装过程中出现 GPG 公钥错误
# 手动导入 GPG 公钥
wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/openresty.gpg
2. 如果软件源添加失败
# Ubuntu/Debian - 手动添加源
sudo tee /etc/apt/sources.list.d/openresty.list <<EOF
deb http://openresty.org/package/ubuntu $(lsb_release -sc) main
EOF
# CentOS/RHEL - 手动添加源
sudo wget -O /etc/yum.repos.d/openresty.repo https://openresty.org/package/centos/openresty.repo
3. 如果服务无法启动
# 检查配置文件语法
sudo openresty -t
# 查看错误日志
sudo tail -f /usr/local/openresty/nginx/logs/error.log
如需卸载 OpenResty:
# Ubuntu/Debian
sudo apt-get remove openresty
# CentOS/RHEL
sudo yum remove openresty