【Docker】Nginx 容器化部署

轻云NGINX托管静态网站模块

轻云NGINX 默认挂载了/app的卷。你可以把宿主机上的文件夹挂载到该目录下。

你可以通过轻云UC部署工具直接安装部署,也可以手动按如下文档操作,该项目已经全面开源,可以从如下环境获取

开源地址: https://gitee.com/qingplus/qingcloud-platform

QingHub Nginx部署手册

容器示例

在示例中,我们将创建一个 nginx 客实例。

适用docker 命令行

创建 nginx服务器实例
console 复制代码
docker run -v /path/to/app:/app nginx:latest

或者通过修改docker-compose.yml文件:

yaml 复制代码
services:
  nginx:
  ...
    volumes:
      - /path/to/app:/app
  ...

从主机访问您的服务器

要从主机访问 Web 服务器,您可以要求 Docker 将主机上的随机端口映射到9000端口。

console 复制代码
docker run --name nginx -P nginx:latest

运行docker port以确定 Docker 分配的随机端口。

console 复制代码
$ docker port nginx
9000/tcp -> 0.0.0.0:32769

您还可以手动指定要从主机转发到容器的端口。

console 复制代码
docker run -p 9000:9000 nginx:latest

通过http://localhost:9000来访问您的浏览器中的 Web 服务器。

配置

添加自定义服务器块

默认情况下nginx.conf包括放置在/etc/nginx/nginx.conf。他会包含如下目录/etc/nginx/conf.d中的所有.conf文件,您可以my_server_block.conf在此位置挂载包含自定义服务器块的文件。

例如,为了添加一个服务器块www.example.com

第 1 步:使用以下内容编写文件my_server_block.conf

nginx 复制代码
server {
  listen 0.0.0.0:9100;
  server_name www.example.com;
  root /app;
  index index.htm index.html;
}

第 2 步:将配置挂载为卷

console 复制代码
docker run --name nginx \
  -v /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro \
  nginx:latest

或者通过修改docker-compose.yml文件:

yaml 复制代码
services:
  nginx:
  ...
    volumes:
      - /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro
  ...

使用自定义 SSL 证书

注意:以下步骤假设您使用自定义域名,并且您已将自定义域名配置为指向您的服务器。

第 1 步:准备您的证书文件

在您的本地计算机中,创建一个名为 的文件夹certs并放置您的证书文件。确保将两个文件分别重命名为server.crt和server.key:

console 复制代码
mkdir -p /path/to/nginx-persistence/certs
cp /path/to/certfile.crt /path/to/nginx-persistence/certs/server.crt
cp /path/to/keyfile.key  /path/to/nginx-persistence/certs/server.key
第 2 步:为 SSL 连接提供自定义服务器块

my_server_block.conf使用 SSL 配置和证书的相对路径写入文件:

nginx 复制代码
  server {
    listen       8443 ssl;

    ssl_certificate      certs/server.crt;
    ssl_certificate_key  certs/server.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
      root   html;
      index  index.html index.htm;
    }
  }
第 3 步:运行NGINX开源镜像并打开SSL端口

运行 NGINX image,从主机挂载证书目录。

console 复制代码
docker run --name nginx \
  -v /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro \
  -v /path/to/nginx-persistence/certs:/etc/nginx/certs \
  nginx:latest

或者通过修改docker-compose.yml文件:

yaml 复制代码
services:
  nginx:
  ...
    volumes:
    - /path/to/nginx-persistence/certs:/etc/nginx/certs
    - /path/to/my_server_block.conf:/etc/nginx/conf.d/my_server_block.conf:ro
  ...

解决重定向问题

默认情况下,NGINX image发出的重定向是相对的。如果您需要激活绝对重定向,您可以设置NGINX_ENABLE_ABSOLUTE_REDIRECT为yes。您应该注意容器正在侦听的端口,否则它不会出现在重定向除非NGINX_ENABLE_PORT_IN_REDIRECT也设置为yes。

在下面几行中,您可以看到解释重定向如何工作的不同示例。所有这些都假设我们在服务器块中有以下内容my_redirect_server_block.conf:

