反向代理
Forgejo 可以独立运行,也可以部署在反向代理之后。
以下场景可能需要使用反向代理:
- 子路径映射。
如果希望将 Forgejo 部署在类似https://example.com/code/或https://example.com/repositories/的路径下,而不是默认的https://example.com。 - 端口映射。
如果希望让 Forgejo 占用标准端口,但该端口已被其他 Web 服务器占用。
即以https://example.com方式运行,而不是https://example.com:3000。 - 代理身份认证。
使用外部登录服务。
Forgejo 通常不需要代理即可实现此功能,因为它可以直接对接多种登录服务。 - 速率限制。
Fail2ban 可以限制 TCP 连接速率,但使用负载均衡器时可以检查请求头、执行 User-Agent 检测,并匹配 ACL 提供的信息。 - 高级安全设置。
使用负载均衡器时,可以应用内容安全策略、调整 SSL 密码套件,并配置Web 应用防火墙。 - 缓存与高可用性。
负载均衡器同时提供缓存和稳健性。例如,HAProxy 能够处理数百万个并发连接,而缓存可以减轻应用负载。
Forgejo 无需借助代理即可使用 HTTPS,它可以直接处理。
在配置 PROTOCOL=https 的 SERVER 部分进行设置,然后选择设置 CERT_FILE 和 KEY_FILE,或使用 ENABLE_ACME=true 让 Forgejo 管理证书。
子路径的安全性
浏览器的安全模型假定同源交互是安全的。[^1] 使用子路径后,浏览器的这一假定不再成立;如果该源还承载了由用户在不同子路径下输入的内容,使用 Forgejo 可能变得不安全。 Forgejo 不提供任何保护来缓解此类风险。 例如,同源上的恶意内容可能会对 Forgejo 发起 CSRF 攻击。
[^1]: 关于这一安全模型的推荐阅读:https://educatedguesswork.org/posts/web-security-model-origin。
NGINX
基础 HTTP
若要在 nginx 中配置基础 HTTP 反向代理,请在 /etc/nginx/conf.d 中创建文件 forgejo.conf,并添加以下配置:
server {
listen 80; # Listen on IPv4 port 80
listen [::]:80; # Listen on IPv6 port 80
server_name git.example.com; # Change this to the server domain name.
merge_slashes off; # Required to handle URL-encoded slashes
location / {
proxy_pass http://127.0.0.1:3000; # Port 3000 is the default Forgejo port
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
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;
client_max_body_size 512M;
}
}
更改配置后,请确保重新加载或重启 nginx。
使用子路径的 HTTP
如果希望通过子路径提供 Forgejo 服务,例如部署在 http://example.com/code,请使用以下配置:
server {
listen 80; # Listen on IPv4 port 80
listen [::]:80; # Listen on IPv6 port 80
server_name example.com; # Change this to the server domain name.
merge_slashes off; # Required to handle URL-encoded slashes
location /code/ { # Replace /code here with your subpath
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
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;
client_max_body_size 512M;
}
}
请确保将 Forgejo 的 ROOT_URL 配置项设置为包含子路径的 URL,否则 Forgejo 生成的链接将无法访问。
HTTPS
使用反向代理时,通常由代理处理 HTTPS 更简单。在 nginx 上配置 HTTPS 也很容易。
使用 Certbot 配置 HTTPS
若要使用 Certbot 配置 HTTPS,请先按上述配置搭建 HTTP 反向代理,并确认其工作正常。使用 HTTPS 需要拥有域名。
然后,安装 certbot。运行 certbot 时,选择承载 Forgejo 实例的域名,并选择自动安装。这应会自动在 443 端口配置 HTTPS,并将旧端口 80 重定向过去。
你可能还需要将 ROOT_URL 配置项改为 HTTPS 协议,使 Forgejo 生成的链接自动使用 HTTPS。
使用手动安装的证书配置 HTTPS
如果你从其他途径获得了证书,或选择不让 Certbot 自动安装证书,请对配置文件进行以下更改:
更改监听端口
将以下行
listen 80;
listen [::]:80;
改为
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
添加 SSL 证书信息
在 mozilla 生成 SSL 配置,并将 SSL 参数添加到配置文件中。请务必将示例中的路径替换为你的证书文件路径。
添加来自 HTTP 的重定向
在 server 块之外,添加以下重定向块:
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
return 301 https://$host$request_uri;
}
}
这会将访问 HTTP 站点的所有请求重定向到 HTTPS 站点。
Apache
基础 HTTP
若要在 Apache 中配置基础 HTTP 代理,请在 /etc/apache2/sites-available 中创建文件 100-forgejo.conf,并添加以下配置:
<VirtualHost *:80>
ServerName git.example.com
ProxyPreserveHost On
ProxyRequests off
AllowEncodedSlashes NoDecode
ProxyPass / http://127.0.0.1:3000/ nocanon
</VirtualHost>
接着,使用 a2ensite 100-forgejo.conf 启用该站点,并使用 a2enmod proxy proxy_http 启用代理模块。最后,重启 Apache 服务器。
使用子路径的 HTTP
如果希望通过子路径提供 Forgejo 服务,例如部署在 http://example.com/code,请使用以下配置:
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
ProxyRequests off
AllowEncodedSlashes NoDecode
ProxyPass /code http://127.0.0.1:3000/ nocanon # Change /code here to your desired subpath.
</VirtualHost>
请确保将 Forgejo 的 ROOT_URL 配置项设置为包含子路径的 URL,否则 Forgejo 生成的链接将无法访问。
HTTPS
使用反向代理时,通常由代理处理 HTTPS 更简单。在 Apache 上配置 HTTPS 也很容易。
使用 Certbot 配置 HTTPS
若要使用 Certbot 配置 HTTPS,请先按上述配置搭建 HTTP 反向代理,并确认其工作正常。使用 HTTPS 需要拥有域名。
然后,安装 certbot。运行 certbot 时,选择承载 Forgejo 实例的域名,并选择自动安装。这应会自动在 443 端口配置 HTTPS,并将旧端口 80 重定向过去。
你可能还需要将 ROOT_URL 配置项改为 HTTPS 协议,使 Forgejo 生成的链接自动使用 HTTPS。
使用手动安装的证书配置 HTTPS
如果你从其他途径获得了证书,或选择不让 certbot 自动安装证书,请对配置文件进行以下更改:
更改监听端口
将 <VirtualHost *:80> 改为 <VirtualHost *:443>。
添加 SSL 证书信息
在 mozilla 生成 SSL 配置,并将 SSL 参数添加到配置文件中。请务必将示例中的路径替换为你的证书文件路径。
添加来自 HTTP 的重定向
在 VirtualHost *:443 之外,添加以下配置:
<VirtualHost *:80>
ServerName git.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =git.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
这会将访问 HTTP 站点的所有请求重定向到 HTTPS 站点。
HAProxy
基础 HTTP
若要在 80 端口上配置不使用虚拟主机的 HAProxy,可以将以下配置段添加到 haproxy.cfg 中:
listen forgejo_80
bind :::80 v4v6
mode http
timeout connect 10s
timeout client 30s
timeout server 30s
server forgejo 127.0.0.1:3000
HTTPS
若要使用 HAProxy 配置基础 HTTPS 代理,可以将以下配置块添加到 haproxy 配置中:
重定向到 SSL
listen forgejo_80
bind :::80 v4v6
mode http
timeout connect 10s
timeout client 30s
timeout server 30s
redirect scheme https code 301
SSL 前端
frontend forgejo_443
bind :::443 v4v6 ssl crt /etc/haproxy_certs/forgejo.example.org.pem
mode http
option httplog
option forwardfor
timeout client 1m
use_backend forgejo_443 if { ssl_fc_sni forgejo.example.org }
SSL 后端
backend forgejo_443
mode http
timeout connect 10s
timeout server 30s
server forgejo 127.0.0.1:3000
使用 UNIX 套接字的 HTTPS
与 TCP 相比,Unix 套接字延迟更低。与 HAProxy 结合使用时,可提供响应迅速、用户体验出色的服务。
我们假设:
- 重定向到 SSL 和 SSL 前端配置与 TCP 部署方式保持一致。
- 以
git用户运行 Forgejo,以haproxy用户运行 HAProxy。 - chroot 环境设置为目录
/var/lib/haproxy。 - 已在 Forgejo 的
server配置段中加入以下设置:
[server]
PROTOCOL = http+unix
HTTP_ADDR = /var/lib/haproxy/forgejo/forgejo.sock
UNIX_SOCKET_PERMISSION = 660
...
接下来,需要创建一个目录,使 HAProxy 使用的 chroot 环境和 Forgejo 均可访问该目录。
install -o git -g haproxy -m 0770 -d /var/lib/haproxy/forgejo
最后,可以将以下配置块添加到 haproxy.cfg 中。
chroot
在 haproxy 配置的 global 部分中加入以下几行:
global
chroot /var/lib/haproxy
user haproxy
group haproxy
...
SSL 后端
后端配置如下:
backend forgejo_443
mode http
timeout connect 10s
timeout server 30s
server forgejo /forgejo/forgejo.sock tfo
注意:Unix 套接字路径相对于 chroot 环境的路径。
使用 Puppet 通过 UNIX 套接字部署 HAProxy
此配置依赖 Puppetlabs HAProxy 模块。
为简洁起见,此代码示例采用了折中方案。
除非提前添加创建用户的语句,并且无需针对 HAProxy 类设置两个目录的依赖关系,否则 Forgejo 后端只有在第二次执行 Puppet 时才可用。此外,还需要推送 SSL 证书,但这些内容均超出本文档的范围。
file {
default:
notify => Service['haproxy'],
require => [Class['haproxy'], User['git']];
'/etc/haproxy_certs':
ensure => directory,
purge => true,
mode => '0700',
owner => haproxy,
group => haproxy,
recurse => true;
'/var/lib/haproxy/forgejo':
ensure => directory,
mode => '0770',
owner => git,
group => haproxy;
}
class { 'haproxy':
package_ensure => $haproxy_version,
global_options => {
'log' => "/dev/log local0\n log /dev/log local1 notice",
'chroot' => '/var/lib/haproxy',
'maxconn' => '150000',
'user' => 'haproxy',
'group' => 'haproxy',
'stats' => 'socket /var/run/haproxy.sock user root group sensu mode 660 level admin',
'tune.ssl.default-dh-param' => '2048',
},
defaults_options => {
'default-server' => 'init-addr libc,none',
'log' => 'global',
'retries' => '5',
'option' => ['redispatch', 'http-server-close', 'logasap'],
'timeout' => ['http-request 7s', 'connect 5s', 'check 9s'],
'maxconn' => '5000',
};
}
haproxy::listen { 'forgejo_80':
bind => { ':::80' => ['v4v6'] },
mode => 'http',
options => [
{ 'timeout' => 'connect 10s' },
{ 'timeout' => 'client 1m' },
{ 'timeout' => 'server 1m' },
{ 'redirect' => 'scheme https code 301' },
];
}
haproxy::frontend { 'forgejo_socket':
bind => { ':::443' => ['v4v6', 'ssl', 'crt', '/etc/haproxy_certs/forgejo.example.com.pem'] },
options => [
{
mode => 'http',
option => ['httplog', 'forwardfor'],
},
{ 'timeout' => 'client 1m' },
{ 'use_backend' => "forgejo if { ssl_fc_sni forgejo.example.com }" },
];
}
haproxy::backend { 'forgejo_socket':
options => [
{ 'mode' => 'http' },
{ 'timeout' => 'connect 10s' },
{ 'timeout' => 'server 1m' },
{ 'server' => 'forgejo /forgejo/forgejo.sock tfo' },
];
}
Caddy
HTTPS
若要使用 Caddyfile 在 Caddy 中配置基础 HTTPS 代理,请在 /etc/caddy/conf.d 中创建文件 forgejo,并添加以下配置:
git.example.com {
reverse_proxy 127.0.0.1:3000
}
Caddy 会自动为该域名获取证书。
使用子路径的 HTTPS
如果希望通过子路径提供 Forgejo 服务,例如部署在 https://example.com/code,请使用以下配置:
example.com {
route /code/* {
uri strip_prefix /code
reverse_proxy 127.0.0.1:3000
}
}
请确保将 Forgejo 的 ROOT_URL 配置项设置为包含子路径的 URL,否则 Forgejo 生成的链接将无法访问。
代理身份认证
Forgejo 支持反向代理请求头身份认证,它会将请求头读取为受信任的登录用户名或用户邮箱地址。此功能默认未启用,可通过以下方式启用:
[service]
ENABLE_REVERSE_PROXY_AUTHENTICATION = true
默认登录用户名位于 X-WEBAUTH-USER 请求头中,可以通过修改 app.ini 中的 [security].REVERSE_PROXY_AUTHENTICATION_USER 进行更改。如果用户不存在,可以通过 ENABLE_REVERSE_PROXY_AUTO_REGISTRATION=true 启用自动注册。
默认登录用户邮箱为 X-WEBAUTH-EMAIL,可以通过修改 app.ini 中的 [security].REVERSE_PROXY_AUTHENTICATION_EMAIL 进行更改,也可以通过 ENABLE_REVERSE_PROXY_EMAIL 禁用。
如果设置了 ENABLE_REVERSE_PROXY_FULL_NAME=true,则自动创建用户时,会将 X-WEBAUTH-FULLNAME 中所期望的用户全名赋予该用户。也可以通过 [security].REVERSE_PROXY_AUTHENTICATION_FULL_NAME 更改请求头名称。
还可以通过 [security].REVERSE_PROXY_TRUSTED_PROXIES 限制反向代理的 IP 地址范围,其默认值为 127.0.0.0/8,::1/128。通过 [security].REVERSE_PROXY_LIMIT 可以限制受信任代理的层级。
注意:反向代理身份认证不支持 API。发起 API 请求时,仍需使用访问令牌或基本身份认证。
Docker / 容器注册表
容器注册表使用固定子路径 /v2,该路径无法更改。
即使以不同的子路径部署 Forgejo,docker 客户端仍会使用 /v2。
因此,你可能需要在反向代理配置中额外添加一条路由。
谢谢你的反馈!