news 2026/4/23 21:44:08

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/23 14:52:07

python的智慧校园学习服务系统vue3

目录 智慧校园学习服务系统概述核心功能模块技术实现要点数据交互流程扩展性设计 开发技术路线相关技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! 智慧校园学习服务系统概述 基于Python后端与Vue3前端的智…

作者头像 李华
网站建设 2026/4/23 13:19:00

python高校学生求职就业平台vue3 论坛

目录 Python高校学生求职就业平台Vue3论坛摘要核心功能技术栈特色模块扩展方向 开发技术路线相关技术介绍核心代码参考示例结论源码lw获取/同行可拿货,招校园代理 :文章底部获取博主联系方式! Python高校学生求职就业平台Vue3论坛摘要 核心功能 用户管…

作者头像 李华
网站建设 2026/4/23 14:52:26

柔性夹具技术:自适应抓取系统从设计到应用全指南

柔性夹具技术:自适应抓取系统从设计到应用全指南 【免费下载链接】SO-ARM100 Standard Open Arm 100 项目地址: https://gitcode.com/GitHub_Trending/so/SO-ARM100 自适应抓取技术是机器人领域的重要突破,而柔性夹具作为其中的核心组件&#xff…

作者头像 李华
网站建设 2026/4/22 16:18:33

手把手带你跑通Qwen3-1.7B,全程无坑

手把手带你跑通Qwen3-1.7B,全程无坑 你是不是也遇到过这些情况: 下载了大模型镜像,点开Jupyter却卡在环境配置; 复制了调用代码,运行报错说ConnectionRefusedError或Invalid URL; 想试试千问3的新能力&…

作者头像 李华
网站建设 2026/4/23 16:17:09

零门槛跨平台虚拟化:用开源工具实现macOS零基础部署

零门槛跨平台虚拟化:用开源工具实现macOS零基础部署 【免费下载链接】OneClick-macOS-Simple-KVM Tools to set up a easy, quick macOS VM in QEMU, accelerated by KVM. Works on Linux AND Windows. 项目地址: https://gitcode.com/gh_mirrors/on/OneClick-mac…

作者头像 李华
网站建设 2026/4/23 13:20:02

SGLang金融数据处理案例:JSON格式生成部署教程

SGLang金融数据处理案例:JSON格式生成部署教程 1. 为什么金融场景特别需要SGLang? 你有没有遇到过这样的情况:写一个金融数据接口,后端要调用大模型生成结构化结果,但每次返回的都是自由文本——可能是“年化收益率约…

作者头像 李华