gbv / jskos-server

Web service to access JSKOS data
https://coli-conc.gbv.de/api/
MIT License
6 stars 4 forks source link

Redirect non-slash base-URL to base URL #86

Closed nichtich closed 4 years ago

nichtich commented 4 years ago

If the server instance is made available at http://example.org/api with baseUrl http://example.org/api/, the start page at http://example.org/api will not show correct links (e.g. http://example.org/voc) instead of http://example.org/api/voc. This can be fixed by correct configuration of the proxy which JSKOS server runs behind, but it would help if JSKOS server can detect access to http://example.org/api and redirect to http://example.org/api/ for this case.

stefandesu commented 4 years ago

I configured a local Apache reverse proxy on my computer and tried everything to get the full path, but it seems to be impossible. If I have the reverse proxy running on http://mycomputer.local:8099/api, no matter how I configure Apache or Express, I can't seem to get the /api part inside of jskos-server. The X-Forwarded-Host header gives me the mycomputer.local:8099 part, but both req.url and req.originalUrl only contain /. The Location header is empty. It might be possible to configure the proxy so that the path is contained in that (or another) header, but if we require a certain proxy configuration, we can just as well say that reverse proxies have to include a trailing slash. Also, we can't force the reverse proxy to add a trailing slash (e.g. if it is configured to remove it).

I guess a recommended proxy config would be something like this (for Apache):

<VirtualHost *:8099>
    Define API_PATH /api
    ServerName mycomputer.local

    RewriteEngine on
    # Remove trailing slash from everything EXCEPT the base URL
    RewriteCond %{REQUEST_URI} !^${API_PATH}/$
    RewriteRule ^(.*)/+$ $1 [R=301,L]
    # Force trailing slash for base URL only
    RewriteCond %{REQUEST_URI} ^${API_PATH}$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

    # Forward to jskos-server
    ProxyPass ${API_PATH}/ http://127.0.0.1:3000/
    ProxyPassReverse ${API_PATH}/ http://127.0.0.1:3000/
</VirtualHost>

(I will see that I can configure our reverse proxy so that https://coli-conc.gbv.de/api forwards to https://coli-conc.gbv.de/api/ as well.)

Of course, if anyone has an idea how to get the proper path behind a reverse proxy, I'm all open for it. But I believe that we should rather give recommendations on how to configure a reverse proxy instead of trying to do redirects in jskos-server (possibly even causing a redirect loop for a misconfigured proxy).

nichtich commented 4 years ago

I believe that we should rather give recommendations on how to configure a reverse proxy

ok