Apache proxy / rewrite setup for specific file types inside sub directory

拈花ヽ惹草 提交于 2019-12-24 06:34:56

问题


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

https://mynew.domain.com

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

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

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

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

But

  • how do I whitelist is really the request is for whitelisted files ?
  • absolute path to file is really not predictable for me, can we pass something like

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]

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


回答1:


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 flag in this rule does what ProxyPass does for you.

This assumes mod_proxy is properly setup in Apache config.



来源:https://stackoverflow.com/questions/56679932/apache-proxy-rewrite-setup-for-specific-file-types-inside-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!