2026/4/15 18:33:02
网站建设
项目流程
化妆品网站素材,龙华网站建设专业定制企业,搭建网站内链系统,推广网站优化怎么做我来为您介绍如何使用 Docker Compose 安装和配置 Nginx#xff0c;包括基础部署、常用配置和进阶场景。
一、基础配置
1. 最简单的 Nginx 部署
创建 docker-compose.yml#xff1a;
yaml version: 3.8
services: nginx: image: nginx:latest container_name:…我来为您介绍如何使用 Docker Compose 安装和配置 Nginx包括基础部署、常用配置和进阶场景。一、基础配置1. 最简单的 Nginx 部署创建 docker-compose.ymlyamlversion: 3.8services:nginx:image: nginx:latestcontainer_name: nginxports:- 80:80- 443:443volumes:- ./html:/usr/share/nginx/html- ./nginx.conf:/etc/nginx/nginx.conf:rorestart: unless-stopped目录结构project/├── docker-compose.yml├── nginx.conf└── html/└── index.html2. 启动命令bash# 启动docker-compose up -d# 查看状态docker-compose ps# 查看日志docker-compose logs -f nginx# 停止docker-compose down# 重启docker-compose restart nginx二、常用配置模板2.1 反向代理配置yamlversion: 3.8services:nginx:image: nginx:alpinecontainer_name: nginx-proxyports:- 80:80- 443:443volumes:- ./nginx.conf:/etc/nginx/nginx.conf:ro- ./conf.d:/etc/nginx/conf.d:ro- ./ssl:/etc/nginx/ssl:ro- ./logs:/var/log/nginxdepends_on:- app1- app2networks:- frontend- backendrestart: always# 后端应用示例app1:image: httpd:alpinecontainer_name: web1networks:- backendapp2:image: httpd:alpinecontainer_name: web2networks:- backendnetworks:frontend:driver: bridgebackend:driver: bridgenginx.conf 配置nginxuser 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/rssxml application/atomxml image/svgxml;# 包含其他配置include /etc/nginx/conf.d/*.conf;}conf.d/default.conf 反向代理配置nginxserver {listen 80;server_name localhost;# 静态文件location / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ 404;}# 反向代理到 app1location /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;}# 反向代理到 app2location /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;}}# 负载均衡 upstreamupstream backend_servers {server app1:80 weight5;server app2:80 weight5;keepalive 32;}三、HTTPS/SSL 配置3.1 使用自签名证书yamlversion: 3.8services:nginx:image: nginx:alpineports:- 80:80- 443:443volumes:- ./nginx.conf:/etc/nginx/nginx.conf:ro- ./ssl:/etc/nginx/ssl:ro- ./html:/usr/share/nginx/htmlrestart: always生成自签名证书bashmkdir sslopenssl req -x509 -nodes -days 365 -newkey rsa:2048 \-keyout ssl/nginx.key \-out ssl/nginx.crt \-subj /CCN/STState/LCity/OOrganization/CNlocalhostSSL 配置文件 conf.d/ssl.confnginxserver {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 重定向到 HTTPSserver {listen 80;server_name localhost;return 301 https://$server_name$request_uri;}3.2 使用 Lets Encrypt自动续期yamlversion: 3.8services:nginx:image: nginx:alpinecontainer_name: nginxports:- 80:80- 443:443volumes:- ./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/htmlrestart: alwayscertbot:image: certbot/certbotcontainer_name: certbotvolumes:- ./certbot/conf:/etc/letsencrypt- ./certbot/www:/var/www/certbotentrypoint: /bin/sh -c trap exit TERM; while :; do certbot renew; sleep 12h wait $${!}; done;初始化证书脚本 init-letsencrypt.shbash#!/bin/bashdomains(example.com www.example.com)rsa_key_size4096data_path./certbotemailyouremail.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四、多站点配置yamlversion: 3.8services:nginx:image: nginx:alpineports:- 80:80- 443:443volumes:- ./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/nginxrestart: alwaysnginx.confnginxuser 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.confnginxserver {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.confnginxserver {listen 80;server_name site2.com www.site2.com;root /var/www/site2;index index.html;# 启用 Gzipgzip on;gzip_types text/css application/javascript;# 缓存静态资源location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {expires 30d;add_header Cache-Control public, immutable;}}五、与 PHP 集成LNMPyamlversion: 3.8services:nginx:image: nginx:alpineports:- 80:80volumes:- ./nginx.conf:/etc/nginx/nginx.conf:ro- ./conf.d:/etc/nginx/conf.d:ro- ./www:/var/www/html:rodepends_on:- phpnetworks:- webphp:image: php:8.1-fpm-alpinevolumes:- ./www:/var/www/htmlnetworks:- webmysql:image: mysql:8.0environment:MYSQL_ROOT_PASSWORD: rootpassMYSQL_DATABASE: mydbMYSQL_USER: userMYSQL_PASSWORD: userpassvolumes:- mysql_data:/var/lib/mysqlnetworks:- webvolumes:mysql_data:networks:web:driver: bridgePHP-FPM 配置 conf.d/php.confnginxserver {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;}}六、生产环境优化配置yamlversion: 3.8services:nginx:image: nginx:alpinecontainer_name: nginxports:- 80:80- 443:443volumes:- ./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/nginxenvironment:- NGINX_HOSTexample.com- NGINX_PORT80restart: unless-stoppeddeploy:resources:limits:cpus: 1memory: 512Mreservations:cpus: 0.25memory: 128Mhealthcheck:test: [CMD, wget, --quiet, --tries1, --spider, http://localhost/health]interval: 30stimeout: 10sretries: 3start_period: 40svolumes:nginx_cache:生产级 nginx.confnginxuser 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 buffer32k flush5s;# 性能优化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/rssxml application/atomxml image/svgxml;# 限速和限流limit_req_zone $binary_remote_addr zoneone:10m rate10r/s;limit_conn_zone $binary_remote_addr zoneaddr:10m;# 缓存配置proxy_cache_path /var/cache/nginx levels1:2 keys_zoneSTATIC:10m inactive24h max_size1g;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 部署提供更详细的配置吗