
我有一個有趣的條件解析問題需要解決,但還沒有成功地在線搜索和查看 lighttpd 的文檔。其中許多搜尋導致了這裡提出的類似問題和有用的答案(對於這些問題,讓我們看看這個問題是如何運作的:
我在網關路由器(OpenWRT,或者 Turris OS,如果你喜歡的話,因為它是 Turris Omnia)上運行 lighttpd,並且它有許多指向它分配的路徑的域,作為 LAN 端伺服器的反向代理網關。
形式上的一般配置如下:
$HTTP["host"] =~ "(a.com|b.com|c.com)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(d.org|e.org)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(f.net|g.net)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
}
這一直是多年來的夢想。
現在我想要一個特定的路徑,所有這些網站都可以直接從該路由器提供服務。
再次準備考試:
$HTTP["url"] =~ "^/topdir/subir/" {
server.document-root = "/www/sharedstuff"
}
我可以將其完美地結合如下(並且它有效):
$HTTP["url"] =~ "^/topdir/subir/" {
server.document-root = "/www/sharedstuff"
} else {
$HTTP["host"] =~ "(a.com|b.com|c.com)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(d.org|e.org)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(f.net|g.net)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
}
}
甜的。
但是,這就是我想要解決的問題。理想情況下,我希望將$HTTP["url"]
條件封裝在一個包含的文件中,並將$HTTP["host"]
條件封裝在另一個包含的文件中,以便我可以:
include "/etc/lighttpd/conf.d/shared.conf" # contains the `$HTTP["url"]` constraint
include "/etc/lighttpd/conf.d/distributed.conf" # contains the `$HTTP["host"]` constraint
我想知道我是否對這裡抱有太多希望。因為我想不出或找到方法來做到這一點。
我想如果shared.conf
包含一些語句,則存在如下配置語句:
$HTTP["url"] =~ "^/topdir/subir/" {
server.document-root = "/www/sharedstuff"
ignore-all-subsequent-host-match-conditions
}
另一個創造性的想法,雖然天真且不可能,但我們是否可以重寫$HTTP["host"]
為:
$HTTP["host"] = "null.net"
這樣後續的匹配都會$HTTP["host"] =~ "(a.com|b.com|c.com)$"
失敗,並且請求保持在本地。
以下是迄今為止探索的一些選項:
伺服器變數
不行,因為這些是在載入設定時評估的,而不是在處理請求時評估的。
https://redmine.lighttpd.net/projects/1/wiki/docs_configuration#Using-variables
請求標頭
setenv.add-request-header
看起來很有吸引力:
https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModSetEnv
如果我們設定一個自訂請求標頭,也許我們可以使用in 來shared.conf
測試它:$REQUEST_HEADER["header"]
distributed.conf
https://redmine.lighttpd.net/projects/1/wiki/docs_configuration#Conditional-Configuration
但我在這方面還沒有任何成功。似乎條件是這樣的:
$REQUEST_HEADER["my_header"] == "value_I_set" {
# Do not act as a reverse proxy
} else $HTTP["host"] =~ "(a.com|b.com|c.com)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(d.org|e.org)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
} else $HTTP["host"] =~ "(f.net|g.net)$" {
proxy.server = ( "" => ( ( "host" => "..." ) ) )
...
}
根本行不通,我也無法真正弄清楚為什麼。很難看出發生了什麼,但如果我在條件處理上記錄輸出,$REQUEST_HEADER["my_header"]
即使對於shared.conf
匹配的 URL,它似乎也總是空白:
$HTTP["url"] =~ "^/topdir/subir/" {
setenv.add-request-header = ("my_header" => "value_I_set")
}
看起來這個條件並沒有測試由 setenv 設定的請求標頭,就像那些交付的那樣。
答案1
一種可能的配置解決方案是將您的共享配置後條件$HTTP["host"]
,以及在您的情況下,覆蓋代理配置
$HTTP["url"] =~ "^/topdir/subir/" {
server.document-root = "/www/sharedstuff"
proxy.server = ()
}
另一個解決方案,更靈活、更強大:lighttpd模組磁鐵允許你在幾行lua中寫出任意複雜的邏輯。您可以讓「共用」配置在 lighttpd mod_proxy 之前處理某些請求(在您的自訂 lua 腳本中)。
順便說一句,以下簡單的解決方案也有效嗎?共享配置:
$HTTP["url"] =~ "^/topdir/subir/" {
server.document-root = "/www/sharedstuff"
}
在包含在lighttpd.conf中的shared.conf中作為
include "/etc/lighttpd/conf.d/shared.conf" # contains the `$HTTP["url"]` constraint
else {
include "/etc/lighttpd/conf.d/distributed.conf" # contains the `$HTTP["host"]` constraint
}