[MapProxy] mapproxy behind a reverse proxy and use of X-Script-Name

Anne Blankert anne.blankert at geodan.nl
Fri Apr 5 09:04:07 PDT 2013

Hello List,

I've created support for a new reverse proxy header "X-Forwarded-Path" 
(source code changes described below).

As far as I can see, this does not break current functionality, but adds 
the possibility to fully reverse proxy MapProxy services like wms, wmts, 
tms, tiles behind a reverse proxy.

Example reverse proxy settings for Apache config:
<Location /wms>
      ProxyPass http://internalserver/mapproxy/multi_wsgi_app_config/service
      RequestHeader add X-Forwarded-Path /wms

<Location /wmts/ >
    ProxyPass http://internalserver/mapproxy/multi_wsgi_app_config/wmts/
    RequestHeader add X-Forwarded-Path /wmts/

<Location /wmts >
    ProxyPass http://internalserver/mapproxy/multi_wsgi_app_config/wmts
    RequestHeader add X-Forwarded-Path /wmts

  <Location /some_path/tms>
         ProxyPass http://internalserver/mapproxy/multi_wsgi_app_config/tms
         RequestHeader add X-Forwarded-Path /some_path/tms

  <Location /some_other_path/tiles>
         RequestHeader add X-Forwarded-Path /some_other_path/tiles

To make this work, the following source changes were applied to MapProxy 
version 1.5:

4 files, base directory: /usr/local/lib/python2.7/dist-packages/mapproxy/

file 1: request/base.py, add new method to class Request(object)

     def resource_url(self):
         forwarded_path = self.environ.get('HTTP_X_FORWARDED_PATH')
         if forwarded_path:
             path_info = self.environ.get('PATH_INFO')
             pos = path_info.rfind('tms')
             if pos >= 0:
                path_info = path_info[pos+3:]
                path_info = ''
             return (self.host_url.rstrip('/')
                 + urllib.quote(forwarded_path).rstrip('/') + path_info)
             return self.base_url

file 2: service/wmts.py, replace line "md['url'] = tile_request.url" by:
  md['url'] = tile_request.http.resource_url

file 3: service/wms.py, replace line "md['url'] = map_request.url" by:
  md['url'] = map_request.http.resource_url

file 4: service/tile.py, replace line "md['url'] = 
map_request.http.base_url" by:
  md['url'] = map_request.http.resource_url + path_info



On 22-3-2013 13:18, Oliver Tonnhofer wrote:
> On 21.03.2013, at 23:06, Anne Blankert wrote:
>> I had hoped that MapProxy would replace the script_name and path_info part of the URL ("/map/service") by the X-Script-Name ("/mywmsservice"). However, the X-Script-Name is not used as a replacement but as a prefix to /map/service (result: "/mywmsservice/map/service")
> MapProxy fully supports the X-Script-Name header and it will adapt the service URLs in the capabilities accordingly. The only problem is that script name refers to MapProxy itself. All /service, /demo, /tms, etc URLs are internals of the MapProxy service, these are not scripts.
>> Is there an easy way to prevent MapProxy from appending "/map/service" to the X-Script-Name in the getCapabilities output?
> No, it's not configurable, it will always be /service.
> Regards,
> Oliver

More information about the MapProxy mailing list