CrunchyData / pg_tileserv

A very thin PostGIS-only tile server in Go. Takes in HTTP tile requests, executes SQL, returns MVT tiles.
Apache License 2.0
858 stars 159 forks source link

Proxy configuration at sub-route #193

Open jtbaker opened 10 months ago

jtbaker commented 10 months ago

Hi, thanks for the great tool!

I have a use case where I'd like to proxy a pg_tileserv deployment at a sub-route of my app, something like /tiles to be able to use its built in authentication functionality. I'm sure I'm not the only person that may use this pattern, and the machine-readable json endpoints are quite helpful.

Using the Forwarded, X-Forwarded-Host and X-Forwarded-Scheme headers works great for proxying to another bare route, updating the detailurl properties to have the right origin. But if I want those values to be mapped to my sub-route, the logic breaks down and I have to do some parsing/reinjection of the properties returned in the .json endpoints to change the route to include my proxied sub-route.

Could I do this using some some sort of HTTP header information that I attach in my proxied requests? It looks like it only respects the Host property and strips anything after that at the moment.

Example header:

Forwarded: host="localhost:8000/tiles/";proto=http

/index.json returns objects like this:

...
detailurl: http://localhost:8000/public.<table_name>.json

instead of

detailurl: http://localhost:8000/tiles/public.<table_name>.json
pramsey commented 10 months ago

Have you played with

# Advertise URLs relative to this server name
# default is to look this up from incoming request headers
# UrlBase = "http://localhost/"

# Optional path to add to the service base URL
# If set, all routes will be prefixed with this path
# BasePath = "/"

https://github.com/CrunchyData/pg_tileserv/blob/master/config/pg_tileserv.toml.example

wendellwt commented 5 months ago

I had the same issue when the server was behind a proxy that rewrites the address. Setting BasePath looked right, but BasePath was used in other places that broke things. UrlBase didn't work either when it ended with a path name (in addition to the fqdn part). The only way I was able to get it to work was to edit util.go near line 44 to force the return of the desired url:

func formatBaseURL(baseHost string, basePath string) string {
        ...
        return "https://myserver.com/tiles"
}

Not pretty, but it worked.