【问题标题】:Apache proxy / rewrite setup for specific file types inside sub directory子目录中特定文件类型的 Apache 代理/重写设置
【发布时间】:2019-06-20 16:16:09
【问题描述】:

最近我不得不迁移一个将近 15 岁的网站,考虑到新的 url 是

https://mynew.domain.com

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

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

所以我想要实现的是假设如果用户在浏览器上点击 url,这是具有白名单扩展名的文件请求,例如(.jpg、.jpeg、.png、.pdf、.doc、.docx、.gif、.svg ) 例如,然后从内部 IP 处理这些请求

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

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

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

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

我也试过了,下面是一个错误请求,您的浏览器发送了一个此服务器无法理解的请求

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

如果我将[P,L] 修改为[R=301,L] 则以上相同,它会正确重定向到其他服务器,不确定代理为什么不起作用

【问题讨论】:

  • 为什么白名单很重要?如果浏览器请求未知扩展名,例如.txt,那么它只会在ProxyPass 转发之后导致404。
  • @anubhava : 先生,有很多文件,我只想允许一些扩展名,例如 pdf jpeg jpg png svg doc docx ppt pptx
  • @anubhava : 先生,我尝试了更多重写尝试,请查看更新,但没有成功

标签: apache mod-rewrite reverse-proxy


【解决方案1】:

要允许某些列入白名单的扩展,您可以使用 mod_rewrite 规则而不是 ProxyPass

RewriteEngine On

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

此规则中的P 标志执行ProxyPass 为您执行的操作。

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

【讨论】:

  • 我收到 400 个错误请求,可能是什么问题?
  • 当您使用标志作为[L,NC] 时会发生什么?
  • 没有P,如果我使用,浏览器正在下载文件,但P它的400
  • 这意味着mod_proxy 没有正确设置。您必须联系您的站点支持人员。
  • 我在另外一台机器上测试了你的代码运行良好,其中有 proxy_module (shared) proxy_ajp_module (shared) proxy_balancer_module (shared) proxy_connect_module (shared) proxy_express_module (shared) proxy_fcgi_module (shared) proxy_fdpass_module (shared) proxy_ftp_module (shared) proxy_http_module (shared) proxy_scgi_module (shared) proxy_wstunnel_module (shared) 非常感谢,你救了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 2020-10-22
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多