1 拉取Openresty镜像

1
docker pull openresty/openresty:bullseye

镜像版本可以按需选择,带有-fat的镜像包含额外的包,只使用Nginx基本功能时无需安装。

2 创建本地Nginx挂载目录

为方便管理,需要将Nginx的配置文件,Html页面文件和Nginx日志文件存储在Docker容器外,容器启动时将对应目录挂载到容器中。需要挂载的文件及目录有:

用途 本地目录 容器内目录
Nginx配置 /home/ubuntu/openresty-docker/openresty/nginx/conf /usr/local/openresty/nginx/conf
SSL证书 /home/ubuntu/openresty-docker/openresty/nginx/certs /usr/local/openresty/nginx/certs
Html文件 /home/ubuntu/openresty-docker/openresty/nginx/html /usr/local/openresty/nginx/html
Nginx日志 /home/ubuntu/openresty-docker/openresty/nginx/logs /usr/local/openresty/nginx/logs
同步时区(只读) /etc/localtime /etc/localtime
1
2
3
# 创建目录,只需要创建证书文件目录和日志文件目录,其他目录复制配置文件时自动创建
mkdir -p /home/ubuntu/openresty-docker/openresty/nginx/certs
mkdir -p /home/ubuntu/openresty-docker/openresty/nginx/logs

创建日志目录是想用外部新建的空目录挂载时“覆盖”容器内的日志目录,这样做是为了将Nginx日志输出到文件中。Openresty默认的做法是在容器内的日志目录中创建了两个符号链接参见

  • /usr/local/openresty/nginx/logs/access.log -> /dev/stdout
  • /usr/local/openresty/nginx/logs/error.log -> /dev/stderr

将日志输出到容器内的终端,使用docker logs openresty查看,但是在容器外的挂载目录中是无法查看的。用容器外部空目录挂载后,这两个符号链接会被删除,Nginx启动(容器启动)时会创建日志文件。这样在容器外就可以直接查看日志文件了。

3 获取Openresty默认配置文件

由于挂载的本地目录会替换容器本身的目录,如果直接挂载上述空目录,缺少相关的配置文件会导致容器启动失败,需要获取Openresty默认的配置文件,将其复制到对应本地目录中。临时启动一次容器,将容器中需要挂载到容器外部的文件复制出来。需要注意Openresty中的Nginx配置分为两个部分,一部分位于openresty/nginx/conf目录下,一部分位于/etc/nginx/conf.d/*(默认状态只有default.conf),全部需要复制到容器外

1
2
3
4
5
6
7
8
# 创建并运行容器,指定 --rm 参数停止后自动删除
docker run --name openresty -d -p 80:80 --rm openresty/openresty:bullseye
# 从容器中复制配置文件夹到本地目录
docker cp openresty:/usr/local/openresty/nginx/conf /home/ubuntu/openresty-docker/openresty/nginx
docker cp openresty:/usr/local/openresty/nginx/html /home/ubuntu/openresty-docker/openresty/nginx
docker cp openresty:/etc/nginx/conf.d/default.conf /home/ubuntu/openresty-docker/openresty/nginx/conf/nginx_server.conf
# 停止容器后自动删除
docker stop openresty

4 启动Openresty容器

4.1 使用命令行启动Openresty容器(不推荐)

1
2
3
4
5
6
7
8
9
10
11
docker run -id \
-p 80:80 \
-p 443:443 \
--name openresty \
--restart always \
-v /home/ubuntu/openresty-docker/openresty/nginx/conf:/usr/local/openresty/nginx/conf \
-v /home/ubuntu/openresty-docker/openresty/nginx/certs:/usr/local/openresty/nginx/certs \
-v /home/ubuntu/openresty-docker/openresty/nginx/logs:/usr/local/openresty/nginx/logs \
-v /home/ubuntu/openresty-docker/openresty/nginx/html:/usr/local/openresty/nginx/html \
-v /etc/localtime:/etc/localtime:ro \
openresty/openresty:bullseye

Openresty 容器运行时无需指定--privileged=true参数,也不需要指定用户-u UID:GID参数。

4.2 使用docker compose启动(推荐)

/home/ubuntu/openresty-docker/目录中创建docker-compose.yml,文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# docker-compose.yml
services:
openresty:
image: openresty/openresty:bullseye
container_name: openresty
ports:
- "80:80"
- "443:443"
restart: always
volumes:
- "/home/ubuntu/openresty-docker/openresty/nginx/conf:/usr/local/openresty/nginx/conf"
- "/home/ubuntu/openresty-docker/openresty/nginx/certs:/usr/local/openresty/nginx/certs"
- "/home/ubuntu/openresty-docker/openresty/nginx/logs:/usr/local/openresty/nginx/logs"
- "/home/ubuntu/openresty-docker/openresty/nginx/html:/usr/local/openresty/nginx/html"
- "/etc/localtime:/etc/localtime:ro"

使用命令docker compose up -d运行容器,使用命令docker compose down停止并删除容器。

5 Nginx配置文件

nginx.conf文件中关于docker-openresty的nginx配置文件的注释:

nginx.conf – docker-openresty

This file is installed to:
/usr/local/openresty/nginx/conf/nginx.conf
and is the file loaded by nginx at startup, unless the user specifies otherwise.

It tracks the upstream OpenResty’s nginx.conf, but removes the server section and adds this directive:
include /etc/nginx/conf.d/*.conf;

The docker-openresty file nginx.vh.default.conf is copied to
/etc/nginx/conf.d/default.conf. It contains the server section
of the upstream nginx.conf.

See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files

参考构建docker-openresty的 Dockerfile及有关Nginx配置文件的README,docker-openresty中Nginx的配置文件由两部分组成:

  • 主要配置文件为/usr/local/openresty/nginx/conf/nginx.conf,对应GitHub中的文件nginx.conf
  • nginx.conf引用了/etc/nginx/conf.d/*.conf,初始状态下只有default.conf,对应GitHub中的文件nginx.vh.default.conf

为方便管理,将配置文件合并到nginx.conf并挂载到容器外部。

5.1 合并修改配置文件

/home/ubuntu/openresty-docker/openresty/nginx/conf/nginx.conf文件尾部的include /etc/nginx/conf.d/*.conf;注释,并将/home/ubuntu/openresty-docker/openresty/nginx/conf/nginx_server.conf文件(即conf.d/default.conf,复制到容器外时将其重命名为nginx_server.conf)的全部内容追加到注释掉的include后。

重启docker容器或运行nginx重载命令生效配置:

1
2
3
docker restart openresty
# 或
docker exec -i openresty nginx -t && nginx -s reload

通过docker exec使用nginx -s reload命令时会出现:

1
2
3
4
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2023/11/30 16:11:16 [warn] 3085487#3085487: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2023/11/30 16:11:16 [notice] 3085487#3085487: signal process started
2023/11/30 16:11:16 [error] 3085487#3085487: open() "/run/nginx.pid" failed (2: No such file or directory)

若进入容器终端docker exec -it openresty bash后,运行命令则正常,原因未知。


本站由 @gsh1209 使用 Stellar 主题创建
Copyright © 2023 - BG3LNT.XYZ
Favicon图标来自 @ChenCJ
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处
正在计算运行时间...

蒙ICP备2022000455号-2