Apache 重寫規則以 DocumentRoot 為前綴

Apache 重寫規則以 DocumentRoot 為前綴

我有以下 VirtualHost 配置:

<VirtualHost *:80>
    DocumentRoot /srv/emptywebroot
    <Directory /srv/emptywebroot>
        Require all denied
    </Directory>

    # Dashboard proxy
    RewriteEngine on
    LogLevel alert rewrite:trace6
    RewriteCond %{HTTP:Upgrade} =websocket
    RewriteRule /dashboard/(.*) ws://dashboard:3838/dashboard/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket
    RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]
    ProxyPreserveHost on
    ProxyPassReverse /dashboard http://dashboard:3838/dashboard
</VirtualHost>

當我訪問時,http://myurl/dashboard我從儀表板後端收到 404。

當我檢查重寫日誌時,我得到:

[Wed Mar 21 14:04:08.051070 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] init rewrite engine with requested uri /dashboard/
[Wed Mar 21 14:04:08.051114 2018] [rewrite:trace1] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] pass through /dashboard/
[Wed Mar 21 14:04:08.051176 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] add path info postfix: /srv/emptywebroot/dashboard -> /srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051186 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] applying pattern '(.*)' to uri '/srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051199 2018] [rewrite:trace4] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] RewriteCond: input='' pattern='=websocket' => not-matched
[Wed Mar 21 14:04:08.051208 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] add path info postfix: /srv/emptywebroot/dashboard -> /srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051216 2018] [rewrite:trace3] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] applying pattern '(.*)' to uri '/srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051225 2018] [rewrite:trace4] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] RewriteCond: input='' pattern='!=websocket' => matched
[Wed Mar 21 14:04:08.051234 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] rewrite '/srv/emptywebroot/dashboard/' -> 'http://dashboard:3838//srv/emptywebroot/dashboard/'
[Wed Mar 21 14:04:08.051243 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] escaped URI in per-dir context for proxy, http://dashboard:3838//srv/emptywebroot/dashboard/ -> http://dashboard:3838//srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051264 2018] [rewrite:trace2] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] forcing proxy-throughput with http://dashboard:3838//srv/emptywebroot/dashboard/
[Wed Mar 21 14:04:08.051272 2018] [rewrite:trace1] [pid 1548:tid 140453800191744] mod_rewrite.c(476): [client 10.174.120.65:59506] 10.174.120.65 - - [idisrv.sivsa.com/sid#7fbdfdd1ed20][rid#7fbdf018f0a0/initial] [perdir /dashboard/] go-ahead with proxy request proxy:http://dashboard:3838//srv/emptywebroot/dashboard/ [OK]

因此,/srv/emptywebroot在將 URI 傳遞給代理之前會添加前綴。我希望不要加前綴。我嘗試過RewriteBase /其他一些事情,但我不明白這一點。

在這種情況下,如何避免 DocumentRoot 作為 URI 的前綴?

編輯1:儀表板後端:3838 不是由 apache 提供服務,它是一個閃亮的伺服器(https://shiny.rstudio.com/)。我已經在儀表板的主機上運行了 tcpdump,發生的情況如上面所示:DocumentRoot 以 apache 的代理虛擬主機為前綴,因此需要後端http://dashboard:3838//srv/emptywebroot/dashboard, 代替http://儀表板:3838/儀表板當我需要的時候。我希望這能澄清我的情況,因此重新表述問題:我如何指示 mod_rewrite 不解析本地路徑(前綴 DocumentRoot)?就我而言,這是一個 mod_rewrite 代理(檢查重寫規則中的 [P],因此為本地路徑添加前綴不是我想要的行為。

答案1

根據重寫日誌,您的規則集的這些行已被命中

RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]

由於RewriteRule有一個[L]標誌,下一行將不再被檢查

現在,該請求將進入下一輪重寫,<VirtualHost dashboard:3838> 您可能會在那裡找到 URI/dashboard/導致 HTTP 錯誤 404 的原因。

  • 這個目錄存在嗎?
  • 有沒有RewriteRule可以解決這個問題?

答案2

我發現透過之前在 REQUEST_URI 上添加重寫條件來強制重寫規則與 REQUEST_URI 匹配可以解決問題,因此以下內容對我有用:

<VirtualHost *:80>
    DocumentRoot /srv/emptywebroot
    <Directory /srv/emptywebroot>
        Require all denied
    </Directory>

    # Dashboard proxy
    RewriteEngine on
    LogLevel alert rewrite:trace6
    RewriteCond %{HTTP:Upgrade} =websocket
    RewriteCond %{REQUEST_URI} ^/dashboard
    RewriteRule /dashboard/(.*) ws://dashboard:3838/dashboard/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket
    RewriteCond %{REQUEST_URI} ^/dashboard
    RewriteRule /dashboard/(.*) http://dashboard:3838/dashboard/$1 [P,L]
    ProxyPreserveHost on
    ProxyPassReverse /dashboard http://dashboard:3838/dashboard
</VirtualHost>

我在以下位置找到了文檔http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule有點令人困惑,因為它指出「在 VirtualHost 上下文中,模式最初將與主機名稱和連接埠之後、查詢字串之前的 URL 部分進行匹配(例如「/app1/index.html」)。這是( %-decoded ) URL-path。因此,加入 RewriteCond 來修改 RewriteRule 重寫的內容(在我的例子中為 REQUEST_URI)似乎可行。

相關內容