Apache Proxy和Redirect(Proxy与Redirect冲突问题),apacheredirect


最近在国外的一台机器上部署了公司的代码,但是因为有很多的图片资源,所以国内访问很慢,而客户这段时间刚好在国内,所以想要把所有的图片资源的链接重定向到国内的一台机器上。

本以为加上一段Redirect或者RedirectMatch什么的就可以了,但是加上了发现并没有起作用。看了一下Apache的配置,发现配置中还用到了proxy来指向node应用。

google了一下,猜测proxy会优先于Redirect/RedirectMatch,所以对于proxy已经命中的资源,RedirectRedirectMatch就不会在起作用,需要在proxy排除这些文件。可以使用类似ProxyPassMatch /static/ !来使Proxy对于/static/失效。再对/static/使用Redirect 301 /static/ http://www.other-domain.com/static

最后贴上完整一点的例子

<VirtualHost *:80>

        ServerName www.example.com
        DocumentRoot /var/www/html/www.example.com
        ProxyRequests Off

        RewriteEngine On
        RedirectMatch /static/(.*).(png|svg) http://www.other-domain.com/static/$1.$2
        <Directory />
              Options FollowSymLinks
              AllowOverride None
        </Directory>
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        ProxyPassMatch /static/(.*).(png|svg) !

        ProxyPass / http://127.0.0.1:7300/
        ProxyPassReverse / http://127.0.0.1:7300/


        ProxyPreserveHost On

        ...
</VirtualHost>

除了Redirect和RedirectMatch,也可以其他的重定向配置,例如RewriteRule等

相关内容

    暂无相关文章