nginx 复制代码
server {
  listen 0.0.0.0:9000;
  server_name www.example.com;
  root /app;
  index index.htm index.html;
  location /test/ {
    return 301 /index.html;
  }
}
默认配置
console 复制代码
docker run --name nginx --rm -p 9000:9000 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  nginx:latest

如前所述,NGINX 镜像发出的默认重定向是相对的。客户端应构建最终 URL

console 复制代码
$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: /index.html
...
$ curl -w %{redirect_url}\\n -o /dev/null http://localhost:9000/test/
http://localhost:9000/index.html

请记住,某些旧客户端可能与相对重定向不兼容。

启用绝对重定向
console 复制代码
docker run --name nginx --rm -p 9000:8080 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  nginx:latest

因此,容器将在Location标头中回复完整的 URL,但没有端口。如果您在标准端口(80 或 443)中公开容器,这非常有用

console 复制代码
$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: http://localhost/index.html
...
重定向中的端口已启用
console 复制代码
docker run --name nginx --rm -p 9000:8080 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  -e NGINX_ENABLE_PORT_IN_REDIRECT=yes \
  nginx:latest

在这种情况下,容器将包含它在重定向中侦听的端口,而不是它公开的端口(在示例8080vs9000)

console 复制代码
$ curl -kI http://localhost:9000/test/
HTTP/1.1 301 Moved Permanently
...
Location: http://localhost:8080/index.html
...

要修改这种情况并构建可访问的 URL,您必须在您公开的同一端口中运行侦听容器

console 复制代码
docker run --name nginx --rm -p 9000:9000 \
  -v /path/to/my_redirect_server_block.conf:/etc/nginx/conf.d/my_redirect.conf:ro \
  -e NGINX_ENABLE_ABSOLUTE_REDIRECT=yes \
  -e NGINX_ENABLE_PORT_IN_REDIRECT=yes \
  -e NGINX_HTTP_PORT_NUMBER=9000
  nginx:latest

配置

该镜像配置在/etc/nginx/nginx.conf。您可以nginx.conf使用自己的自定义配置文件覆盖该文件。

console 复制代码
docker run --name nginx \
  -v /path/to/your_nginx.conf:/etc/nginx/nginx.conf:ro \
  nginx:latest

或者通过修改docker-compose.yml此存储库中存在的文件:

yaml 复制代码
services:
  nginx:
  ...
    volumes:
      - /path/to/your_nginx.conf:/etc/nginx/nginx.conf:ro
  ...

反向代理到其他容器

NGINX 可用于使用 Docker 的链接系统反向代理到其他容器。如果您想通过 NGINX 前端提供动态内容,这尤其有用。为此,请在文件夹中添加如下所示的服务器块/etc/nginx/conf.d/:

nginx 复制代码
server {
    listen 0.0.0.0:8080;
    server_name yourapp.com;
    access_log /etc/nginx/logs/yourapp_access.log;
    error_log /etc/nginx/logs/yourapp_error.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://[your_container_alias]:[your_container_port];
        proxy_redirect off;
    }
}

参考:

日志

console 复制代码
docker logs nginx

或使用 Docker Compose:

console 复制代码
docker-compose logs nginx

如果您希望以不同方式使用容器日志,您可以使用该选项配置容器日志记录驱动程序。--log-driver在默认配置中,docker 使用json-file驱动程序。

相关推荐
IvanCodes2 小时前
一、Docker:一场颠覆应用部署与运维的容器革命
docker·容器
栗子~~2 小时前
Milvus docker-compose 部署
docker·容器·milvus
椰汁菠萝3 小时前
ubuntu下免sudo执行docker
ubuntu·docker·免sudo
紫璨月3 小时前
nginx反向代理的bug
运维·nginx·bug
没有名字的小羊3 小时前
2.安装Docker
运维·docker·容器
xiezhr3 小时前
50 个常用 Docker 命令
运维·docker·容器
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
退役小学生呀9 天前
三、kubectl使用详解
云原生·容器·kubernetes·k8s
小生云木10 天前
Linux离线编译安装nginx
linux·运维·nginx
API开发10 天前
苹果芯片macOS安装版Homebrew(亲测) ,一键安装node、python、vscode等,比绿色软件还干净、无污染
vscode·python·docker·nodejs·openssl·brew·homebrew