全栈部署实战指南:Nginx反向代理+CDN加速+进程管理全流程

一、环境准备与工具链选型

1.1 服务器基础环境

生产环境建议选择主流云服务商的Linux服务器(推荐CentOS 8或Ubuntu 22.04 LTS),需满足以下核心组件:

  • Web服务器:Nginx 1.20+(支持HTTP/2和动态模块加载)
  • 后端运行时
    • Node.js 16+(配合PM2进程管理)
    • OpenJDK 11+(针对Java Spring Boot应用)
  • 构建工具链:Git 2.30+、Maven/Gradle(Java项目)、npm/yarn(前端项目)

1.2 辅助工具配置

  1. # 基础开发环境安装(Ubuntu示例)
  2. sudo apt update && sudo apt install -y \
  3. nginx \
  4. nodejs npm \
  5. openjdk-11-jdk \
  6. git curl wget
  7. # Node.js版本管理(推荐使用nvm)
  8. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
  9. nvm install 18
  10. nvm use 18

1.3 CDN与安全防护

注册主流CDN服务提供商账号(建议选择支持全站加速和WAF防护的服务),重点配置:

  • DNS解析:将域名A记录指向服务器IP
  • CNAME配置:启用CDN加速域名
  • 安全策略:开启CC防护、IP黑名单、SQL注入检测

二、前端部署自动化方案

2.1 构建优化策略

