Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

javascript - apache mod_proxy, configuring ProxyPass & ProxyPassReverse for cross-domain ajax calls

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...