docker部署nginx脚本
·
文章目录
一、建目录结构脚本 (setup_directories.sh)
#!/bin/bash
# 创建必要的目录
echo "创建nginx目录结构..."
sudo mkdir -p /software/nginx/{data,logs,config,html,ssl}
# 设置目录权限
echo "设置目录权限..."
sudo chmod -R 755 /software/nginx
sudo chown -R $USER:$USER /software/nginx
echo "目录创建完成!"

二、脚本
1.nginx.conf配置文件
代码如下(示例):
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
keepalive_timeout 65;
# 隐藏Nginx版本号,增强安全性
server_tokens off;
# 开启Gzip压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss;
gzip_vary on;
# 客户端最大上传文件大小
client_max_body_size 1000m;
# HTTP 服务器块 - 强制跳转到HTTPS
server {
listen 80;
server_name yshdhat.com www.yshdhat.com 127.0.0.1;
# 所有HTTP请求重定向到HTTPS
return 301 https://$host$request_uri;
}
# HTTPS 服务器块
server {
listen 443 ssl;
server_name yshdhat.com www.yshdhat.com 127.0.0.1;
# SSL证书配置 使用容器内路径
ssl_certificate /etc/nginx/ssl/yshdhat.com.crt;
ssl_certificate_key /etc/nginx/ssl/yshdhat.com.key;
# SSL安全配置
ssl_protocols TLSv1.2 TLSv1.3;
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;
# 根目录配置
root /usr/share/nginx/html/factory-task-prod;
index index.html index.htm;
# 前端静态文件服务
location / {
try_files $uri $uri/ /index.html;
# 静态资源缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
# 后端项目 - 管理后台API
location /admin-api/ {
proxy_pass http://127.0.0.1:8080/admin-api/;
proxy_set_header Host $http_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_read_timeout 30s;
proxy_send_timeout 30s;
}
# 后端项目 - 用户App API
location /app-api/ {
proxy_pass http://127.0.0.1:8080/app-api/;
proxy_set_header Host $http_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_read_timeout 30s;
proxy_send_timeout 30s;
}
# MinIO Web界面
location /minio-web/ {
rewrite ^/minio-web/(.*) /$1 break;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 300;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
chunked_transfer_encoding off;
proxy_pass http://127.0.0.1:9090/;
sub_filter '<base href="/"/>' '<base href="/minio-web/"/>';
sub_filter_once off;
}
# MinIO API
location /minio-api/ {
proxy_pass http://127.0.0.1:9000/;
proxy_http_version 1.1;
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_buffering off;
tcp_nodelay on;
}
# 健康检查端点
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# 错误页面配置
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
internal;
}
}
}
2.启动(start_nginx.sh)
提示:创建my-network,否则删除 --network my-network \
代码如下(示例):
#!/bin/bash
echo "正在启动Nginx容器..."
# 检查Docker是否运行
if ! systemctl is-active --quiet docker; then
echo "启动Docker服务..."
systemctl start docker
sleep 3
fi
# 检查nginx镜像是否存在,不存在才拉取
echo "检查Nginx镜像..."
if ! docker images | grep -q "nginx.*latest"; then
echo "Nginx镜像不存在,拉取最新版本..."
docker pull nginx:latest
else
echo "✅ Nginx镜像已存在,跳过拉取"
fi
# 停止并删除已存在的nginx容器
echo "清理现有容器..."
docker stop nginx 2>/dev/null
docker rm nginx 2>/dev/null
# 启动Nginx容器
echo "启动新的Nginx容器..."
docker run -d \
--name nginx \
--restart always \
--network my-network \
-p 80:80 \
-p 443:443 \
-v /software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /software/nginx/ssl:/etc/nginx/ssl:ro \
-v /software/nginx/logs:/var/log/nginx \
-v /software/nginx/html:/usr/share/nginx/html:ro \
-v /software/nginx/data:/var/nginx/data \
nginx:latest
# 检查容器是否启动成功
if [ $? -eq 0 ]; then
echo "✅ Nginx容器启动成功!"
else
echo "❌ Nginx容器启动失败!"
exit 1
fi
sleep 2
docker ps -f name=nginx
3.重启(restart_nginx.sh)
代码如下(示例):
#!/bin/bash
echo "重启Nginx容器..."
# 检查容器是否存在
if docker ps -a | grep -q nginx; then
echo "停止并删除现有容器..."
docker stop nginx
docker rm nginx
else
echo "没有找到现有的nginx容器"
fi
# 直接启动容器(使用已有镜像)
echo "启动Nginx容器..."
docker run -d \
--name nginx \
--restart always \
--network my-network \ #网络
-p 80:80 \
-p 443:443 \
-v /software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /software/nginx/ssl:/etc/nginx/ssl:ro \
-v /software/nginx/logs:/var/log/nginx \
-v /software/nginx/html:/usr/share/nginx/html:ro \
-v /software/nginx/data:/var/nginx/data \
nginx:latest
if [ $? -eq 0 ]; then
echo "✅ Nginx容器重启成功!"
docker ps -f name=nginx
else
echo "❌ Nginx容器重启失败!"
fi
4.执行
代码如下(示例):
1 给所有脚本添加执行权限
chmod +x setup_directories.sh start_nginx.sh restart_nginx.sh
2 创建目录结构
./setup_directories.sh
3 编写 nginx.conf
4 启动
./start_nginx.sh
更多推荐

所有评论(0)