Lighttpd 1.4.45 - HTTP 到 HTTPS 重定向未替换 ${url.authority}${url.path}${qsa}

Lighttpd 1.4.45 - HTTP 到 HTTPS 重定向未替换 ${url.authority}${url.path}${qsa}

我尝试重新配置 lighttpd,以自动将所有 HTTP 请求重新定位为 HTTPS 请求。为此,我在以下配置中找到了Lighttpd Redmine 维基

$HTTP["scheme"] == "http" {
            url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}

但是,lighttpd 似乎不会替换占位符。以下是通过 telnet 的示例 HTTP 请求/响应(我将我的主机名替换为“example.org”,其余均为原始内容):

# telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.0
Host: example.org

HTTP/1.0 301 Moved Permanently
Location: https://${url.authority}${url.path}${qsa}
Content-Length: 0
Connection: close
Date: Fri, 11 Sep 2020 12:43:32 GMT
Server: lighttpd/1.4.45

Connection closed by foreign host.

而不是Location: https://${url.authority}${url.path}${qsa}应该的线Location: https://example.org/

出了什么问题?我是不是忘了加载模块了?

有任何想法吗?

我的 lighttpd.conf 的某些部分:

由于模块的加载顺序似乎很重要。这是我当前的配置:

server.modules = (
        "mod_expire",
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_rewrite",
        "mod_setenv"
)
# cat /etc/debian_version
9.13

Lighttpd 似乎对配置文件很满意(没有 -t 或 -tt 的输出):

# lighttpd -tt -v -f /etc/lighttpd/lighttpd.conf
lighttpd/1.4.45 (ssl) - a light and fast webserver
Build-Date: Jan 14 2017 21:07:19
#

答案1

经过更多研究,我终于找到了解决方案:

命名占位符仅支持 lighttpd 版本 1.4.50+,但对于旧版本有不同的解决方案:

$HTTP["scheme"] == "http" {
        $HTTP["host"] =~ ".*" {
                url.redirect = (".*" => "https://%0$0")
        }
}

这似乎有效。%0被完整主机名(最后一个正则表达式匹配的结果)替换,并被$0包括查询字符串部分的整个 URI 替换。这正是我需要的...

相关内容