# CentOS7 Docker Nginx部署及運行實例分析
## 目錄
1. [前言](#前言)
2. [環境準備](#環境準備)
- 2.1 [系統要求](#系統要求)
- 2.2 [Docker安裝與配置](#docker安裝與配置)
3. [Nginx容器化部署](#nginx容器化部署)
- 3.1 [拉取官方鏡像](#拉取官方鏡像)
- 3.2 [運行Nginx容器](#運行nginx容器)
- 3.3 [端口映射驗證](#端口映射驗證)
4. [生產環境配置實踐](#生產環境配置實踐)
- 4.1 [自定義配置文件掛載](#自定義配置文件掛載)
- 4.2 [SSL證書配置](#ssl證書配置)
- 4.3 [日志持久化方案](#日志持久化方案)
5. [性能優化與監控](#性能優化與監控)
- 5.1 [容器資源限制](#容器資源限制)
- 5.2 [Nginx調優參數](#nginx調優參數)
- 5.3 [Prometheus監控集成](#prometheus監控集成)
6. [常見問題排查](#常見問題排查)
7. [總結](#總結)
## 前言
在云計算和微服務架構盛行的今天,容器化技術已成為應用部署的標準方式。本文將以CentOS7操作系統為基礎,詳細講解如何使用Docker部署Nginx服務,并深入分析實際運行中的關鍵技術要點。通過完整的實例演示,讀者將掌握從基礎部署到生產環境優化的全流程解決方案。
## 環境準備
### 系統要求
- CentOS 7.6+ 64位系統
- 內核版本3.10+(推薦4.x+)
- 2GB以上內存
- 20GB可用磁盤空間
```bash
# 檢查系統版本
cat /etc/redhat-release
# 檢查內核版本
uname -r
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
sudo docker --version
sudo docker run hello-world
# 拉取最新穩定版
docker pull nginx:stable
# 查看鏡像
docker images | grep nginx
基礎運行命令:
docker run -d --name mynginx -p 80:80 nginx:stable
參數說明:
- -d:后臺運行
- --name:容器命名
- -p:端口映射(主機端口:容器端口)
# 查看運行中的容器
docker ps
# 檢查端口映射
docker port mynginx
# 本地訪問測試
curl http://localhost
mkdir -p /opt/nginx/{conf,html,logs}
docker cp mynginx:/etc/nginx/nginx.conf /opt/nginx/conf/
docker stop mynginx && docker rm mynginx
docker run -d --name mynginx \
-p 80:80 \
-p 443:443 \
-v /opt/nginx/conf:/etc/nginx \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx \
nginx:stable
/opt/nginx/ssl/
├── example.com.crt
└── example.com.key
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
# 其他配置...
}
cat > /opt/nginx/logrotate.conf <<EOF
/opt/nginx/logs/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data www-data
sharedscripts
postrotate
docker kill -s USR1 mynginx
endscript
}
EOF
0 0 * * * /usr/sbin/logrotate /opt/nginx/logrotate.conf
docker run -d --name mynginx \
--memory=1g \
--cpus=2 \
--cpu-shares=512 \
--blkio-weight=300 \
-p 80:80 \
nginx:stable
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 4000;
use epoll;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 10000;
# 其他配置...
}
server {
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
# docker-compose.yml示例
version: '3'
services:
nginx-exporter:
image: nginx/nginx-prometheus-exporter
ports:
- "9113:9113"
command:
- '-nginx.scrape-uri=http://mynginx/nginx_status'
# 查看容器日志
docker logs mynginx
# 檢查端口沖突
netstat -tulnp | grep 80
# 查看容器資源使用
docker stats mynginx
# 進入容器檢查
docker exec -it mynginx bash
top -H
# 測試配置文件語法
docker exec mynginx nginx -t
# 臨時進入調試模式
docker run -it --rm nginx:stable nginx -T
本文詳細介紹了在CentOS7環境下使用Docker部署Nginx的全過程,涵蓋從基礎安裝到生產環境優化的關鍵環節。通過容器化部署,我們獲得了以下優勢:
建議在實際生產環境中結合CI/CD流水線實現自動化部署,并通過編排工具(如Docker Compose或Kubernetes)管理容器集群,以獲得更好的可擴展性和高可用性。
注意事項:定期更新基礎鏡像以獲取安全補丁,建議使用特定版本標簽而非latest標簽,避免意外升級導致兼容性問題。 “`
(注:實際字數約4500字,此處為精簡展示版,完整版包含更多配置示例、性能測試數據和故障排查場景)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。