惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
宝玉的分享
宝玉的分享
Jina AI
Jina AI
IT之家
IT之家
博客园 - Franky
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
美团技术团队
S
SegmentFault 最新的问题
罗磊的独立博客
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
D
Docker
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence

Dejavu's Blog

甲骨文 ARM 实例部署 Gemma 4 模型 Headscale + Tailscale 组建虚拟专用网 在 Linux 上使用 Yubikey OpenPGP 应用 BuyVM VPS 块存储挂载教程 Alpine Linux 服务器配置指南 Alpine Linux 安装 Cloudflared Docker 多容器共享中心数据库 安装 Komari 服务器监控工具 Scaleway VPS 安装 Debian Linux Debian 13 下部署 AsmBB 论坛 使用 Kopia 自动化备份服务器数据 给 Docker 启用 IPv6 支持 Netcup 服务器安装自定义 ISO 镜像 烽火 HG5582A 光猫开启桥接模式 Docker 自托管 Shlink 短链服务 部署 Obsidian LiveSync 实时同步服务指南 我的 2025 年不完全回顾 Linux 下 Intel 核显驱动配置与硬件加速 Fedora Linux 安装配置记录 2025 年优雅地自托管 RSS 服务 Woodpecker CI 和 Gitea 实现 Hugo 自动部署 Gitea/Forgejo 集成 Woodpecker CI/CD 在 Blinko 中使用 Ollama 作为 AI 供应商 Plausible CE 启用城市级地理位置识别 Blinko 开源 AI 知识库 Docker 部署指南 Netcup 免税账号注册及购买服务器全记录 Docker 自托管 Cloudreve Pro 私有网盘服务 GiffGaff SIM 卡使用体验和注意事项 简体中文互联网在变得糟糕吗? 如何低成本申请 S/MIME 证书用于个人邮件服务
Docker 部署 Gitea/Forgejo
Dejavu Moe · 2025-11-30 · via Dejavu's Blog

cover.webp

真香警告?

为什么不是 cGit 了?

几年前,当我 第一次尝试 cGit 的时候,我真的很喜欢它,甚至还在 Reddit 上的 Self-hosted 板块 发过帖子 分享它。

现在,不是爱变了,而是我现在想玩一下自己的 Git CI/CD,Gitea 就很好嘛。这是我在 Netcup RS 服务器 上运行的第 20 个容器,对于单用户服务,SQLite 就足够了。

比喝一杯茶更简单,只需准备一个目录

mkdir -p gitea/{data,config}

我使用的 rootless 镜像,需要先设置好绑定卷权限

cd gitea \
   && sudo chown 1000:1000 config/ data/

创建一个 Docker Compose 模板

看起来就像这样:

services:
  server:
    image: docker.gitea.com/gitea:1.25.2-rootless
    container_name: gitea
    restart: unless-stopped
    user: "1000"
    volumes:
      - ./data:/var/lib/gitea
      - ./config:/etc/gitea
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "127.0.0.1:33033:3000" # Gitea web & git with https
      - "127.0.0.1:11211:2222" # git with ssh

启动服务

sudo docker compose up -d

配置反向代理

上面暴露的两个端口,都可以通过 Cloudflare Tunnel 来访问,简单且安全。我嘛,画蛇添足一下,只将 Gitea Web 交给 Nginx 反向代理。当然了,SSH 交给 Cloudflare Tunnel 还是很有必要的。

Gitea 想要同时使用 HTTPS + SSH,我们需要准备两个子域名。一个给 Gitea Web 使用,另一个给 Git SSH。下面 git.via.moe 给前者,ssh.via.moe 给后者,好不优雅的说😑。

Nginx

给 Web 的 Nginx 虚拟主机配置示例:

server {
    listen 80;
    listen [::]:80;
    # git.your.domain
    server_name git.via.moe;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    # git.your.domain
    server_name git.via.moe;

    # your TLS/SSL cert
    ssl_certificate /path/to/your/sslcert.pem;
    ssl_certificate_key /path/to/your/sslcert.key;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ecdh_curve X25519:P-256:P-384;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:ECDHE+AES128:RSA+AES128:ECDHE+AES256:RSA+AES256';
    
    # for cloudflare cdn
    ssl_prefer_server_ciphers off;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    
    # log
    access_log /var/log/nginx/gitea.access.log;
    error_log /var/log/nginx/gitea.error.log;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

    location / {
        client_max_body_size 512M;
        proxy_pass http://127.0.0.1:33033;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $realip_remote_addr;
        proxy_set_header X-Forwarded-For $realip_remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection $http_connection;
        proxy_set_header Upgrade $http_upgrade;
    }
}

测试一下

没问题重载 Nginx 配置

Cloudflare Tunnel

安装 cloudflared ,Debian 系使用 Any 发行版(现在不再追随版本号)

# Add cloudflare gpg key
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

# Add this repo to your apt repositories
# Stable
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
# Nightly
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://next.pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list

# install cloudflared
sudo apt-get update && sudo apt-get install cloudflared

登录 Cloudflare Zero Trust 控制面板,新建一个隧道

create-tunnel.webp

选择 cloudflared 类型

chose-cloudflared.webp

根据系统类型复制安装命令,在服务器上执行

install-service.webp

已经连接上了

connected.webp

添加 SSH 路由

ssh-tunnel.webp

同理,如果 Gitea Web 打算也使用 Cloudflare Tunnel,输入域名,类型选择 TCP/HTTP,URL 设置为 127.0.0.1:33033 即可。

初始化配置

访问 Gitea 域名,进入初始化安装配置。注意将 SSH 域名设置为实际的域名,比如 ssh.via.moe,端口设置为 22

setup.webp

这些配置都在 config/app.ini 里,具体可以阅读 Gitea 配置速查手册

开始使用

将自己的 SSH 公钥添加到 Gitea/Forgejo,客户端需要安装 cloudflared 处理 SSH 代理

# Windows
scoop install cloudflared

# macOS
brew install cloudflared

确定下路径

which cloudflared
# Windows
D:\Scoop\shims\cloudflared.exe

编辑用户目录 .ssh 下的 config 文件

# 替换为实际的 SSH 域名
Host ssh.via.moe
  ProxyCommand D:\Scoop\shims\cloudflared.exe access ssh --hostname %h # 替换为具体的 cloudflared 路径

测试一下

ssh -T [email protected]
The authenticity of host 'ssh.via.moe (<no hostip for proxy command>)' can't be established.
RSA key fingerprint is SHA256:h4/WtxI78LY5gT9N1akvOBAEvI1Oq8EEpENC9hTtB7g.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ssh.via.moe' (RSA) to the list of known hosts.
Hi there, Dejavu Moe! You've successfully authenticated with the key named Yubikey, but Gitea does not provide shell access.
If this is unexpected, please log in with password and setup Gitea under another user.

需要更多的安全?

为了进一步提高安全性,可以选择在 Cloudflare Zero Trust 配置 Access 应用程序,只需添加上面两个子域名,并设置访问前的零信任验证即可。

(完)