【问题标题】:Apache URL rewrite before Proxy代理之前的 Apache URL 重写
【发布时间】:2015-02-06 11:07:34
【问题描述】:

我已将 apache 设置为需要在特定端口上运行的应用程序的代理,这适用于虚拟主机。现在我需要添加另一个指令,我需要从任何传入请求中删除 /ex/ 并将重写的 url 传递给代理。

我似乎无法同时工作,要么 apache 能够重写 url,但随后无法代理并尝试自己提供请求,或者它在不删除 /ex/ 的情况下正确代理,并且我的应用程序路由失败,因为它正在寻找 /ex/。

这是有效的代理配置(无需重写)。 如何在将 /ex/ 传递给代理之前将其删除?显然apache不能同时[P,R],PT只是按原样转发。

<VirtualHost *:82>
  ServerName xxx
  ServerAlias xxx

  DocumentRoot /opt/xxx

  RewriteEngine On

  # neither of these works, simply proxies as is to my application, routing fails
  # RewriteRule ^ex/(.*) /$1 [L,R]
  # RewriteRule ^/ex$ / [L,PT]

  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ http://127.0.0.1:11110%{REQUEST_URI} [P,QSA,L]

  ProxyPass / http://127.0.0.1:11110/
  ProxyPassReverse / http://127.0.0.1:11110/
  ProxyPreserveHost on

  # this doesn't work either
  # ProxyPass /ex http://127.0.0.1:11110/

</VirtualHost>

【问题讨论】:

    标签: apache mod-rewrite virtualhost mod-proxy


    【解决方案1】:

    您可以只使用 ProxyPassMatch 代替 ProxyPass:

    ProxyPassMatch ^/ex/(.*) http://127.0.0.1:11110/$1
    

    【讨论】:

      【解决方案2】:

      简单,这一行

      ProxyPass /ex http://127.0.0.1:11110/
      

      应该在之前

      ProxyPass / http://127.0.0.1:11110/
      

      因为 / 将匹配所有内容,它比 /ex 更大,因此 ProxyPass / 应该始终位于配置文件的最后。这意味着如果没有找到匹配的 /pattern 则 / 将在最后处理所有模式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-23
        • 2012-08-09
        • 2011-10-21
        • 2014-05-29
        • 2014-03-17
        • 2017-03-16
        • 2016-06-14
        • 2018-02-01
        相关资源
        最近更新 更多