news 2026/4/15 16:02:46

docker-compose安装nginx

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
docker-compose安装nginx

我来为您介绍如何使用 Docker Compose 安装和配置 Nginx,包括基础部署、常用配置和进阶场景。

一、基础配置

1. 最简单的 Nginx 部署

创建 `docker-compose.yml`:

```yaml
version: '3.8'

services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./html:/usr/share/nginx/html
- ./nginx.conf:/etc/nginx/nginx.conf:ro
restart: unless-stopped
```

目录结构:

```
project/
├── docker-compose.yml
├── nginx.conf
└── html/
└── index.html
```

2. 启动命令

```bash
# 启动
docker-compose up -d

# 查看状态
docker-compose ps

# 查看日志
docker-compose logs -f nginx

# 停止
docker-compose down

# 重启
docker-compose restart nginx
```

二、常用配置模板

2.1 反向代理配置

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d:/etc/nginx/conf.d:ro
- ./ssl:/etc/nginx/ssl:ro
- ./logs:/var/log/nginx
depends_on:
- app1
- app2
networks:
- frontend
- backend
restart: always

# 后端应用示例
app1:
image: httpd:alpine
container_name: web1
networks:
- backend

app2:
image: httpd:alpine
container_name: web2
networks:
- backend

networks:
frontend:
driver: bridge
backend:
driver: bridge
```

`nginx.conf` 配置:

```nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

# 日志格式
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;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 100M;

# Gzip 压缩
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

