子目录中特定文件类型的Apache代理/重写设置

问题描述:

最近我不得不迁移一个已有近15年历史的网站,考虑新的URL是

Recently I had to migrate somesite which was nearly 15 years old, consider the new url is

https://mynew.domain.com

,旧站点为 https://myold.domain.com (内部IP:192.168.1.15 ,在公共领域不可用)

and the old site was https://myold.domain.com (Internal IP : 192.168.1.15, which is not available in public domain)

在旧站点中,它有一个名为"sitefiles"的目录,其中包含许多子文件夹和文件,例如图像,文件等

In old site it was having directory called "sitefiles", in which many sub folders and files like images, files etc

所以我要实现的假设是,如果用户在浏览器上点击了带有白名单扩展名(例如.jpg,.jpeg,.png,.pdf,.doc,.docx,.gif,.svg的文件请求)的URL, ),例如,然后从内部IP服务那些请求

So what I want to achieve is suppose if user hits url on browser which was file request with white listed extensions such (.jpg , .jpeg, .png, .pdf, .doc, .docx, .gif, .svg) say for example, then serve those requests from internal IP

https://mynew.domain.com/sitefiles/events/somedirectory /somefile.jpg

https://mynew.domain.com/sitefiles/somedoc.jpg

请注意sitefiles目录很大,而且它是嵌套的,我不知道该怎么做,目前我尝试的是

Please note sitefiles directory is quite big, and its nested, I don't know how to go about this, currently what I tried is

ProxyPass /sitefiles/ http://192.168.1.15/sitefiles
ProxyPassReverse /sitefiles/ http://192.168.1.15/sitefiles

但是

  • 我如何将白名单实际上是对列入白名单的文件的请求?
  • 文件的绝对路径对我来说真的是不可预测的,我们可以传递
  • 之类的信息吗?

ProxyPass %{REQUEST_URI} http://192.168.1.15/$1

我也尝试过,在下面一个让我不好的请求下,您的浏览器发送了该服务器无法理解的请求

I also tried, below one which gives me Bad Request your browser sent a request that this server could not understand

RewriteCond %{REQUEST_URI} ^/sitefiles
RewriteRule ^\/?(.*)$ http://192.168.1.15/$1 [P,L]

如果我将[P,L]修改为[R=301,L],它可以正确地重定向到其他服务器,则不知道相同的原因,

Above same one if I modify [P,L] to [R=301,L] it properly redirecting to other server, not sure why proxy is not working

要允许某些列入白名单的扩展,您可以使用mod_rewrite规则代替ProxyPass:

To allow certain whitelisted extensions you may use mod_rewrite rule instead of ProxyPass:

RewriteEngine On

RewriteRule ^sitefiles/.+\.(?:jpe?g|png|pdf|docx?|gif|svg|pptx?)$ http://192.168.1.15/$0 [L,NC,P]

此规则中的

P标志执行ProxyPass为您执行的操作.

P flag in this rule does what ProxyPass does for you.

这假定mod_proxy已在Apache配置中正确设置.

This assumes mod_proxy is properly setup in Apache config.