31人参与 • 2025-08-06 • Linux
在现代云原生和微服务架构中,容器化已成为应用部署的标准方式。nginx,作为全球最流行的 web 服务器和反向代理服务器,其容器化部署不仅简化了环境依赖,还极大地提升了部署的灵活性和可移植性。本文将深入探讨 nginx 在 docker 容器环境中的配置文件管理,从基础概念到高级技巧,为你提供一份详尽的实战指南。
环境一致性: “一次构建,到处运行”,避免了开发、测试、生产环境因 nginx 版本或配置差异导致的问题。
快速部署与扩展: 利用 docker 的轻量级特性,可以秒级启动、停止和扩展 nginx 实例。
资源隔离: 每个 nginx 容器拥有独立的网络、文件系统和进程空间,互不干扰。
易于管理: 结合 docker compose 或 kubernetes,可以轻松管理复杂的多服务应用。
版本控制: docker 镜像和配置文件可以纳入版本控制系统(如 git),实现配置的审计和回滚。
docker 官方提供了高质量的 nginx 镜像,是我们的首选。
# 拉取最新稳定版 docker pull nginx:stable # 或者拉取特定版本 (推荐生产环境使用具体版本) docker pull nginx:1.25.3-alpine
镜像标签说明:
nginx:latest
: 最新版本,可能不稳定。nginx:stable
: 稳定版本,推荐生产使用。nginx:<version>
: 特定版本号。nginx:<version>-alpine
: 基于 alpine linux 的轻量级版本,体积更小,但可能缺少某些工具。# 运行一个最基础的 nginx 容器,映射端口 8080 到主机 80 docker run --name my-nginx -d -p 8080:80 nginx:stable
访问 http://localhost:8080
,你应该能看到 nginx 的欢迎页面。但这只是开始,真正的价值在于自定义配置。
这是本文的重点。如何将自定义的 nginx 配置文件注入到容器中?主要有三种策略:
这是最直接、最灵活的方式。将宿主机上的配置文件目录挂载到容器内的 /etc/nginx
目录。
步骤:
1.创建本地配置目录:
mkdir -p ~/my-nginx/conf # 将官方镜像中的默认配置复制出来作为起点 docker run --rm nginx cat /etc/nginx/nginx.conf > ~/my-nginx/conf/nginx.conf mkdir -p ~/my-nginx/conf/conf.d docker run --rm nginx cat /etc/nginx/conf.d/default.conf > ~/my-nginx/conf/conf.d/default.conf
2.修改本地配置文件:
编辑 ~/my-nginx/conf/nginx.conf
和 ~/my-nginx/conf/conf.d/default.conf
进行自定义。
3.运行容器并挂载配置:
docker run --name my-nginx \ -v ~/my-nginx/conf:/etc/nginx \ -p 8080:80 \ -d nginx:stable
优点:
nginx -s reload
即可重载配置,无需重建镜像。缺点:
将配置文件直接打包进自定义的 docker 镜像。这是生产环境的最佳实践,确保了镜像的完整性和可移植性。
步骤:
1.创建项目目录结构:
my-nginx-project/
├── dockerfile
├── nginx.conf
└── conf.d/
└── default.conf
2.编写 dockerfile:
# 使用官方 nginx 镜像作为基础 from nginx:stable # 维护者信息 (可选) label maintainer="your-email@example.com" # 删除默认的配置文件 run rm -rf /etc/nginx/conf.d/default.conf # 将本地的自定义配置文件复制到容器的相应位置 # 注意:copy 指令中的路径是相对于构建上下文的 copy nginx.conf /etc/nginx/nginx.conf copy conf.d/ /etc/nginx/conf.d/ # 可选:复制静态文件 # copy html/ /usr/share/nginx/html/ # 可选:暴露端口 (dockerfile 中的 expose 只是声明,仍需在运行时用 -p 映射) expose 80 # 可选:设置工作目录 workdir /etc/nginx # 容器启动时执行的命令 (通常 nginx 镜像已定义,可省略) # cmd ["nginx", "-g", "daemon off;"]
3.构建自定义镜像:
cd my-nginx-project docker build -t my-custom-nginx:1.0 .
4.运行自定义镜像:
docker run --name my-nginx -d -p 8080:80 my-custom-nginx:1.0
优点:
1.0
, v1.2.3
)清晰标识了配置版本。缺点:
在 docker swarm 或 kubernetes 这样的编排平台中,使用专门的配置管理对象。
docker swarm:
# 创建 config echo "$(cat ~/my-nginx/conf/nginx.conf)" | docker config create nginx_conf - # 运行服务并挂载 config docker service create --name my-nginx \ --config src=nginx_conf,target=/etc/nginx/nginx.conf \ -p 8080:80 nginx:stable
kubernetes (configmap):
# nginx-config.yaml apiversion: v1 kind: configmap metadata: name: nginx-config data: nginx.conf: | # ... 你的 nginx.conf 内容 ... default.conf: | # ... 你的 default.conf 内容 ... --- apiversion: apps/v1 kind: deployment metadata: name: nginx-deployment spec: replicas: 2 selector: matchlabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:stable ports: - containerport: 80 volumemounts: - name: nginx-config-volume mountpath: /etc/nginx/nginx.conf subpath: nginx.conf - name: nginx-config-volume mountpath: /etc/nginx/conf.d/default.conf subpath: default.conf volumes: - name: nginx-config-volume configmap: name: nginx-config
优点: 专为编排设计,安全(支持加密)、动态更新(k8s)、易于管理大量配置。
缺点: 仅限于特定编排平台。
这是 nginx 的核心配置文件。容器中的路径通常是 /etc/nginx/nginx.conf
。
# ==================== 全局块 (global context) ==================== # 定义 nginx 的工作模式和基本属性 user nginx; # 运行 nginx 的用户 (在容器内通常为 nginx) worker_processes auto; # 工作进程数,auto 通常等于 cpu 核心数 error_log /var/log/nginx/error.log warn; # 错误日志级别 (error, warn, notice, info, debug) pid /var/run/nginx.pid; # nginx 主进程 pid 文件路径 # ==================== events 块 (events context) ==================== events { worker_connections 1024; # 每个 worker 进程允许的最大连接数 # use epoll; # linux 高效的 i/o 多路复用机制,通常自动选择 # multi_accept on; # 允许一个 worker 进程同时接受多个新连接 } # ==================== http 块 (http context) ==================== # 这是配置 http 服务器的核心 http { # ----------- 基础设置 ----------- include /etc/nginx/mime.types; # 包含 mime 类型映射文件 default_type application/octet-stream; # 默认 mime 类型 # ----------- 日志格式 ----------- log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; # 访问日志路径和格式 # access_log off; # 关闭访问日志 (可节省 i/o) # ----------- 性能优化 ----------- sendfile on; # 启用高效文件传输 (零拷贝) tcp_nopush on; # 启用 tcp_nopush (与 sendfile 配合,减少网络小包) tcp_nodelay on; # 启用 tcp_nodelay (减少延迟,禁用 nagle 算法) keepalive_timeout 65; # 保持连接超时时间 (秒) # keepalive_requests 100; # 一个 keep-alive 连接上允许的最大请求数 # ----------- gzip 压缩 ----------- # gzip on; # 启用 gzip 压缩 # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml; # ----------- 安全相关 ----------- # server_tokens off; # 隐藏 nginx 版本号 (在 server 块中更常见) # client_max_body_size 10m; # 允许客户端请求的最大单文件字节数 # ----------- 包含其他配置文件 ----------- # 这是关键!它会加载 /etc/nginx/conf.d/ 目录下所有以 .conf 结尾的文件 include /etc/nginx/conf.d/*.conf; # include /etc/nginx/sites-enabled/*; # debian/ubuntu 风格 }
通常,我们将具体的 server
块配置放在 /etc/nginx/conf.d/
目录下,文件名如 default.conf
, myapp.conf
。
# conf.d/default.conf server { listen 80; # 监听端口 server_name localhost; # 服务器域名或 ip # ----------- 根目录与索引 ----------- location / { root /usr/share/nginx/html; # 网站根目录 index index.html index.htm; # 默认索引文件 } # ----------- 错误页面 ----------- error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # ----------- 访问日志 ----------- # 可以覆盖 http 块中的 access_log # access_log /var/log/nginx/myapp.access.log main; }
这是 nginx 最常见的用途之一,将请求转发给后端应用(如 node.js, python, java 应用)。
# conf.d/myapp.conf upstream backend_nodes { # 定义后端服务器组 (可以是容器名、ip 或服务发现地址) server app-server-1:3000; # 假设后端应用在名为 app-server-1 的容器的 3000 端口 server app-server-2:3000; # server 192.168.1.100:8080; # 可配置负载均衡策略 # least_conn; # 最少连接 # ip_hash; # 基于客户端 ip 的会话保持 # hash $request_uri consistent; # 一致性哈希 } server { listen 80; server_name myapp.example.com; location / { # 将请求代理到 upstream 定义的组 proxy_pass http://backend_nodes; # ----------- 代理关键头信息 ----------- # 传递原始客户端 ip proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-forwarded-proto $scheme; # ----------- 超时与缓冲 ----------- proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; # ----------- 重试 ----------- # proxy_next_upstream error timeout http_500 http_502 http_503 http_504; } # ----------- 静态资源缓存 ----------- location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { root /usr/share/nginx/html/static; expires 1y; # 缓存 1 年 add_header cache-control "public, immutable"; } }
# conf.d/ssl-site.conf server { listen 80; server_name secure.example.com; # http 重定向到 https return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; # 启用 http/2 server_name secure.example.com; # ----------- ssl 证书 ----------- # 在容器中,证书通常通过卷挂载或 secrets 提供 ssl_certificate /etc/nginx/ssl/secure.example.com.crt; ssl_certificate_key /etc/nginx/ssl/secure.example.com.key; # ssl_certificate /etc/letsencrypt/live/secure.example.com/fullchain.pem; # ssl_certificate_key /etc/letsencrypt/live/secure.example.com/privkey.pem; # ----------- ssl 安全设置 ----------- ssl_protocols tlsv1.2 tlsv1.3; # 禁用不安全的 sslv3, tlsv1, tlsv1.1 ssl_ciphers ecdhe-rsa-aes256-gcm-sha512:dhe-rsa-aes256-gcm-sha512:ecdhe-rsa-aes256-gcm-sha384:dhe-rsa-aes256-gcm-sha384; ssl_prefer_server_ciphers off; # 让客户端选择更强的密码 ssl_session_cache shared:ssl:10m; ssl_session_timeout 10m; ssl_session_tickets off; # ----------- hsts (http strict transport security) ----------- # 告诉浏览器未来一段时间内都使用 https add_header strict-transport-security "max-age=31536000; includesubdomains" always; # ----------- 其他安全头 ----------- add_header x-frame-options deny; add_header x-content-type-options nosniff; add_header x-xss-protection "1; mode=block"; # ----------- 代理或根目录 ----------- location / { proxy_pass http://backend_app; # ... 代理设置 ... } }
证书在容器中的管理:
-v /path/to/certs:/etc/nginx/ssl:ro
)。certbot
容器配合 nginx-proxy
或 traefik
等工具自动申请和更新 let’s encrypt 证书。修改配置后,无需重启容器,只需重载 nginx:
docker exec my-nginx nginx -s reload
这会平滑地应用新配置,不会中断现有连接。
在 dockerfile
或编排文件中定义健康检查:
healthcheck --interval=30s --timeout=3s --start-period=5s --retries=3 \ cmd curl -f http://localhost/ || exit 1
挂载日志目录: 将容器内的 /var/log/nginx/
挂载到宿主机,便于集中收集(如 elk, fluentd)。
-v ~/my-nginx/logs:/var/log/nginx
使用 docker 日志驱动: docker run --log-driver=json-file ...
或 --log-driver=syslog
。
将静态网站文件(html, css, js, 图片)放在 /usr/share/nginx/html
目录下。可以通过卷挂载或在 dockerfile
中 copy
进去。
虽然 nginx 本身不直接读取环境变量,但可以使用 envsubst
在容器启动时动态替换配置文件中的占位符。
准备模板文件 nginx.conf.template
:
server { listen ${nginx_port:-80}; server_name ${server_name:-localhost}; ... }
修改 dockerfile
或启动脚本:
from nginx:stable copy nginx.conf.template /etc/nginx/nginx.conf.template copy entrypoint.sh /docker-entrypoint.d/entrypoint.sh # entrypoint.sh 负责用 envsubst 替换模板
entrypoint.sh
示例:
#!/bin/bash set -e envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf exec "$@"
运行时传入变量:
docker run -e nginx_port=8080 -e server_name=myapp.com my-custom-nginx
如果需要在构建过程中编译某些资源(如压缩 js/css),可以使用多阶段构建:
# 第一阶段:构建前端应用 from node:16 as builder workdir /app copy package*.json ./ run npm ci copy . . run npm run build # 生成 dist 目录 # 第二阶段:构建 nginx 镜像 from nginx:stable copy --from=builder /app/dist /usr/share/nginx/html copy nginx.conf /etc/nginx/nginx.conf
查看容器日志: docker logs my-nginx
进入容器: docker exec -it my-nginx /bin/sh
检查配置语法: docker exec my-nginx nginx -t
查看进程: docker exec my-nginx ps aux
网络连通性: docker exec my-nginx curl -v http://backend-service
nginx 容器化是现代应用架构的基石。通过合理选择配置管理策略(开发用卷挂载,生产用自定义镜像),深入理解 nginx.conf
和 server
块的配置,并结合反向代理、https、负载均衡等高级功能,你可以构建出高效、安全、可扩展的 web 服务。
关键要点回顾:
dockerfile
打包配置。include
指令保持配置清晰。到此这篇关于从基础配置到生产实践深入解析nginx容器化部署的文章就介绍到这了,更多相关nginx容器部署内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论