反向代理背后的 Wordpress

lyn*_*ynx 7 reverse-proxy wordpress apache-2.2

我们的组织正在改造它的网站。有人在新服务器上建立了新站点。将条目放入 /etc/hosts 后即可访问它。并且以这种方式访问​​时可以完美运行。

但是由于涉及的大多数人都不擅长计算机,因此我决定设置反向代理。

我无权访问该站点,也无权托管它的服务器。我在那里安装了 Wordpress 的编辑器帐户。

我在我的私人服务器的 /etc/hosts 中放置了一个条目并使用以下配置设置反向代理,我的服务器在 Debian stable 下运行 apache-2.2:

<VirtualHost *:80>
    ServerName xxx.xxx.xxx.xxx
    ProxyRequests off
    ProxyPass /some/prefix/ http://site.example.com/
    ProxyPassReverse /some/prefix/ http://site.example.com/
    ProxyHTMLURLMap http://site.example.com/ http://xxx.xxx.xxx.xxx/some/prefix/
    <Location /some/prefix/>
            SetOutputFilter INFLATE;proxy-html;DEFLATE
            ProxyHTMLURLMap  http://site.example.com/ /some/prefix/
    </Location>
    ProxyPassReverseCookieDomain site.example.com xxx.xxx.xxx.xxx
    ProxyPassReverseCookiePath / /some/prefix/
    ProxyHTMLExtended On
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

几乎一切正常。我无法发布新帖子(文本编辑器未正确加载)。Iceweasel 的 (Firefox) 开发者模式说:

(...)
[00:13:33.365] GET http://xxx.xxx.xxx.xxx/some/prefix/wp-includes/js/tinymce/langs/pl.js?wp-mce-4107-20141130 [HTTP/1.1 404 Not Found 399ms]
(...)
[00:13:33.648] Failed to load: http://xxx.xxx.xxx.xxx/some/prefix/wp-includes/js/tinymce/langs/pl.js
[00:13:46.733] POST http://xxx.xxx.xxx.xxx/wp-admin/admin-ajax.php [HTTP/1.1 404 Not Found 102ms]
Run Code Online (Sandbox Code Playgroud)

我省略了非错误。在我看来,Apache 并没有重写某些东西。有任何想法吗?

mas*_*oeh 2

这是我针对您的情况的工作配置。

<VirtualHost *:80>
    ServerName proxy.example.net
    ProxyRequests off
    ProxyPass /some/prefix/ http://backend.example.net/
    ProxyPassReverse /some/prefix/ http://backend.example.net/

    <Location /some/prefix/>
            ProxyHTMLEnable On
            ProxyHTMLExtended On

            ProxyHTMLLinks  a               href
            ProxyHTMLLinks  area            href
            ProxyHTMLLinks  link            href
            ProxyHTMLLinks  img             src longdesc usemap
            ProxyHTMLLinks  object          classid codebase data usemap
            ProxyHTMLLinks  q               cite
            ProxyHTMLLinks  blockquote      cite
            ProxyHTMLLinks  ins             cite
            ProxyHTMLLinks  del             cite
            ProxyHTMLLinks  form            action
            ProxyHTMLLinks  input           src usemap
            ProxyHTMLLinks  head            profile
            ProxyHTMLLinks  base            href
            ProxyHTMLLinks  script          src for
            ProxyHTMLLinks  iframe          src

            RequestHeader    unset  Accept-Encoding

            ProxyHTMLURLMap  /wp-admin/  /some/prefix/wp-admin/
            ProxyHTMLURLMap  \/wp-admin\/ \/some\/prefix\/wp-admin\/
            ProxyHTMLURLMap  http://backend.example.net/ http://proxy.example.net/some/prefix/
    </Location>
    ProxyPassReverseCookieDomain backend.example.net proxy.example.net
    ProxyPassReverseCookiePath / /some/prefix/

#    LogLevel warn proxy_html:trace3
    ErrorLog ${APACHE_LOG_DIR}/errorprox.log
    CustomLog ${APACHE_LOG_DIR}/accessprox.log combined

</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

一些解释

  • 我必须设置ProxyHTMLLinks,因为下面的apache日志中有一些错误。该配置是从这篇博客文章中删除的。

    [Sun Dec 21 23:02:49.053825 2014] [proxy_html:trace1] [pid 3368:tid 140385487116032] mod_proxy_html.c(823): [client 36.71.243.192:56711] 未配置链接:proxy-html 过滤器无需执行任何操作

  • 参数RequestHeader unset Accept-Encoding用于替代参数SetOutputFilter INFLATE;proxy-html;DEFLATE。效果是代理和真实 WordPress 之间的流量没有被压缩。详情请参阅此页。

  • URLwp-admin/admin-ajax.php由javascript定义和调用。参数ProxyHTMLExtended On应该完成这项工作。

  • 不带域定义的URL wp-admin/admin-ajax.php(在 Firefox 中单击“查看页面源”时可以看到它)。这导致参数http://site.example.com/ /some/prefix/与该字符串不匹配。所以,我设置了新参数

    • ProxyHTMLURLMap /wp-admin/ /some/prefix/wp-admin/对于常规字符串。
    • ProxyHTMLURLMap \/wp-admin\/ \/some\/prefix\/wp-admin\/对于转义字符串。