【问题标题】:Why does Apache mod_rewrite remove the uri part?为什么 Apache mod_rewrite 会删除 uri 部分?
【发布时间】:2020-01-06 19:02:19
【问题描述】:

我正在尝试将 Apache 配置为我们的 websoket 服务器 http://localhost:8000 的加密代理 https://chat.example.com(在同一操作系统上)。我根据官方文档启用了 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 admin@example.com
    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

【问题讨论】:

  • “我向http://chat.example.com/connection/info?t=123144343 发出了一个常规的http GET 请求” - 那是HTTP(端口80),而不是HTTPS(端口443)?或者这只是你问题中的一个错字?
  • 对不起,这只是一个错字。固定

标签: mod-rewrite apache2


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

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

您需要使用$1 反向引用在替换 中包含捕获的URL 路径。例如:

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 路径是相对于根的,并且包含斜杠前缀,因此斜杠不应被包含在 susbtitution 中(除非从捕获的模式中省略)。

如果这不起作用,请尝试改用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]

编辑:虽然令人困惑的是,我从问题历史中看到(令我惊讶的是,我最初没有看到这一点)原始问题中的代码块已经包含 $1反向引用并且仅在您最近编辑之后才被删除?! (尽管按照它的实现方式,它会导致在 URl 路径的开头出现双斜杠。)

【讨论】:

  • 对不起。第一次,我从错误的 vhost 文件中复制了该配置。非常感谢!现在我详细了解了我的错误。
猜你喜欢
  • 2016-05-21
  • 1970-01-01
  • 2021-11-17
  • 2011-08-10
  • 2012-03-24
  • 2021-01-21
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多