Skip to content

自定义服务器部署

This content is not available in your language yet.

如果你拥有自己的 VPS 或云服务器,可以完全掌控博客的部署环境。本指南介绍使用 Nginx 和 Caddy 部署的方法。

最简单的方式是在服务器上设置一个定时任务从 Git 仓库拉取更新。

  1. 在服务器上克隆 Gridea Pro 推送的仓库:
Terminal window
cd /var/www
git clone https://github.com/username/blog.git gridea-blog
  1. 创建自动拉取脚本:
#!/bin/bash
cd /var/www/gridea-blog
git pull origin main
  1. 添加到 crontab(每 5 分钟检查一次):
Terminal window
*/5 * * * * /path/to/pull-blog.sh

如果不使用 Git,可以直接将输出文件上传到服务器:

Terminal window
# 通过 rsync 上传
rsync -avz --delete ~/Documents/Gridea/output/ user@server:/var/www/gridea-blog/
# 或通过 scp
scp -r ~/Documents/Gridea/output/* user@server:/var/www/gridea-blog/
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 自动处理 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
}
}
Terminal window
# 安装 Certbot
sudo apt install certbot python3-certbot-nginx
# 获取并配置证书
sudo certbot --nginx -d blog.example.com

Caddy 默认自动从 Let’s Encrypt 获取和续期 HTTPS 证书,无需额外配置。

  1. 启用 HTTP/2 — Nginx 在 HTTPS 配置中添加 http2
  2. 启用 Brotli 压缩 — 比 gzip 更高效
  3. 配置 CDN — 在服务器前面加一层 Cloudflare 等 CDN 加速全球访问
  4. 合理的缓存策略 — 静态资源长期缓存,HTML 文件短期缓存