Apache mod_rewrite:将请求从本地主机重定向到远程服务器
我有以下一个场景:Apache mod_rewrite:将请求从本地主机重定向到远程服务器,apache,mod-rewrite,Apache,Mod Rewrite,我有以下一个场景: 远程服务器,某些webapp在http://remote/webapp 公司网络内的本地机器 他们之间的公司代理 在本地机器上运行mod_rewrite的Apache 我想让mod_代理重写每个请求,比如http://localhost/webapp?someparams进入http://remote/webapp?someparams 目前我有以下httpd.conf: DocumentRoot "C:/Apache2.2/htdocs" <Directory
- 远程服务器,某些webapp在
http://remote/webapp
- 公司网络内的本地机器
- 他们之间的公司代理
- 在本地机器上运行mod_rewrite的Apache
http://localhost/webapp?someparams
进入http://remote/webapp?someparams
目前我有以下httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
<Directory />
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
DocumentRoot“C:/Apache2.2/htdocs”
重新启动发动机
重写规则^(+)http://remote/$1
选项如下符号链接
允许超越所有
命令拒绝,允许
全盘否定
这会导致mod_rewrite转换http://localhost/webapp?someparams
进入http://remote/C:/Apache2.2/htdocs/webapp?someparams
如何配置mod_rewrite以正确处理它?因为看起来您可以访问vhost/server配置,所以您应该放弃mod_rewrite,只使用mod_代理:
ProxyPass /webapp http://remote/webapp
ProxyPassReverse /webapp http://remote/webapp
并去掉2个mod_重写行(这是重定向,而不是代理):
请注意,如果您有cookie,则可能需要反向映射其域。使用
此外:
windows绝对路径出现在URL中的事实是由于mod_重写的错误配置,这是我试图避免的 这不是mod_rewrite的错误配置。将重写规则放入
中时,将在匹配中使用文件路径,而不是URI路径。根据
什么是匹配的?
在VirtualHost
上下文中,模式最初将与URL中主机名和端口之后、查询字符串之前的部分匹配(例如“/app1/index.html”)
在目录
和htaccess上下文中,在删除将服务器引导到当前重写规则的前缀后,模式最初将与文件系统路径匹配(例如,“app1/index.html”或“index.html”,具体取决于指令的定义位置)
谢谢Jon的灵感,最后mod_代理+mod_重写成功了:
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/
我知道这是一个简单而粗糙的解决方案,但对我来说是可行的。我认为这叫做反向代理,你可能想看看upI认为拒绝来自所有人是罪魁祸首,允许来自所有人将起作用我认为,你能试一次吗?而且,你的期望是不可预测的,你在URL中使用的是windows绝对路径,真的无法找出需要什么windows绝对路径出现在URL中的事实是由于对mod_重写的错误配置,这是我试图避免的。与域名一起使用的ProxyPass导致“DNS查找失败”错误,我不能使用IP代替域名。但是谢谢!
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/