schlagmichdoch / PairDrop

PairDrop: Transfer Files Cross-Platform. No Setup, No Signup.
https://pairdrop.net
GNU General Public License v3.0
4.4k stars 245 forks source link

[Bug] self hosting under a sub path not working #307

Closed terrytw closed 3 months ago

terrytw commented 3 months ago

What problem is solved by the new feature When self hosting, I want to use mydomain.example.com/pairdrop instead of pairdrop.example.com. It is easier to manage in terms of DNS and domain name.

Describe the feature A lot of self hosted project allow to set a "baseurl" variable to achieve this, otherwise reverse proxy is going to break.

schlagmichdoch commented 3 months ago

As of my knowledge all paths are already relative as to make serving under a subdirectory possible. Is this not the case? At what point does it break for you? Does PairDrop not load at all? Does your browser log show any error?

terrytw commented 3 months ago

My apologies, since you are so certain, I went back to double check all my settings, then I found that I was missing a / at the end of url.

I was using https://mydomain.com/pairdrop instead of https://mydomain.com/pairdrop/, so the website is not loading assets correctly.

I have also added a rule in my reverse proxy to redirect https://mydomain.com/pairdrop to https://mydomain.com/pairdrop/. Now everything works flawlessly.

schlagmichdoch commented 3 months ago

No worries! Great that you got it working!

It is not ideal though, if it does not work out of the box. We could detect via JS if PairDrop is hosted under a path different than / and change the asset paths accordingly. I’ll reopen this and try to find out if it’s easily doable :)

schlagmichdoch commented 3 months ago

So I had another look at hosting PairDrop under a path and I think your solution is the best way to go.

If we use a subpath we actually point to a subdirectory. When not serving in a subdirectory but on root, we do point to a directory as well (/). Reverse proxies like Nginx or Apache then automatically serve files like index.html inside of the specified directory. So I think pointing to the directory like so https://mydomain.com/pairdrop/ is correct.

Otherwise all relative paths would be broken. /pairdrop: images/logo.png is converted as /images/logo.png

*: Actually I found out that /index.html perfectly served the PairDrop instance as well as /. As I only wanted one URL https://pairdrop.net to be valid, I tried to redirect /index.html to /. At first, I ended up in a redirection loop as / is equivalent to /index.html for nginx which redirects to / again and again until the browser throws an error. I solved it in the end by adding the following bit to the nginx conf:

server  {
    (...)
    if ( $request_uri ~ "/index.html" ) {
        rewrite ^ / permanent;
    }
    (...)
}

So anyone who serves PairDrop under a sub path should simply serve it the subdirectory like so: /pairdrop/