部署
clone
git clone https://github.com/anuraghazra/github-readme-stats.git cd github-readme-stats
|
新建 .env 文件
放在项目根目录,与 docker-compose.yml 同级:
# 必填:GitHub PAT(只需要 public_repo 权限即可) PAT_1=ghp_xxxxxxxxxxxxxxxxxxxxx
# 服务监听端口 PORT=9000
# 缓存时间,减少 API 调用(秒) CACHE_SECONDS=1800
# 单位时间内允许的请求数(速率限制) MAX_REQUESTS=300
|
获取 GitHub Personal Access Token(PAT)的步骤如下:
- 登录 GitHub,右上角头像 → Settings。
- 左侧菜单拉到最下方 Developer settings → Personal access tokens → 选择 Tokens (classic)
- 点击 Generate new token
- 填好备注(Token name)及有效期(Expiration)
- 勾选需要的权限:如果只给 github-readme-stats 用来读取公开仓库信息,勾选 public_repo 即可;若需访问私仓,再额外勾选整个 repo。
新建 docker-compose.yml 文件
services: grs: build: context: . container_name: github-readme-stats env_file: - .env ports: - "9000:9000" restart: unless-stopped
|
修改 package.json 文件
把 "devDependencies" 中的 "express": "^5.1.0", 移动到 "dependencies" 中去
新建 Dockerfile 文件
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production ENV PORT=9000
COPY package*.json ./ RUN npm ci --omit=dev --ignore-scripts
COPY . .
EXPOSE 9000 CMD ["node", "./express.js"]
|
最后
docker compose up -d --build
|
反向代理
创建 /etc/nginx/sites-available/api.diraw.top.conf:
server { listen 80; server_name api.diraw.top;
location / { return 404 "Not Found"; }
# 匹配 /github/stats 路径的请求 location /github/stats {
#if ($arg_username != "diraw") {return 403 "Forbidden: You can email dirawtop@gmail.com with github username for access.";}
proxy_pass http://127.0.0.1:9000/api;
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_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s;
add_header X-Frame-Options "DENY"; add_header X-Content-Type-Options "nosniff"; }
}
|
sudo ln -s /etc/nginx/sites-available/api.diraw.top.conf /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
|
sudo certbot --nginx -d api.diraw.top sudo nginx -t sudo systemctl reload nginx
|
之后把 https://github-readme-stats.vercel.app/api 替换成 https://api.diraw.top/github/stats 即可