[英]apache mod_proxy, configuring ProxyPass & ProxyPassReverse for cross-domain ajax calls
I'm creating an html5 - JavaScript app (for mobile devices, using PhoneGap). 我正在创建一个html5 - JavaScript应用程序(适用于移动设备,使用PhoneGap)。 I have to interact with a REST service.
我必须与REST服务进行交互。
The service is now running on "http://localhost:8080/backend/mvc/"
该服务现在在
"http://localhost:8080/backend/mvc/"
I'm developing my app on an wamp server (apache2) ( http://localhost/stage/
) I'm using Chrome for browser. 我正在wamp服务器上开发我的应用程序(apache2)(
http://localhost/stage/
)我正在使用Chrome浏览器。
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.
当执行ajax调用时,浏览器会响应:
XMLHttpRequest cannot load http://localhost:8080/backend/mvc/event. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
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: 所以我找到了几种方法来规避这种跨域的ajax调用问题:
1) starting chrome chrome.exe --disable-web-security
=> no difference 1)启动chrome
chrome.exe --disable-web-security
=>没有区别
2) configuring apache using mod_proxy to redirect the traffic. 2)使用mod_proxy配置apache以重定向流量。
I enabled in the httpd.conf: 我在httpd.conf中启用了:
proxy_module
proxy_connect_module
proxy_http_module
I put a .htaccess
file in the www root with the following content: 我在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]
I restarted all services (apache,php,..) 我重新启动了所有服务(apache,php,..)
resulting in error 500 导致错误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
apache错误日志:
[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? 关于如何解决这个问题的任何线索?
I found a working solution: 我找到了一个有效的解决方
Enable: 启用:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Put this in the main section of your configuration (or desired virtual host, if using Apache virtual hosts): 将其放在配置的主要部分(如果使用Apache虚拟主机,则为所需的虚拟主机):
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /EMBackend http://localhost:8080/backend/mvc
ProxyPassReverse /EMBackend http://localhost:8080/backend/mvc
<Location /EMBackend>
Order allow,deny
Allow from all
</Location>
So I guess I can't put it in .htaccess
or I had to set ProxyPreserveHost On
. 所以我想我不能把它放在
.htaccess
或者我必须设置ProxyPreserveHost On
。 I put Include conf/extra/
in the httpd.conf
file and created the httpd-proxy.conf
file and put the script above in it. 我在
httpd.conf
文件中Include conf/extra/
并创建了httpd-proxy.conf
文件并将上面的脚本放入其中。 Restarted apache and it's working! 重启apache,它正在运行!
You could simply add the given lines in the httpd.conf after enabling the proxy modules. 在启用代理模块后,您只需在httpd.conf中添加给定的行。
ProxyPreserveHost On
ProxyPass /EMBackend http://localhost:8080/backend/mvc
ProxyPassReverse /EMBackend http://localhost:8080/backend/mvc
Just restart the server and you are done. 只需重启服务器即可完成。
In very modern apache, turn on proxy by: 在非常现代的apache中,打开代理:
a2enmod proxy;
a2enmod proxy_http
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.