自定义服务器部署
如果你拥有自己的 VPS 或云服务器,可以完全掌控博客的部署环境。本指南介绍使用 Nginx 和 Caddy 部署的方法。
方式一:Git + 自动拉取
Section titled “方式一:Git + 自动拉取”最简单的方式是在服务器上设置一个定时任务从 Git 仓库拉取更新。
- 在服务器上克隆 Gridea Pro 推送的仓库:
cd /var/wwwgit clone https://github.com/username/blog.git gridea-blog- 创建自动拉取脚本:
#!/bin/bashcd /var/www/gridea-bloggit pull origin main- 添加到 crontab(每 5 分钟检查一次):
*/5 * * * * /path/to/pull-blog.sh方式二:手动上传
Section titled “方式二:手动上传”如果不使用 Git,可以直接将输出文件上传到服务器:
# 通过 rsync 上传rsync -avz --delete ~/Documents/Gridea/output/ user@server:/var/www/gridea-blog/
# 或通过 scpscp -r ~/Documents/Gridea/output/* user@server:/var/www/gridea-blog/Nginx 配置
Section titled “Nginx 配置”server { listen 80; server_name blog.example.com; root /var/www/gridea-blog; index index.html;
# 处理 clean URL location / { try_files $uri $uri/ $uri/index.html =404; }
# 自定义 404 页面 error_page 404 /404.html;
# 静态资源缓存 location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff2?)$ { expires 1y; add_header Cache-Control "public, immutable"; }
# 启用 gzip 压缩 gzip on; gzip_types text/html text/css application/javascript;}Caddy 配置
Section titled “Caddy 配置”Caddy 自动处理 HTTPS 证书,配置更加简洁:
blog.example.com { root * /var/www/gridea-blog file_server try_files {path} {path}/ {path}/index.html handle_errors { rewrite * /404.html file_server }}HTTPS 配置
Section titled “HTTPS 配置”使用 Let’s Encrypt(Nginx)
Section titled “使用 Let’s Encrypt(Nginx)”# 安装 Certbotsudo apt install certbot python3-certbot-nginx
# 获取并配置证书sudo certbot --nginx -d blog.example.com使用 Caddy
Section titled “使用 Caddy”Caddy 默认自动从 Let’s Encrypt 获取和续期 HTTPS 证书,无需额外配置。
性能优化建议
Section titled “性能优化建议”- 启用 HTTP/2 — Nginx 在 HTTPS 配置中添加
http2 - 启用 Brotli 压缩 — 比 gzip 更高效
- 配置 CDN — 在服务器前面加一层 Cloudflare 等 CDN 加速全球访问
- 合理的缓存策略 — 静态资源长期缓存,HTML 文件短期缓存