LaySite 1.4.6 · 简化 CDN 回源 IP 配置 #17

Merged
enoch merged 2 commits from ChatGPT/laysite:laysite-1.4.6 into main 2026-11-27 23:59:29 +08:00
4 changed files with 29 additions and 16 deletions
Showing only changes of commit 2117ba1944 - Show all commits
+4
View File
@@ -8,7 +8,11 @@ LAYSITE_SITE_URL=http://127.0.0.1:8787
# LAYSITE_WORKERS=2 # LAYSITE_WORKERS=2
# LAYSITE_DB=storage/laysite.sqlite # LAYSITE_DB=storage/laysite.sqlite
# LAYSITE_MEDIA_DIR=storage/media # LAYSITE_MEDIA_DIR=storage/media
# 默认按 Webman 的规则信任内网代理。可选填固定代理 IP 或留空禁用转发头。
# LAYSITE_TRUSTED_PROXIES=127.0.0.1,::1 # LAYSITE_TRUSTED_PROXIES=127.0.0.1,::1
# 源站入口已限制为仅 CDN 回源时,可信任所有回源地址。
# LAYSITE_TRUSTED_PROXIES=*
# MCP 请在后台「站点设置」配置。此变量仅用于兼容已有部署。 # MCP 请在后台「站点设置」配置。此变量仅用于兼容已有部署。
# LAYSITE_MCP_TOKEN= # LAYSITE_MCP_TOKEN=
+7 -3
View File
@@ -28,7 +28,7 @@ php start.php start
| `LAYSITE_DB` | SQLite 文件,默认 `storage/laysite.sqlite` | | `LAYSITE_DB` | SQLite 文件,默认 `storage/laysite.sqlite` |
| `LAYSITE_MEDIA_DIR` | 用户上传目录,默认 `storage/media` | | `LAYSITE_MEDIA_DIR` | 用户上传目录,默认 `storage/media` |
| `LAYSITE_SECURE_COOKIES` | 生产 HTTPS 设为 `1`。本机 HTTP 留空 | | `LAYSITE_SECURE_COOKIES` | 生产 HTTPS 设为 `1`。本机 HTTP 留空 |
| `LAYSITE_TRUSTED_PROXIES` | 信任的反代 IP,默认 `127.0.0.1,::1` | | `LAYSITE_TRUSTED_PROXIES` | 可选。默认按 Webman 的规则信任内网代理。可填写逗号分隔的代理 IP,或在入口已限制为仅 CDN 回源时填 `*`。显式空值禁用转发头 |
| `LAYSITE_MCP_TOKEN` | 兼容已有部署的 MCP Bearer 密钥,至少 32 字符。首次保存后台 MCP 设置后由后台管理 | | `LAYSITE_MCP_TOKEN` | 兼容已有部署的 MCP Bearer 密钥,至少 32 字符。首次保存后台 MCP 设置后由后台管理 |
## 页面与站点设计 ## 页面与站点设计
@@ -160,7 +160,7 @@ docker run -d --name laysite --restart unless-stopped \
-e LAYSITE_SITE_URL=https://example.com \ -e LAYSITE_SITE_URL=https://example.com \
-e LAYSITE_SECURE_COOKIES=1 \ -e LAYSITE_SECURE_COOKIES=1 \
-v laysite-storage:/opt/laysite/storage \ -v laysite-storage:/opt/laysite/storage \
git.laysense.com/laysense/laysite:v1.4.5 git.laysense.com/laysense/laysite:v1.4.6
docker exec -it laysite php bin/admin.php admin@example.com docker exec -it laysite php bin/admin.php admin@example.com
``` ```
@@ -172,6 +172,8 @@ Gitea Actions 使用 `host` 标签。runner 的运行账户需要访问 Docker
在 `.env` 配置 `LAYSITE_SITE_URL=https://example.com` 和 `LAYSITE_SECURE_COOKIES=1`。下列示例假定代理和 Webman 在同一主机。代理应以实际访客来源覆盖 `X-Real-IP`,并将项目文件、数据库和媒体存储保持在公开根目录之外。 在 `.env` 配置 `LAYSITE_SITE_URL=https://example.com` 和 `LAYSITE_SECURE_COOKIES=1`。下列示例假定代理和 Webman 在同一主机。代理应以实际访客来源覆盖 `X-Real-IP`,并将项目文件、数据库和媒体存储保持在公开根目录之外。
LaySite 用访客地址记录后台登录失败次数。公网 CDN 可以直接回源 Webman。源站防火墙或认证回源已限制入口只接受 CDN 连接时,设置 `LAYSITE_TRUSTED_PROXIES=*` 即可,适用于回源 IP 会变化的 CDN。也可以在该变量填写明确的回源 IP。可信连接优先用 `X-Real-IP`、`CF-Connecting-IP`,随后交给 Webman 解析其他转发头。同机和 Docker 内网代理可使用默认设置。
### Nginx ### Nginx
在现有 HTTPS 虚拟主机内加入: 在现有 HTTPS 虚拟主机内加入:
@@ -180,6 +182,7 @@ Gitea Actions 使用 `host` 标签。runner 的运行账户需要访问 Docker
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
location / { location / {
proxy_pass http://127.0.0.1:8787; proxy_pass http://127.0.0.1:8787;
@@ -206,7 +209,8 @@ example.com {
max_size 2MiB max_size 2MiB
} }
reverse_proxy 127.0.0.1:8787 { reverse_proxy 127.0.0.1:8787 {
header_up X-Real-IP {remote_host} header_up X-Real-IP {client_ip}
header_up X-Forwarded-For {client_ip}
} }
} }
``` ```
+1 -1
View File
@@ -1 +1 @@
1.4.5 1.4.6
+17 -12
View File
@@ -8,24 +8,29 @@ use App\Config\Environment;
final class Request extends \support\Request final class Request extends \support\Request
{ {
private const LOOPBACK = ['127.0.0.1', '::1'];
public function clientAddress(): string public function clientAddress(): string
{ {
$peer = $this->getRemoteIp(); $peer = trim($this->getRemoteIp(), '[]');
$forwarded = $this->header('x-real-ip', ''); $configured = Environment::get('LAYSITE_TRUSTED_PROXIES');
if (!$this->fromTrustedProxy() || !filter_var($forwarded, FILTER_VALIDATE_IP)) { $trusted = match (true) {
$configured === false => self::isIntranetIp($peer),
trim($configured) === '*' => true,
default => in_array($peer, array_map('trim', explode(',', $configured)), true),
};
if (!$trusted) {
return $peer; return $peer;
} }
return inet_ntop(inet_pton($forwarded)); // Prefer a single address set by the ingress over a forwarded chain.
} foreach (['x-real-ip', 'cf-connecting-ip'] as $name) {
$address = $this->header($name);
if (filter_var($address, FILTER_VALIDATE_IP)) {
return inet_ntop(inet_pton($address));
}
}
private function fromTrustedProxy(): bool $address = $this->getRealIp(safeMode: false);
{
$configured = Environment::get('LAYSITE_TRUSTED_PROXIES');
$trusted = $configured === false ? self::LOOPBACK : array_map('trim', explode(',', $configured));
return in_array($this->getRemoteIp(), $trusted, true); return filter_var($address, FILTER_VALIDATE_IP) ? inet_ntop(inet_pton($address)) : $peer;
} }
} }