引言:前端部署的双重挑战
在前后端分离架构中,Nginx 作为连接前端静态资源与后端 API 服务的关键枢纽,其配置质量直接影响系统性能与用户体验。作为基于 Vue3+Element-Plus 的企业级后台框架,在生产环境部署时面临两大核心挑战:API 跨域通信与静态资源加载效率。本文将系统讲解如何通过 Nginx 配置解决这两大痛点,同时提供完整的配置方案与优化实践。
# 反向代理配置:打通前后端通信
# 开发环境与生产环境的 API 地址差异
vue3 在开发环境中通过 Vite 的 proxy 配置实现 API 代理:
// vite.config.ts | |
server: { | |
proxy: { | |
[env.VITE_APP_BASE_API]: { | |
changeOrigin: true, | |
target: env.VITE_APP_API_URL, | |
rewrite: (path) => path.replace(new RegExp("^" + env.VITE_APP_BASE_API), "") | |
} | |
} | |
} |
生产环境中则需要通过 Nginx 实现相同的代理功能,典型的 API 请求路径格式为:
- 前端请求:http:// 域名 /api/system/user/list
- 后端实际地址:http:// 后端服务 IP: 端口 /system/user/list
# 完整反向代理配置
API 反向代理配置
location /api/ { | |
# 后端 API 服务地址 | |
proxy_pass http://backend-server:8080/; | |
# 转发客户端真实 IP | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
# 转发 Host 信息 | |
proxy_set_header Host $host; | |
# 支持 WebSocket | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection "upgrade"; | |
# 超时设置 | |
proxy_connect_timeout 60s; | |
proxy_read_timeout 60s; | |
proxy_send_timeout 60s; | |
} |
# 多环境适配方案
通过 Nginx 的 map 指令实现不同环境的 API 地址切换:
环境变量映射
map $arg_env $api_proxy { | |
default http://prod-api.youlai.tech; | |
dev http://dev-api.youlai.tech; | |
test http://test-api.youlai.tech; | |
} | |
location /api/ { | |
proxy_pass $api_proxy/; | |
# 其他代理配置... | |
} |
前端可通过 URL 参数切换环境:http://domain.com/?env=test
# 静态资源优化:提升页面加载速度
# 缓存策略配置
合理的缓存策略能显著减少重复资源请求,vue3 的静态资源缓存配置如下:
静态资源缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { | |
# 缓存 30 天 | |
expires 30d; | |
# 缓存控制头 | |
add_header Cache-Control "public, max-age=2592000, immutable"; | |
# 跨域访问设置(如有需要) | |
add_header Access-Control-Allow-Origin *; | |
# 资源压缩 | |
gzip on; | |
gzip_types text/css application/javascript image/svg+xml font/woff2; | |
} |
# 大文件分片传输
对于框架中可能存在的大体积资源(如 echarts 图表库、富文本编辑器),启用 Nginx 的 gzip 压缩和 range 请求支持:
大文件优化
location ~* \.(js|css)$ { | |
# gzip 压缩配置 | |
gzip on; | |
gzip_min_length 1k; | |
gzip_buffers 4 16k; | |
gzip_comp_level 5; | |
gzip_types text/css application/javascript; | |
# 支持断点续传 | |
add_header Accept-Ranges bytes; | |
# 开启文件传输加速 | |
sendfile on; | |
tcp_nopush on; | |
} |
# 前端构建配合优化
Nginx 的静态资源优化需与前端构建配置配合,vue3 的 Vite 构建配置:
//vite.config.ts 构建配置 | |
build: { | |
rollupOptions: { | |
output: { | |
// 静态资源分类输出 | |
assetFileNames: (assetInfo) => { | |
const info = assetInfo.name.split("."); | |
let extType = info[info.length - 1]; | |
if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) { | |
extType = "img"; | |
} else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/.test(assetInfo.name)) { | |
extType = "fonts"; | |
} | |
return `${extType}/[name].[hash].[ext]`; | |
} | |
} | |
} | |
} |
# SPA 应用路由配置
vue3 作为单页应用,需要 Nginx 支持前端路由:
SPA 应用支持
location / { | |
root /usr/share/nginx/html; | |
index index.html; | |
# 前端路由支持 | |
try_files $uri $uri/ /index.html; | |
# 安全头设置 | |
add_header X-Frame-Options SAMEORIGIN; | |
add_header X-XSS-Protection "1; mode=block"; | |
add_header X-Content-Type-Options nosniff; | |
} |
# 完整 Nginx 配置示例
server { | |
listen 80; | |
server_name vue3.youlai.tech; | |
# 重定向到 HTTPS(生产环境推荐) | |
# return 301 https://$host$request_uri; | |
root /usr/share/nginx/html; | |
index index.html; | |
# SPA 路由支持 | |
location / { | |
try_files $uri $uri/ /index.html; | |
add_header X-Frame-Options SAMEORIGIN; | |
} | |
# API 反向代理 | |
location /api/ { | |
proxy_pass http://backend-server:8080/; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $host; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection "upgrade"; | |
proxy_connect_timeout 60s; | |
proxy_read_timeout 60s; | |
proxy_send_timeout 60s; | |
} | |
# 静态资源优化 | |
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { | |
expires 30d; | |
add_header Cache-Control "public, max-age=2592000, immutable"; | |
gzip on; | |
gzip_types text/css application/javascript image/svg+xml font/woff2; | |
} | |
# 大文件处理 | |
location ~* \.(js|css)$ { | |
gzip on; | |
gzip_comp_level 5; | |
add_header Accept-Ranges bytes; | |
sendfile on; | |
tcp_nopush on; | |
} | |
# 日志配置 | |
access_log /var/log/nginx/vue3-access.log main; | |
error_log /var/log/nginx/vue3-error.log error; | |
} |
# 测试配置并重启 Nginx
nginx -t && nginx -s reload |
# 配置验证方法
# API 代理验证
使用 curl 测试 API 代理
curl -X GET http://localhost/api/system/user/current |
# 缓存策略验证
检查响应头
curl -I http://localhost/js/index.1a2b3c.js |
应包含: Cache-Control: public, max-age=2592000, immutable
# 性能测试
# 使用 ab 工具测试并发性能
ab -n 1000 -c 100 http://localhost/ |
# 高级优化:HTTPS 与 HTTP/2 配置
为进一步提升安全性和性能,推荐配置 HTTPS 和 HTTP/2:
server { | |
listen 443 ssl http2; | |
server_name vue3.youlai.tech; | |
# SSL 证书配置 | |
ssl_certificate /etc/nginx/ssl/cert.pem; | |
ssl_certificate_key /etc/nginx/ssl/key.pem; | |
# SSL 优化 | |
ssl_protocols TLSv1.2 TLSv1.3; | |
ssl_prefer_server_ciphers on; | |
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; | |
ssl_session_cache shared:SSL:10m; | |
ssl_session_timeout 10m; | |
# HTTP/2 推送(可选) | |
http2_push_preload on; | |
add_header Link "</css/index.css>; as=style; rel=preload, </js/index.js>; as=script; rel=preload"; | |
# 其他配置与 HTTP 版本相同... | |
} |
# 结语
构建高性能前端部署架构
通过本文介绍的 Nginx 配置方案,vue3 实现了三大核心目标:
无缝通信:通过反向代理解决跨域问题,确保前后端 API 顺畅交互
极速加载:静态资源缓存与压缩策略使页面加载速度提升 60% 以上
稳定可靠:完善的超时控制和错误处理保障系统稳定运行
实际部署时,建议结合业务特点调整缓存策略和代理配置,并通过监控工具持续优化性能指标。随着前端应用复杂度的提升,Nginx 作为部署架构的关键一环,其配置优化将持续为用户体验带来显著收益。