[GIS] OpenLayers doesn’t proxy WMS requests to Geoserver

geoserveropenlayers-2PROXYrequestwms

I had an issue with WMS requests from OpenLayers to Geoserver via Proxy.php.
My OL version is 2.12 and my Geoserver is 2.4.0.
I set the variable OpenLayers.ProxyHost as shown in OL documentation, but the requests go directly to the remote IP not passing through Proxy.php.
I read on forums that OpenLayers WMS requests have this problem, and suggest to use this getURL class:

var proxyHost=OpenLayers.ProxyHost="proxy.php?url=";
var myWMS = 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;
    }
});

Are you aware of this? is it a bug of OpenLayers?
Does OL need a fix?

Best Answer

This is the expected behavior of OpenLayers.ProxyHost. ProxyHost is there to get around the same-origin policy that prevent browser to do AJAX request to URL different from the application URL. If you look at the WFS OpenLayers example, you will notice that the WFS request go through the Proxy, but the WMS request don't. WFS queries are subject to this browser limitation and WMS request are not. This is why you have to this getURL function if you want to have your WMS request go throught the same proxy:

var myWMS = 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;
  }
});

The next question is, do you really need the WMS request go through this proxy? If yes, you could also use it as your WMS URL directly.