# 包含其他配置
include /etc/nginx/conf.d/*.conf;
}
```

`conf.d/default.conf` 反向代理配置:

```nginx
server {
listen 80;
server_name localhost;

# 静态文件
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ =404;
}

# 反向代理到 app1
location /api1/ {
proxy_pass http://app1:80/;
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;
}

# 反向代理到 app2
location /api2/ {
proxy_pass http://app2:80/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# 负载均衡示例
location /load-balance/ {
proxy_pass http://backend_servers;
}
}

# 负载均衡 upstream
upstream backend_servers {
server app1:80 weight=5;
server app2:80 weight=5;
keepalive 32;
}
```

三、HTTPS/SSL 配置

3.1 使用自签名证书

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
- ./html:/usr/share/nginx/html
restart: always
```

生成自签名证书:

```bash
mkdir ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl/nginx.key \
-out ssl/nginx.crt \
-subj "/C=CN/ST=State/L=City/O=Organization/CN=localhost"
```

SSL 配置文件 `conf.d/ssl.conf`:

```nginx
server {
listen 443 ssl http2;
server_name localhost;

ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;

# SSL 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

location / {
root /usr/share/nginx/html;
index index.html;
}
}

# HTTP 重定向到 HTTPS
server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}
```

3.2 使用 Let's Encrypt(自动续期)

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d:/etc/nginx/conf.d:ro
- ./certbot/conf:/etc/letsencrypt:ro
- ./certbot/www:/var/www/certbot
- ./html:/usr/share/nginx/html
restart: always

certbot:
image: certbot/certbot
container_name: certbot
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
```

初始化证书脚本 `init-letsencrypt.sh`:

```bash
#!/bin/bash

domains=(example.com www.example.com)
rsa_key_size=4096
data_path="./certbot"
email="your@email.com"

# 创建目录
mkdir -p $data_path/conf $data_path/www

# 获取证书
docker-compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
--register-unsafely-without-email \
--agree-tos \
--force-renewal \
-d ${domains[0]} -d ${domains[1]}" certbot
```

四、多站点配置

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./sites:/etc/nginx/sites:ro
- ./html/site1:/var/www/site1
- ./html/site2:/var/www/site2
- ./logs:/var/log/nginx
restart: always
```

`nginx.conf`:

```nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

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;

sendfile on;
tcp_nopush on;
keepalive_timeout 65;

# 包含所有站点配置
include /etc/nginx/sites/*.conf;
}
```

`sites/site1.conf`:

```nginx
server {
listen 80;
server_name site1.com www.site1.com;
root /var/www/site1;
index index.html;

location / {
try_files $uri $uri/ =404;
}

location /api {
proxy_pass http://backend:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
```

`sites/site2.conf`:

```nginx
server {
listen 80;
server_name site2.com www.site2.com;
root /var/www/site2;
index index.html;

# 启用 Gzip
gzip on;
gzip_types text/css application/javascript;

# 缓存静态资源
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
```

五、与 PHP 集成(LNMP)

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d:/etc/nginx/conf.d:ro
- ./www:/var/www/html:ro
depends_on:
- php
networks:
- web

php:
image: php:8.1-fpm-alpine
volumes:
- ./www:/var/www/html
networks:
- web

mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: userpass
volumes:
- mysql_data:/var/lib/mysql
networks:
- web

volumes:
mysql_data:

networks:
web:
driver: bridge
```

PHP-FPM 配置 `conf.d/php.conf`:

```nginx
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```

六、生产环境优化配置

```yaml
version: '3.8'

services:
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./conf.d:/etc/nginx/conf.d:ro
- ./ssl:/etc/nginx/ssl:ro
- ./html:/usr/share/nginx/html:ro
- nginx_cache:/var/cache/nginx
- ./logs:/var/log/nginx
environment:
- NGINX_HOST=example.com
- NGINX_PORT=80
restart: unless-stopped
deploy:
resources:
limits:
cpus: '1'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s

volumes:
nginx_cache:
```

生产级 `nginx.conf`:

```nginx
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 4096;
use epoll;
multi_accept on;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

# 日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';

access_log /var/log/nginx/access.log main buffer=32k flush=5s;

# 性能优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 1000;
reset_timedout_connection on;
client_body_timeout 10;
send_timeout 2;

# 压缩
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

# 限速和限流
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;

# 缓存配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

include /etc/nginx/conf.d/*.conf;
}
```

七、常用命令速查

```bash
# 启动
docker-compose up -d

# 查看日志
docker-compose logs -f nginx

# 进入容器
docker-compose exec nginx sh

# 测试配置
docker-compose exec nginx nginx -t

# 重载配置(不重启)
docker-compose exec nginx nginx -s reload

# 查看状态
docker-compose ps

# 停止并删除
docker-compose down -v

# 更新镜像
docker-compose pull && docker-compose up -d
```

需要我针对特定场景(如负载均衡、WebSocket 代理、Kubernetes 部署)提供更详细的配置吗?

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/13 18:36:06

双卡4090D部署GPT-OSS-20B,开箱即用的网页推理体验

双卡4090D部署GPT-OSS-20B,开箱即用的网页推理体验 你有没有试过——不用写一行代码、不配环境、不调参数,点开浏览器就能和一个20B级大模型对话?不是API调用,不是远程服务,而是真正在你手边的显卡上跑起来&#xff0…

作者头像 李华
网站建设 2026/4/14 8:07:03

Z-Image-Turbo集成ControlNet全流程详解

Z-Image-Turbo集成ControlNet全流程详解 在AI图像生成领域,“快”与“准”长期难以兼得:传统扩散模型追求质量往往牺牲速度,而轻量模型又常在结构控制、细节还原上力不从心。Z-Image-Turbo的出现打破了这一惯性——它用8步推理实现10241024高…

作者头像 李华
网站建设 2026/4/11 21:56:10

IQuest-Coder-V1-40B-Instruct部署手册:多GPU并行配置

IQuest-Coder-V1-40B-Instruct部署手册:多GPU并行配置 1. 为什么需要关注这个模型 你可能已经用过不少代码大模型,但IQuest-Coder-V1-40B-Instruct有点不一样——它不是为“写点小脚本”设计的,而是冲着真实软件工程场景去的。比如&#xf…

作者头像 李华
网站建设 2026/4/8 9:58:47

革新AI视频创作:ComfyUI-LTXVideo实战技术指南

革新AI视频创作:ComfyUI-LTXVideo实战技术指南 【免费下载链接】ComfyUI-LTXVideo LTX-Video Support for ComfyUI 项目地址: https://gitcode.com/GitHub_Trending/co/ComfyUI-LTXVideo 在数字内容创作的浪潮中,AI视频生成技术正经历着前所未有的…

作者头像 李华
网站建设 2026/4/13 8:56:21

如何用N_m3u8DL-RE实现高质量视频下载?2024最新全场景指南

如何用N_m3u8DL-RE实现高质量视频下载?2024最新全场景指南 【免费下载链接】N_m3u8DL-RE 跨平台、现代且功能强大的流媒体下载器,支持MPD/M3U8/ISM格式。支持英语、简体中文和繁体中文。 项目地址: https://gitcode.com/GitHub_Trending/nm3/N_m3u8DL-…

作者头像 李华
网站建设 2026/4/5 7:46:24

springboot街道摊贩管理系统设计开发实现

街道摊贩管理系统设计开发的背景与意义 背景 城市化进程加快导致流动摊贩数量激增,传统人工管理方式效率低下,易引发占道经营、卫生安全等问题。政府需数字化手段规范管理,平衡市容秩序与民生需求。 意义 规范管理:通过信息化…

作者头像 李华