多域名实例配置

多域名实例配置(Multi-Domain Instance Configuration)

Wavelog 可以从不同的 URL 访问。如果你的 Wavelog 实例只应通过一个域名访问,可以使用 config.php 中设置的默认配置。特别感谢 @adilinden 在这次讨论中提出的想法!

<?php
$config['base_url'] = 'https://wavelog.example.com/';

然而在某些情况下,你希望 Wavelog 实例可以从不同域名访问,这时可以把该行替换为:

<?php
$config['base_url'] = call_user_func(function () {
    // Whitelist of allowed domains for this Wavelog instance
    // Ignore the port here. It's just about domain/subdomain
    // IPv4 works, IPv6 don't due to the regex
    // In docker environments it's important to add 'localhost' aswell
    $url_allowed   = [
        'wavelog.example.org',
        'log.domain2.xyz',
        '192.168.0.123',
        'localhost'
    ];

    // Define the redirect URL for invalid/non-whitelisted hosts
    $redirect_url  = 'https://www.wavelog.org/';

    // Early fail-fast: reject empty or too long hosts before expensive regex
    $http_host     = strtolower($_SERVER['HTTP_HOST'] ?? '');
    if ($http_host === '' || strlen($http_host) > 259
        || !preg_match('/^([a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?)*)(:([1-9][0-9]{0,4}))?$/', $http_host, $m)
        || (!empty($m[6]) && (int)$m[6] > 65535)
        || !in_array($m[1], $url_allowed, true)
    ) {
        header('Location: ' . $redirect_url, true, 301);
        exit;
    }

    $is_https = (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
        || (!empty($_SERVER['HTTP_X_FORWARDED_SSL'])   && $_SERVER['HTTP_X_FORWARDED_SSL'] === 'on')
        || (!empty($_SERVER['HTTPS'])                  && $_SERVER['HTTPS'] === 'on')
        || (!empty($_SERVER['SERVER_PORT'])             && (int)$_SERVER['SERVER_PORT'] === 443);

    $port = !empty($m[6]) ? ':' . $m[6] : '';

    return ($is_https ? 'https' : 'http') . '://' . $m[1] . $port . '/';
});
Wavelog 手册(中文版) / 多域名实例配置 0 字 0 行 bg8lng
2026-10-21T12:42:36.006095296Z 2026-10-21T12:43:39.254523051Z