以Vue项目为例,推荐配置:

  1. // vue.config.js 示例
  2. module.exports = {
  3. publicPath: process.env.NODE_ENV === 'production' ? '/dist/' : '/',
  4. productionSourceMap: false,
  5. configureWebpack: {
  6. optimization: {
  7. splitChunks: {
  8. chunks: 'all',
  9. cacheGroups: {
  10. vendor: {
  11. test: /[\\/]node_modules[\\/]/,
  12. name: 'vendors',
  13. chunks: 'all'
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }

2.2 自动化部署流程

  1. # 1. 本地构建
  2. npm run build
  3. # 2. 同步到服务器(使用rsync替代scp实现增量同步)
  4. rsync -avz --delete dist/ user@server:/data/www/html/
  5. # 3. 部署后验证
  6. curl -I http://your-domain.com/manifest.json

2.3 Nginx静态资源配置

  1. server {
  2. listen 80;
  3. server_name frontend.your-domain.com;
  4. root /data/www/html;
  5. index index.html;
  6. # 前端路由支持
  7. location / {
  8. try_files $uri $uri/ /index.html;
  9. }
  10. # 静态资源缓存策略
  11. location ~* \.(js|css|png|jpg|jpeg|svg|ico|webp)$ {
  12. expires 1y;
  13. add_header Cache-Control "public, no-transform";
  14. access_log off;
  15. }
  16. # 禁止访问敏感文件
  17. location ~ /(\.git|\.env|README|LICENSE) {
  18. deny all;
  19. return 404;
  20. }
  21. }

三、后端服务守护方案

3.1 Node.js进程管理

  1. # 安装PM2
  2. npm install -g pm2
  3. # 启动应用(推荐使用生态系统文件)
  4. pm2 ecosystem
  5. # 生成的ecosystem.config.js示例
  6. module.exports = {
  7. apps: [{
  8. name: 'api-service',
  9. script: './dist/main.js',
  10. instances: 'max',
  11. exec_mode: 'cluster',
  12. env: {
  13. NODE_ENV: 'production',
  14. PORT: 3001
  15. },
  16. log_file: '/var/log/api-service.log',
  17. error_file: '/var/log/api-service-err.log'
  18. }]
  19. }
  20. # 启动服务
  21. pm2 start ecosystem.config.js
  22. pm2 save
  23. pm2 startup # 设置开机自启

3.2 Java服务管理方案

  1. # 使用systemd管理Spring Boot应用
  2. sudo vim /etc/systemd/system/springboot.service
  3. # 文件内容示例
  4. [Unit]
  5. Description=Spring Boot Application
  6. After=syslog.target network.target
  7. [Service]
  8. User=appuser
  9. WorkingDirectory=/data/apps/springboot
  10. ExecStart=/usr/bin/java -jar /data/apps/springboot/app.jar
  11. SuccessExitStatus=143
  12. Restart=always
  13. RestartSec=10
  14. [Install]
  15. WantedBy=multi-user.target
  16. # 启用服务
  17. sudo systemctl daemon-reload
  18. sudo systemctl enable springboot
  19. sudo systemctl start springboot

3.3 服务监控与告警

推荐集成以下监控方案:

  • 基础监控:Node Exporter + Prometheus + Grafana
  • 日志管理:ELK Stack或Loki+Grafana
  • 告警系统:Alertmanager配置邮件/Webhook通知

四、Nginx高级配置实践

4.1 反向代理核心配置

  1. upstream node_backend {
  2. server 127.0.0.1:3001;
  3. keepalive 32;
  4. }
  5. upstream java_backend {
  6. server 127.0.0.1:8080;
  7. keepalive 32;
  8. }
  9. server {
  10. listen 443 ssl http2;
  11. server_name api.your-domain.com;
  12. ssl_certificate /path/to/fullchain.pem;
  13. ssl_certificate_key /path/to/privkey.pem;
  14. # API代理配置
  15. location /api/v1/ {
  16. proxy_pass http://node_backend/api/v1/;
  17. proxy_set_header Host $host;
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. # 超时设置
  21. proxy_connect_timeout 60s;
  22. proxy_read_timeout 300s;
  23. proxy_send_timeout 300s;
  24. # 缓冲区设置
  25. proxy_buffer_size 4k;
  26. proxy_buffers 8 16k;
  27. proxy_busy_buffers_size 32k;
  28. }
  29. # WebSocket支持
  30. location /ws/ {
  31. proxy_pass http://node_backend;
  32. proxy_http_version 1.1;
  33. proxy_set_header Upgrade $http_upgrade;
  34. proxy_set_header Connection "upgrade";
  35. }
  36. }

4.2 性能优化配置

  1. # 全局优化
  2. worker_processes auto;
  3. worker_rlimit_nofile 65535;
  4. events {
  5. worker_connections 4096;
  6. multi_accept on;
  7. }
  8. # HTTP优化
  9. http {
  10. # 连接优化
  11. keepalive_timeout 75s;
  12. keepalive_requests 1000;
  13. sendfile on;
  14. tcp_nopush on;
  15. tcp_nodelay on;
  16. # Gzip压缩
  17. gzip on;
  18. gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  19. gzip_min_length 1k;
  20. gzip_comp_level 6;
  21. gzip_vary on;
  22. # 缓冲区配置
  23. client_body_buffer_size 128k;
  24. client_header_buffer_size 16k;
  25. client_max_body_size 8m;
  26. large_client_header_buffers 4 32k;
  27. }

五、CDN集成与安全加固

5.1 CDN配置最佳实践

  1. 缓存策略

    • 静态资源:缓存时间设置为1年(通过Cache-Control头控制)
    • HTML文件:缓存时间设置为0秒(必须回源验证)
    • API响应:根据业务需求设置(通常不缓存或短时间缓存)
  2. 回源配置

    • 协议跟随:保持HTTP/HTTPS一致性
    • 回源HOST:设置为实际后端服务域名
    • 301/302跟随:根据安全需求开启

5.2 安全防护方案

  1. # 安全相关配置
  2. server {
  3. # 防止点击劫持
  4. add_header X-Frame-Options "SAMEORIGIN";
  5. # XSS防护
  6. add_header X-XSS-Protection "1; mode=block";
  7. # 内容类型嗅探防护
  8. add_header X-Content-Type-Options "nosniff";
  9. # CSP策略(根据实际需求调整)
  10. add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://trusted.cdn.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self';";
  11. # 限制请求方法
  12. if ($request_method !~ ^(GET|HEAD|POST|OPTIONS)$ ) {
  13. return 405;
  14. }
  15. # 防止文件遍历
  16. location ~ /\. {
  17. deny all;
  18. return 403;
  19. }
  20. }

六、部署自动化与CI/CD集成

6.1 GitLab CI/CD示例

  1. # .gitlab-ci.yml 示例
  2. stages:
  3. - build
  4. - deploy
  5. build_frontend:
  6. stage: build
  7. image: node:18
  8. script:
  9. - cd frontend
  10. - npm install
  11. - npm run build
  12. artifacts:
  13. paths:
  14. - frontend/dist/
  15. deploy_production:
  16. stage: deploy
  17. image: alpine:latest
  18. before_script:
  19. - apk add --no-cache openssh-client rsync
  20. - mkdir -p ~/.ssh
  21. - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
  22. - chmod 600 ~/.ssh/id_rsa
  23. - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
  24. script:
  25. - rsync -avz --delete frontend/dist/ user@$SERVER_IP:/data/www/html/
  26. - ssh user@$SERVER_IP "pm2 restart ecosystem.config.js --env production"
  27. only:
  28. - main

6.2 部署验证清单

  1. 基础验证

    • 访问首页是否正常加载
    • 检查控制台无404错误
    • 验证所有API接口连通性
  2. 性能验证

    • 使用Lighthouse进行性能评分
    • 检查CDN缓存命中率
    • 监控服务器资源使用情况
  3. 安全验证

    • 扫描常见漏洞(如SQL注入、XSS)
    • 检查安全头配置
    • 验证HTTPS证书有效性

本方案通过系统化的技术栈整合,实现了从代码提交到生产部署的全流程自动化。通过合理的架构设计和配置优化,可支撑中小型项目百万级日活量的访问需求。实际部署时建议先在测试环境验证所有配置,再逐步推广到生产环境。