8

When exactly do I need to use a proxy with openlayers? I've done a lot of reading on this but can't seem to find a definitive answer. Wondering if this is the route of my problems.

For instance, if I have one server running Geoserver (WFS) on port 8080 and Apache (serving OpenLayers) on port 80 would I need to use a proxy?

If the servers are physically separate would I need to use proxy?

3 Answers 3

8

You need a proxy if you are making an AJAX request to a machine and/or port that is different from the one that your webpage was served from.

So in both your examples above you will need a proxy (on the server that is serving the webpage) if you want to make WMS getfeatureinfo requests or any sort of WFS request. However you do not need a proxy for simple WMS getMap requests.

1
  • Thanks - that makes sense. I'll have a fiddle with the proxy setup!
    – user8262
    Commented Jun 19, 2012 at 16:10
3

pls read here.

Why do I need a ProxyHost?

Due to security restrictions in Javascript, it is not possible to retrieve information from remote domains via an XMLHttpRequest.

Classes like WFS and GeoRSS use XMLHTTPRequest to get their data. If they are querying a remote server (anything other than the machine hosting your page), you must install a proxy script somewhere web accessible on that machine. See below for how to set up your own ProxyHost.

If the OpenLayers.ProxyHost variable is not set to a valid proxy host, requests are sent directly to the remote servers. In most cases, the result will be a security exception, although this exception often occurs silently.

i hope it helps you...

2

When JavaScript was first developed, it was not imagined that it would have such wide use, and hence many assumptions were made during its development, which have become problematic today.

When Microsoft designed the XMLHttpRequest in their Internet Explorer Browser, it was designed with a big limitation.

The XMLHttpRequest does not allow you to directly make a request to another server. Even two servers running on the same machine, but different ports have this limitation.

What you need to do, is to have a cgi proxy on your apache webserver (port 80), and use the OpenLayers.ProxyHost to point to it.

The OpenLayers site has a FAQ Answer for this issue :

Why do I need a ProxyHost?

Due to security restrictions in Javascript, it is not possible to retrieve information from remote domains via an XMLHttpRequest.

Classes like WFS and GeoRSS use XMLHTTPRequest to get their data. If they are querying a remote server (anything other than the machine hosting your page), you must install a proxy script somewhere web accessible on that machine. See below for how to set up your own ProxyHost.

If the OpenLayers.ProxyHost variable is not set to a valid proxy host, requests are sent directly to the remote servers. In most cases, the result will be a security exception, although this exception often occurs silently. How do I set up a ProxyHost?

An example proxy host script is available here: trunk/openlayers/examples/proxy.cgi

For the standard Apache configuration, you would place proxy.cgi into your /usr/lib/cgi-bin/ directory.

Once a proxy host script has been installed, you must then edit the OpenLayers.ProxyHost variable to match that URL.

Given the above standard Apache configuration:

OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";

If you have done something like this, you should be able to visit:

 http://YourDomain.example.com/cgi-bin/proxy.cgi

The resulting content at that page should be the openlayers.org website.

If you get a 404 error instead, either the proxy script is not in the right location, or your webserver is not configured correctly.

After you have copied over the proxy file to your server, don't forget to edit the allowedHosts array.


Though the existing answers are quite old, I feel that there needs to be an exhaustive answer, since this question has become a canonical question, and many others are closed as its duplicate. This is my attempt to answer this question, based on other answers I have written on this site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.