



























为什么不是 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 给后者,好不优雅的说😑。
给 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 配置
安装 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 控制面板,新建一个隧道

选择 cloudflared 类型

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

已经连接上了

添加 SSH 路由

同理,如果 Gitea Web 打算也使用 Cloudflare Tunnel,输入域名,类型选择 TCP/HTTP,URL 设置为 127.0.0.1:33033 即可。
访问 Gitea 域名,进入初始化安装配置。注意将 SSH 域名设置为实际的域名,比如 ssh.via.moe,端口设置为 22。

这些配置都在 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 应用程序,只需添加上面两个子域名,并设置访问前的零信任验证即可。
(完)
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。