openresty docker
时间: 2025-01-01 07:30:10 浏览: 54
### 使用 OpenResty 和 Docker 的配置与设置
#### 安装并运行 OpenResty 镜像
为了在 Docker 中使用 OpenResty,可以基于官方镜像创建容器。下面是一个简单的 `Dockerfile` 示例:
```dockerfile
FROM openresty/openresty:alpine
COPY ./html /usr/local/openresty/nginx/html/
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
```
此文件定义了一个新的 Docker 映像,它从官方的 OpenResty 映像开始,并复制本地目录中的 HTML 文件和 Nginx 配置到映像中。
#### 创建自定义 Nginx 配置
Nginx 配置文件通常位于 `/etc/nginx/nginx.conf` 或者在这个例子中是在 `/usr/local/openresty/nginx/conf/nginx.conf` 下面。这里提供一个基本的例子来展示如何配置 OpenResty 来托管静态页面或作为反向代理服务器[^1]。
```nginx
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
```
这段配置指定了工作进程的数量、事件模块参数以及 HTTP 块内的多个指令,其中包括监听端口和服务名称等信息。还设置了默认首页位置和其他错误页的位置。
#### 构建和启动容器
完成上述准备工作之后,在命令行工具里执行如下操作即可构建新映像并启动容器:
```bash
$ docker build -t my-openresty .
$ docker run --name some-openresty -d -p 80:80 my-openresty
```
这会下载所需的依赖项并将它们打包成一个新的 Docker 映像;然后以前台模式启动该映像的一个实例,同时将主机上的端口 80 转发给容器内部相同的端口。
阅读全文
相关推荐

















