Javascript apache mod_proxy,为跨域ajax调用配置ProxyPass & ProxyPassReverse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7807600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
apache mod_proxy, configuring ProxyPass & ProxyPassReverse for cross-domain ajax calls
提问by VDP
I'm creating an html5 - JavaScript app (for mobile devices, using PhoneGap). I have to interact with a REST service.
我正在创建一个 html5 - JavaScript 应用程序(用于移动设备,使用 PhoneGap)。我必须与 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.
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:
我.htaccess
在 www 根目录中放了一个文件,内容如下:
# 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/ [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
阿帕奇错误日志: [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?
有关如何解决此问题的任何线索?
回答by VDP
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
. I put Include conf/extra/
in the httpd.conf
file and created the httpd-proxy.conf
file and put the script above in it. Restarted apache and it's working!
所以我想我不能把它放进去,.htaccess
或者我必须设置ProxyPreserveHost On
. 我把Include conf/extra/
中httpd.conf
文件和创建的httpd-proxy.conf
文件,并把上面的脚本在里面。重新启动apache,它正在工作!
回答by Mohammad Arif
You could simply add the given lines in the httpd.confafter 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.
只需重新启动服务器即可完成。
回答by Kevin
In very modern apache, turn on proxy by:
在非常现代的 apache 中,通过以下方式打开代理:
a2enmod proxy;
a2enmod proxy_http