Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 665
VDP Avatar asked Oct 18 '11 13:10

VDP


People also ask

What is proxypass in Apache mod_proxy?

ProxyPass, ProxyPassReverse, etc… Apache mod_proxy defines a few directives which let it forward traffic to a remote server. They are listed below with a short description. Note: I know the directives introduced in this article could be used in much complicated ways. ProxyPass maps local server URLs to remote servers + URL.

How do I configure Apache HTTP Server as a proxy?

Apache HTTP Server can be configured in both a forward and reverse proxy (also known as gateway) mode. An ordinary forward proxy is an intermediate server that sits between the client and the origin server. In order to get content from the origin server, the client sends a request to the proxy naming the origin server as the target.

What is Apache mod_proxy directive?

Apache mod_proxy defines a few directives which let it forward traffic to a remote server. They are listed below with a short description. Note: I know the directives introduced in this article could be used in much complicated ways. ProxyPass maps local server URLs to remote servers + URL. It applies on traffic from client to server.

Can I use Apache as a reverse proxy?

While Apache might not be your first choice as a reverse proxy, with more modern alternatives like NGINX tending to steal attention, mod_proxy is useful for servers which are already running Apache and now need to route traffic to another service. You can set up an Apache virtual host to pass on requests for a given domain to a separate web server.


2 Answers

You could simply add the given lines in the httpd.conf after enabling the proxy modules.

ProxyPreserveHost On
ProxyPass /EMBackend http://localhost:8080/backend/mvc
ProxyPassReverse /EMBackend http://localhost:8080/backend/mvc

Just restart the server and you are done.

like image 24
Mohammad Arif Avatar answered Oct 24 '22 21:10

Mohammad Arif


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!

like image 95
VDP Avatar answered Oct 24 '22 20:10

VDP