apache mod_proxy,配置ProxyPass & ProxyPassReverse 用于跨域ajax调用
我正在创建一个 html5 - JavaScript 应用程序(适用于移动设备,使用 PhoneGap)。我必须与 REST 服务交互。
该服务现在运行在 "http://localhost:8080/backend/mvc/"
我正在 wamp 服务器 (apache2) 上开发我的应用程序 (http://localhost/阶段/
) 我使用 Chrome 浏览器。
当执行 ajax 调用时,浏览器响应: XMLHttpRequest 无法加载 http://localhost:8080/backend/mvc/event。 Access-Control-Allow-Origin 不允许 Origin http://localhost。
所以我找到了几种方法来规避这个跨域 ajax 调用问题:
1) 启动 chrome chrome.exe --disable -网络安全
=>没有区别
2) 使用 mod_proxy 配置 apache 来重定向流量。
我在httpd.conf中启用:
proxy_module
proxy_connect_module
proxy_http_module
我在www根目录中放置了一个.htaccess
文件,其中包含以下内容:
# start mod_rewrite
RewriteEngine On
ProxyRequests off
<Proxy>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /EMBackend/ http://localhost:8080/backend/mvc/
ProxyPassReverse /EMBackend/ http://localhost:8080/backend/mvc/
RewriteRule ^/EMBackend/(.*)$ /backend/mvc/$1 [R]
我重新启动了所有服务(apache,php,..),
导致错误500
apache错误日志: [2011 年 10 月 18 日星期二 14:30:11] [alert] [client 127.0.0.1] C:/wamp/www/.htaccess: ProxyRequests 不此处允许
有关如何解决此问题的任何线索吗?
I'm creating an html5 - JavaScript app (for mobile devices, using PhoneGap). I have to interact with a REST service.
The service is now running on "http://localhost:8080/backend/mvc/"
I'm developing my app on an wamp server (apache2) (http://localhost/stage/
)
I'm using Chrome for browser.
when preforming an ajax call the browser responds: XMLHttpRequest cannot load http://localhost:8080/backend/mvc/event. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
So I find several ways to circumvent this cross-domain ajax call problem:
1) starting chrome chrome.exe --disable-web-security
=> no difference
2) configuring apache using mod_proxy to redirect the traffic.
I enabled in the httpd.conf:
proxy_module
proxy_connect_module
proxy_http_module
I put a .htaccess
file in the www root with the following content:
# start mod_rewrite
RewriteEngine On
ProxyRequests off
<Proxy>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /EMBackend/ http://localhost:8080/backend/mvc/
ProxyPassReverse /EMBackend/ http://localhost:8080/backend/mvc/
RewriteRule ^/EMBackend/(.*)$ /backend/mvc/$1 [R]
I restarted all services (apache,php,..)
resulting in error 500
apache error log: [Tue Oct 18 14:30:11 2011] [alert] [client 127.0.0.1] C:/wamp/www/.htaccess: ProxyRequests not allowed here
Any clues on how to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个可行的解决方案:
启用:
将其放入配置的主要部分(或所需的虚拟主机,如果使用 Apache 虚拟主机):
所以我想我不能将其放入
.htaccess
或我必须将 ProxyPreserveHost 设置为 On。我将Include conf/extra/
放入httpd.conf
文件中,并创建了httpd-proxy.conf
文件,并将上面的脚本放入其中。重启apache就可以了!I found a working solution:
Enable:
Put this in the main section of your configuration (or desired virtual host, if using Apache virtual hosts):
So I guess I can't put it in
.htaccess
or I had to setProxyPreserveHost On
. I putInclude conf/extra/
in thehttpd.conf
file and created thehttpd-proxy.conf
file and put the script above in it. Restarted apache and it's working!启用代理模块后,您只需在 httpd.conf 中添加给定的行即可。
只需重新启动服务器即可完成。
You could simply add the given lines in the httpd.conf after enabling the proxy modules.
Just restart the server and you are done.
在非常现代的 apache 中,通过以下方式打开代理:
In very modern apache, turn on proxy by: