为什么apache重写URL两次?

时间:2015-09-01 09:15:35

标签: node.js apache .htaccess tomcat httpd.conf

我有一个场景,其中有3台服务器:

  1. 端口8080上的Apache(Web服务器)
  2. 端口8083上的Tomcat(应用程序服务器)
  3. 端口8084上的NodeJs服务器(用于url预取)
  4. 我的要求是,如果请求到达8080,则将所有请求重定向到8083,除了具有" _escaped_fragment _ ="在它。

    所以我写了两个代理

    <IfModule mod_proxy_http.c>
      ProxyPass /makeSnapshot http://[IP]:8084
      ProxyPassReverse /makeSnapshot http://[IP]:8084
    </IfModule>
    <IfModule mod_proxy_http.c>
      ProxyPass /site http://[IP]:8083
      ProxyPassReverse /site http://[IP]:8083
    </IfModule>
    

    现在我已在配置文件中直接编写了一些重写规则,如下所示:

    <IfModule mod_rewrite.c>
        RewriteEngine On
            LogLevel alert rewrite:trace3
        RewriteBase /
        RewriteRule ^.*lib/(.*)$ /site/lib/$1 [NC,L]
        RewriteRule ^.*uploaded_files/(.*)$ /site/uploaded_files/$1 [NC,L]
        RewriteRule ^.*assets/(.*)$ /site/assets/$1 [NC,L]
        RewriteRule ^.*app/(.*)$ /site/app/$1 [NC,L]
    
        RewriteCond %{QUERY_STRING} ^.*_escaped_fragment_=(.*)
        RewriteRule ^.*$ /makeSnapshot?url=%1 [B,P,L,S=1]
    
        RewriteRule ^(.*)$ /site/$1 [L,P]
    </IfModule>
    

    我的期望是所有服务器调用都将被重定向到包含静态文件的tomcat,因为我在重写规则中提到过它。

    节点服务器用于生成从tomcat服务器查询的静态页面,并在解析javascript之后。

    现在的问题是,所有重定向都发生得很好,但是当从nodejs服务器呈现页面时,它会再次自动重定向到tomcat。

    我尝试了很多方法来解决这个问题,但我不确定为什么apache会将其重定向两次。

    非常感谢帮助。

1 个答案:

答案 0 :(得分:2)

当您使用P标志时,就好像您正在呼叫ProxyPass一样。所以你不需要你的vhost配置中的ProxyPass行(反正这些指令在htaccess中不起作用)并试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
        LogLevel alert rewrite:trace3
    RewriteBase /
    RewriteRule ^.*lib/(.*)$ /site/lib/$1 [NC,L]
    RewriteRule ^.*uploaded_files/(.*)$ /site/uploaded_files/$1 [NC,L]
    RewriteRule ^.*assets/(.*)$ /site/assets/$1 [NC,L]
    RewriteRule ^.*app/(.*)$ /site/app/$1 [NC,L]

    RewriteCond %{QUERY_STRING} ^.*_escaped_fragment_=(.*)
    RewriteRule ^.*$ http://[IP]:8084/?url=%1 [B,P,L,S=1]

    RewriteRule ^(.*)$ http://[IP]:8083/$1 [L,P]
</IfModule>