I am using the OpenLayers/GeoServer stack and my WMS requests are configured to go via a proxy using the following:


So far so good, all future WMS request are now channelled through this proxy script and WMS images are generated/returned as expected.

My problem arises when viewing the legend, the request to get the layer image (WMS request) ignores the OpenLayers proxy definition and tries connecting direct (client browser->geoserver server which is not externally accessible - hence the proxy).

How can I force the legend image requests to use the proxy?

  • You are probably mixing up a Proxy and a reverse proxy. In OpenLayers, requests are made via a proxy to avoid the cross domain issues. What you have is a reverse Proxy they are not the same. Apr 2, 2014 at 7:47
  • Thanks for the reply but I think you miss understood my question. Basically the legend WMS image requests are not following the same route as the standard map WMS requests. The standard map WMS requests are honouring the defined proxy whereas the legend WMS requests are not. Nothing to do with a reverse proxy I'm afraid!
    – ScottFree
    Apr 2, 2014 at 8:44
  • 1
    The proxy setting is only used when for XHR requests. The proxy will not be used for Image requests like the tiles & images. This is your issue. Apr 2, 2014 at 8:46
  • Ah yes you're right, I thought it was the other way round for some reason (WMS->proxy, XHR->direct). I've used this piece of code to send WMS requests through the proxy, guess I need to use my proxyWMS when getting the legend images:
    – ScottFree
    Apr 2, 2014 at 9:16
  • Tried adding the code but the formatting is horrendous, I'll include it anyway in case someone else can make use of it in the future: //override the default wms object and send all traffic through the proxy var proxyWMS = OpenLayers.Class(OpenLayers.Layer.WMS, { getURL: function (bounds) { var url = OpenLayers.Layer.WMS.prototype.getURL.call(this, bounds); if (OpenLayers.ProxyHost && OpenLayers.String.startsWith(url, "http")) { url = OpenLayers.ProxyHost + encodeURIComponent(url); } return url; }});
    – ScottFree
    Apr 2, 2014 at 9:30

1 Answer 1


So I eventually got to the bottom of it, a slight bodge perhaps but to request legend images via the previously defined proxy I had to modify the GeoExt LegendImage.js file and modify the setUrl function (see the if statement at the bottom), specifically the el.dom.src = proxyUrl + encodeURIComponent(url) which set the src of the image to the proxy + already defined legend image url (the proxyUrl variable has been defined globally before we get to this method):

/** api: method[setUrl]
 *  :param url: ``String`` The new URL.
 *  Sets the url of the legend image.
setUrl: function(url) {
    this.url = url;
    var el = this.getEl();
    if (el) {
        el.un("error", this.onImageLoadError, this);
        el.on("error", this.onImageLoadError, this, {single: true});
        if (proxyUrl) {
            el.dom.src = proxyUrl + encodeURIComponent(url);
        } else {
            el.dom.src = url;
  • 1
    Probably a stupid question, but when setting up the WMS layer, why don't you use the URL with proxy URL? Apr 3, 2014 at 14:01

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.