Skip to content

Reverse proxy support for custom server base path#113

Open
tilusnet wants to merge 2 commits into
mreichhoff:mainfrom
tilusnet:server-base-path
Open

Reverse proxy support for custom server base path#113
tilusnet wants to merge 2 commits into
mreichhoff:mainfrom
tilusnet:server-base-path

Conversation

@tilusnet

Copy link
Copy Markdown
Contributor

My self hosted apps are typically under a https://mydomain.com/app path.
The web app should have a way of handling requests under a base path which is not necessarily root.

This is my first attempt.

I believe some more code tweaking is needed since not all requests seem to go through the python server's handler.

Inputs and suggestions are highly appreciated as I'd like to be able to host a variant of this app with configurability on reverse proxies.

Thanks!

Comment thread scripts/server_with_rewrites.py Outdated
Comment thread scripts/server_with_rewrites.py
@mreichhoff

Copy link
Copy Markdown
Owner

for this one:

not all requests seem to go through the python server's handler

I think there are a few things to look at:

  • the frontend is explicit in its setting of the path via history.pushState. The code is: const newUrl = `/${activeGraph.prefix}/${words}`;, so we might need a global base_path variable on the frontend as well, so that we could set newUrl to ${basePath}/${activeGraph.prefix}/${words}. I could help with this if needed, and then presumably it could be overridden in the same way we'd override USE_FIREBASE (let me know if that doesn't make sense and I can dig into it a bit more)
    • related: back when I hosted HanziGraph on github pages, I recall I changed most of the paths in the JS to be ./ instead of /, since the paths would've been mreichhoff.github.io/HanziGraph/whatever.
    • The service worker might also have to change to include the base path, as it also has a list of files to cache. Same with data-load.js.
  • history.pushState changes the path in the browser's URL bar, but it doesn't trigger a separate request to the server, so perhaps that's part of the discrepancy you're noticing? Or feel free to let me know which files you weren't seeing requested for help debugging!

@tilusnet

Copy link
Copy Markdown
Contributor Author

Yes I only remember that after "cold booting" the server, subsequent (and repeated) requests were no longer hitting the server as often, likely due to browser caching?
I can fire up that debug server again and give you some examples if it helps.

@tilusnet

Copy link
Copy Markdown
Contributor Author

So it seems there is a bit of work needed on the core code to fully support base_path am I right?
Thanks for considering looking into this.

For the time being I opened an extra port for this app on my server - long term however I'd like it more secure behind a reverse proxy (+certificates), the base path will become very handy especially with Docker in the mix.

@mreichhoff

Copy link
Copy Markdown
Owner

So it seems there is a bit of work needed on the core code to fully support base_path am I right?

yes, that's correct, though I have this possibly silly idea that we could just run some string replacements with sed in your docker setup. I would just need a common pattern for the paths in the JS files

@tilusnet

Copy link
Copy Markdown
Contributor Author

Cool, quick wins are good.

@tilusnet

Copy link
Copy Markdown
Contributor Author

Hey hey.

I'm realising there's one more reason to support reverse proxy: HanziGraph doesn't allow saving the dictionary offline and enabling the tool offline, unless the connection is over https (unless I'm wrong?). The reverse proxy would enable the certificates for https.

@mreichhoff

Copy link
Copy Markdown
Owner

nah, you're right: service workers are https-only, and I lean on those for offline support.

Did you want to try writing in some sed in the dockerfile in this PR, or you want me to give it a shot?

@tilusnet

Copy link
Copy Markdown
Contributor Author

If you can please give it a shot, as I wouldn't know what I am doing :)

@tilusnet

tilusnet commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

I have a working implementation of this now @mreichhoff .
See changes here: tilusnet/HanziGraph@main...tilusnet:HanziGraph:base-path

I pulled it off with some AI assistance, but take a look and let me know if you want it here as a PR.
I will then rebase it on your latest code.

@mreichhoff

Copy link
Copy Markdown
Owner

it makes sense, though I had hoped to keep the base path stuff out of the public/js folder. I wonder if I can think of a way to make sed in the dockerfile work on the paths without too much trouble

@tilusnet

tilusnet commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

sure.
if it helps in finding a better practical solution, base path (re)configuration is typically handled by the reverse proxy itself (my case is Traefik) - the docker image stack would not [have to] change if/when I remap the hosting under a different path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants