Apache mod_rewrite为什么要删除uri部分?

问题描述 投票:1回答:1

我正在尝试将Apache配置为我们的websoket服务器https://chat.example.com的加密代理http://localhost:8000(在同一操作系统上)。我根据官方文档启用了mod_proxy,mod_proxy_http,mod_proxy_wstunnel,mod_rewrite和mod_ssl。然后,我向https://chat.example.com/connection/info?t=123144343发出了常规https GET请求,但是Apache将其重写为http://localhost:8000/?t=123144343而不是http://localhost:8000/connection/info?t=123144343。为什么?

<VirtualHost *:443>

    ServerAdmin [email protected]
    ServerName chat.example.com

    RewriteEngine On

    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule ^(.*)$ ws://localhost:8000/ [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule ^(.*)$ http://localhost:8000/ [P,L]

    ProxyRequests Off
    ProxyPreserveHost On

    SSLEngine on
    SSLProtocol +TLSv1 +TLSv1.1 +TLSv1.2 +TLSv1.3
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    SSLCertificateChainFile /etc/ssl/certs/example.com-chain.crt

    LogLevel rewrite:trace7
    ErrorLog /var/log/example.com/chat_error.log
    CustomLog /var/log/example.com/chat_access.log io

</VirtualHost>

[Wed Dec 25 18:57:16.057448 2019] [rewrite:trace2] [pid 9604:tid 140105269184256] mod_rewrite.c(483): init rewrite engine with requested uri /connection/info, referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057523 2019] [rewrite:trace3] [pid 9604:tid 140105269184256] mod_rewrite.c(483): applying pattern '^(.*)$' to uri '/connection/info', referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057566 2019] [rewrite:trace4] [pid 9604:tid 140105269184256] mod_rewrite.c(483): RewriteCond: input='' pattern='=websocket' [NC] => not-matched, referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057600 2019] [rewrite:trace3] [pid 9604:tid 140105269184256] mod_rewrite.c(483): applying pattern '^(.*)$' to uri '/connection/info', referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057637 2019] [rewrite:trace4] [pid 9604:tid 140105269184256] mod_rewrite.c(483): RewriteCond: input='' pattern='!=websocket' [NC] => matched, referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057670 2019] [rewrite:trace2] [pid 9604:tid 140105269184256] mod_rewrite.c(483): rewrite '/connection/info' -> 'http://localhost:8000/', referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057702 2019] [rewrite:trace2] [pid 9604:tid 140105269184256] mod_rewrite.c(483): forcing proxy-throughput with http://localhost:8000/, referer: https://example.com/personal/chats
[Wed Dec 25 18:57:16.057735 2019] [rewrite:trace1] [pid 9604:tid 140105269184256] mod_rewrite.c(483): go-ahead with proxy request proxy:http://localhost:8000/ [OK], referer: https://example.com/personal/chats

mod-rewrite apache2
1个回答
0
投票
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^(.*)$ ws://localhost:8000/ [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^(.*)$ http://localhost:8000/ [P,L]

您正在使用RewriteRule pattern捕获URL路径,即(.*)。但是,您没有在substitution中使用此捕获的URL路径,而只是重写到文档根目录:http://localhost:8000/。查询字符串将隐式复制到substitution

您需要使用$1后向引用将捕获的URL路径包括在substitution中。例如:

RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^(.*)$ ws://localhost:8000$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^(.*)$ http://localhost:8000$1 [P,L]

请注意,在virtualhost上下文中,与RewriteRule pattern匹配的URL路径是根目录,并包含斜杠前缀,因此,斜杠不应包含在susbititution]中。 (除非捕获的模式中省略了)。

如果此操作无效,请尝试使用REQUEST_URI服务器变量:

RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^ ws://localhost:8000%{REQUEST_URI} [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^ http://localhost:8000%{REQUEST_URI} [P,L]

EDIT:尽管我从问题历史记录中看到,但很令人困惑,(很惊讶,我最初没有看到这一点),原始问题中的代码块已经包含了$1反向引用,并且仅在您最近的edit?!

之后删除
© www.soinside.com 2019 - 2024. All rights reserved.