Support for proxy server that reverse proxies a subdirectory

Hi! I’m just starting to work with photostructure. It’s running successfully on localhost. Now I’m trying to reverse proxy from a path within my virtualserver host, like so:

https://www.example.com/photos/ <-> http://localhost:1787/

I’m able to fetch the proxied web pages, but the assets linked into them are expressed in absolute terms, for example:

<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">

This, of course, breaks across the proxy. I can’t figure out how to get them returned either as relative or rebased URL’s:

<--relative: -->
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png"> 
<--rebased: -->
<link rel="icon" type="image/png" sizes="32x32" href="/photos/favicon-32x32.png">

Does such a configuration setting exist? Is it an approriate feature request to create it?

My setup

OS: Fedora 35
PhotoStructure: v1.1.0
Apache: 2.4.54

My Apache proxy configuration:

ProxyPass "/photos/" "http://localhost:1787/"
ProxyPassReverse "/photos/" "http://localhost:1787/"
ProxyPassReverseCookieDomain "localhost" "www.example.com"
ProxyPassReverseCookiePath "/" "/photos/"

I reverse proxy photostructure myself using traefik. I use a different domain for each app I reverse proxy to avoid the sort of problems you’re dealing with. Having said that, I dabbled a bit with what you’re trying to do with traefik, and I know it has the ability to rewrite URLs like this.

I don’t have first hand experience doing reverse proxy or URL rewriting with Apache, but a quick google shows a lot of info on this. search for “apache reverse proxy url rewrite” This might help Using mod_rewrite for Proxying - Apache HTTP Server Version 2.4

Thanks! Using a different domain (or a different port) seems to be the obvious workaround, at the cost of setting up a new virtual host on the proxy server (and perhaps some DNS glue and firewall hole-punching).

A bit more info for the record:

A decent list of strategies for handling this case is described in the accepted answer at serverfault.com/…/how-to-handle-relative-urls-correctly-with-a-reverse-proxy.

Nextcloud, for example, uses strategy 2, allowing admins to specify the web root under which the app lives (ref: nextcloud setting overwritewebroot described here.). Thus, the reverse proxy can simply map www.example.com/photos to https://localhost:8187/photos, and the app will always generate pages containing references with respect to the given webroot (i.e. <img src="/photos/images/foo.jpg"/>)

The mod_proxy_html option (couple answers down from the accepted one) is what I thought would be the Apache way to handle this. But I agree that if @mrm is willing to create yet another config option for this it would cleaner. Good luck! I’d vote for your feature request but I ran out of votes long ago…

Welcome to PhotoStructure, @brad2014 !

The ideal solution would require no configuration, and have things “just work,” but if you need a different webroot, I think that’s going to require config.

An approach could be to add a <base> element whose content was sniffed from the requested URL (but could be overridden in settings to support different sub-path webroots, or more exotic hostname/port reverse proxy setups), and twiddle all generated URLs to not start with a forward slash.

I’d been using absolute path name URLs throughout mostly out of fear of old browser support of <base> and relative paths–but given that <base> was introduced ages ago, it has wide support.

(It’s OK to vote on your own feature requests, btw!)

My 2c: I used to try and use subdomains for each app, so I had www.example.com/appname type locations. I stopped this and now use subdomains for a few reasons:

  1. It required every app to honour a subdirectory config (some like Nextcloud have this out of the box, lots don’t)

  2. Wildcard certs got cheaper, as did auto-certificates for new subdomains

  3. Password managers are easier to configure with subdomains

My vote would be to spend development effort elsewhere.

2 Likes