Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base path to reverse proxy #313

Closed
fana13 opened this issue Jan 11, 2023 · 24 comments
Closed

Base path to reverse proxy #313

fana13 opened this issue Jan 11, 2023 · 24 comments
Labels
😖 duplicate This issue or pull request already exists

Comments

@fana13
Copy link

fana13 commented Jan 11, 2023

It will be great if we could specify a base path through the docker environment, like this : -e BASE_PATH=/speedtest

@alexdelprete
Copy link
Contributor

If you search the issues, there are many discussions on reverse proxies setup, Traefik etc.

@alexjustesen alexjustesen added the 😖 duplicate This issue or pull request already exists label Jan 11, 2023
@fana13
Copy link
Author

fana13 commented Jan 11, 2023

I've read many discusssions about this, but not really that issue.
It's about subfolder nginx for reverse proxy.
APP_URL in .env it's not for that, right ?

@alexdelprete
Copy link
Contributor

the app is listening on 80 and 443, you should be able to address this with NGINX without requiring modifications.

What exactly are you trying to achieve?

@fana13
Copy link
Author

fana13 commented Jan 11, 2023

I try to have https://myowndomain/speedtracker

@alexdelprete
Copy link
Contributor

With Traefik I can do that using a prefix after the domain. I guess it's doable also with NGINX if that's what you use, you don't need basepath on the app.

@Racle
Copy link

Racle commented Jan 16, 2023

With Traefik I can do that using a prefix after the domain. I guess it's doable also with NGINX if that's what you use, you don't need basepath on the app.

@alexdelprete how you do this in Traefik?

When using with nginx /speedtracker path shows 404 and docker logs show this:
"GET /speedtracker/ HTTP/1.0" 404 6603 "

If I put this to nginx (basically app sees / while reverse proxy/user uses /speedtracker/)
rewrite /speedtracker/(.*) /$1 break;

It will try to redirect to mydomain.com/admin after that, which is also incorrect behaviour as it should redirect to mydomain.com/speedtracker/admin.

With older speedtest-tracker using environment key APP_URL=/speedtracker worked perfectly.

I also tried to use same env config with this one without success. Same thing when trying to use APP_URL=mydomain.com/speedtracker.

@fana13
Copy link
Author

fana13 commented Jan 16, 2023

Same for me, it doesn't work.
For example, with the older henrywhitaker's repo, this nginx configuration work like a charm.
@alexdelprete could you unclose this issue, it seem real

@alexdelprete
Copy link
Contributor

how you do this in Traefik?

https://doc.traefik.io/traefik/middlewares/http/stripprefix/

image

@alexdelprete
Copy link
Contributor

Same for me, it doesn't work.

what exactly doesn't work? Speedtest-Tracker is not a proxy, nor a webserver, it's a webapp.

you can say something doesn't work when its documentation describes a feature, a configuration, and that doesn't work. this is not the case. your expectations about an app is not a bug.

this webapp is listening on http/https ports, no path/subpaths, but on root. it's up to your reverse proxy to be configured like you want it to behave, in order to proxy correctly this webapp on root path. On Traefik StripPrefix can be used, on NGINX I have no idea because I never used it. Check NGINX documentation on how to achieve this and don't blame it on the webapp.

Furthermore, I'm not the maintainer/dev, that would be @alexjustesen and he already closed the case because it's not a bug, it's something the reverse proxy has to manage.

@Racle
Copy link

Racle commented Jan 17, 2023

how you do this in Traefik?

https://doc.traefik.io/traefik/middlewares/http/stripprefix/

@alexdelprete This doesn't work, same redirect problem with traefik when using
image

It tries to redirect to mydomain/admin when it should be redirecting mydomain/speedtest-tracker/admin

Base url has to be programmed into application, middleware/reverse proxy cannot fix this as far as I know.

If you have working configuration, feel free to share.

@fana13
Copy link
Author

fana13 commented Jan 17, 2023

Same for me, it doesn't work.

what exactly doesn't work? Speedtest-Tracker is not a proxy, nor a webserver, it's a webapp.

you can say something doesn't work when its documentation describes a feature, a configuration, and that doesn't work. this is not the case. your expectations about an app is not a bug.

this webapp is listening on http/https ports, no path/subpaths, but on root. it's up to your reverse proxy to be configured like you want it to behave, in order to proxy correctly this webapp on root path. On Traefik StripPrefix can be used, on NGINX I have no idea because I never used it. Check NGINX documentation on how to achieve this and don't blame it on the webapp.

Furthermore, I'm not the maintainer/dev, that would be @alexjustesen and he already closed the case because it's not a bug, it's something the reverse proxy has to manage.

I don't tag it as a bug, tag was FEATURE and i think it's a real feature.
So if you can't have any idea because you never use it, why did you close this ?
I have checked NGINX documentation, i have a lot of app in my nginx configuration.
I gave you an example with the older repo, which have added a APP_PATH or PATH_URL in the .env, that's why i asked if this feature will be implemented, nothing more

@alexdelprete
Copy link
Contributor

I don't tag it as a bug, tag was FEATURE and i think it's a real feature.

Sorry, I tought you opened it as an issue, my bad.

So if you can't have any idea because you never use it, why did you close this ?

I'm not the maintainer, you are confusing names.

I gave you an example with the older repo

This is not the old project, it's a new one. You can achieve what you want if you configure properly your reverse proxy, if your reverse proxy doesn't allow to do it, change proxy.

@alexdelprete
Copy link
Contributor

alexdelprete commented Jan 17, 2023

Base url has to be programmed into application, middleware/reverse proxy cannot fix this as far as I know.

Quote from traefik docs:

Use a StripPrefix middleware if your backend listens on the root path (/) but should be exposed on a specific prefix.

That is exactly what you're trying to achieve, that's the use case.

Feel free to look for working configs in Traefik community forums, there are a lot of threads, here's one: https://community.traefik.io/t/traefik-v2-6-pathprefix-wont-work/13398/2

Also:

I don't use this config, I use speedtest.mydomain.dom.

@Racle
Copy link

Racle commented Jan 17, 2023

Use a StripPrefix middleware if your backend listens on the root path (/) but should be exposed on a specific prefix.

@alexdelprete and that's the issue, this applies to backend. I see how you would think this would work in situation like this when it doesn't

If I had an API which calls mydomain/api, I could redirect that with mydomain/speedtracker/api and it would work just fine as backend usually doesn't try to redirect user to another address.

When connecting to speedtest-tracker frontend tries to redirect to /admin path and you cannot change how that works with middleware, you have to change that in code.

It also hardcodes assets path to /, so even when I could load page without redirecting, assets aren't loading correctly.

And even on your stackoverflow link it's stated that it will only work if your application works like that:

This will only work as long as the app you are trying to start does NOT set a custom html to something hardcoded like root /.

Traefik isn't the solution here. Code changes are required to set correct base path.

@fana13
Copy link
Author

fana13 commented Jan 18, 2023

@alexdelprete i'm sorry, i misconfusing yours nicks
@alexjustesen could you unclose this feature demand ?
Thanks both for your time

@alexdelprete
Copy link
Contributor

and that's the issue, this applies to backend. I see how you would think this would work in situation like this when it doesn't

It does...and that's the only way to make it work with Laravel, Laravel still doesn't officially support serving an app through subdirs.

image

Luckily they merged this PR, which leverages X-Forwarded-Prefix. And Traefik's StripPrefix also creates that header exactly for these use-cases.

When connecting to speedtest-tracker frontend tries to redirect to /admin path and you cannot change how that works with middleware, you have to change that in code.

Sure you can, because Traefik sets X-Forwarded-Prefix header, and the backend (Laravel in this specific case) should use that to build the relative URLs, prepending the Prefix:

image

Now I found out that the configuration of Laravel in Speedtest-Tracker didn't include the support of X-Forwarded-Prefix, so I'll try to include it and do some tests.

@alexdelprete
Copy link
Contributor

i'm sorry, i misconfusing yours nicks

our names...these are our real names, not nicknames. :)

@alexjustesen could you unclose this feature demand ?
Thanks both for your time

check my reply above to Racle, with some technical details.

@alexjustesen
Copy link
Owner

@alexdelprete the bulk of the questions you seem to target are proxy related, espcially when dealing with Traefix. Would you be up for writing a section in the docs in extreme detail on how to setup reverse proxies and then how they can be used with Speedtest Tracker? Thinking we can eliminate a lot of these questions and issues if we get some good docs written.

If you're interested we can connect on Telegram or Discord.

@alexdelprete
Copy link
Contributor

alexdelprete commented Jan 19, 2023

Would you be up for writing a section in the docs in extreme detail on how to setup reverse proxies and then how they can be used with Speedtest Tracker? Thinking we can eliminate a lot of these questions and issues if we get some good docs written.

Sure, I'd love to contribute, but first we need to address some issues about ST, in order to find the proper configuration of the project. I'm not a real expert but I love to share what I learned on these things while setting up my homelab, and I'm using Traefik.

Let's connect on Discord: @alexdelprete#5566.

@vitormfgoncalves
Copy link

Base url has to be programmed into application, middleware/reverse proxy cannot fix this as far as I know.

Quote from traefik docs:

Use a StripPrefix middleware if your backend listens on the root path (/) but should be exposed on a specific prefix.

That is exactly what you're trying to achieve, that's the use case.

Feel free to look for working configs in Traefik community forums, there are a lot of threads, here's one: https://community.traefik.io/t/traefik-v2-6-pathprefix-wont-work/13398/2

Also:

I don't use this config, I use speedtest.mydomain.dom.

I don't understand why you refuse to accept a valid argument and a true problem.
Not everyone uses traefik, and reverse proxies are not magic.
Most of the web runs on Javascript (webapps included). When you reverse-proxy a webapp, the JS still contains the incorrect paths. Even if you rewrite the server's response (aka the javascript), which I think you are referring to some posts above, it's almost certain that you will run into trouble.
Finally, not everyone can use a subdomain. I can't (at home).

@alexdelprete
Copy link
Contributor

alexdelprete commented Jan 22, 2023

I don't understand why you refuse to accept a valid argument and a true problem.

Because you don't read carefully, or you didn't understand what you read, so I'll be even more clear:

  1. Laravel does NOT support subfolders. Full stop. They merged only recently a PR that allows to circumvent the issue, and that's what I'm thinking to follow in order to make it work.
  2. In order to make it work, Laravel (Symfony actually) leverages specific headers to build the path of resources, so when you say "the JS still contains the incorrect path" it's clear you don't understand what we're talking about. I'm not rewriting the server's response, it's Laravel through headers building paths dynamically. And this works not only for Traefik but with all reverse proxies that support X-Forwarded-Prefix. NGINX supports it, and I bet others too.
  3. Finally, I didn't say USE SUBDOMAINS, I said: "I use subdomains", I hope you understand the difference. Furthermore, Laravel devs official reply is: "use subdomains, subfolders ARE NOT SUPPORTED".

image

Got it now? Hope so.

Regarding this other thing:

Most of the web runs on Javascript (webapps included). When you reverse-proxy a webapp, the JS still contains the incorrect paths. Even if you rewrite the server's response (aka the javascript), which I think you are referring to some posts above, it's almost certain that you will run into trouble.

This is completely incorrect, so much so, that I won't even bother to comment, because it's clear you don't have a clear idea on how a webapp works. Let alone we're talking specifically of Laravel, so talking generically about javascript is totally nonsense.

Finally, not everyone can use a subdomain. I can't (at home).

At home, in my homelab, I use subdomains, why can't you? You should replace "can" with "don't want to do it" or "don't know how to do it". "Can't" is nonsense.

@jscmidt
Copy link

jscmidt commented Apr 12, 2023

Now I found out that the configuration of Laravel in Speedtest-Tracker didn't include the support of X-Forwarded-Prefix, so I'll try to include it and do some tests.

Hi,
Any news on this? I would also appreciate if ST could be hosted under a subpath.

@alexdelprete
Copy link
Contributor

alexdelprete commented Apr 12, 2023

Any news on this?

X-Forwarded-Prefix has been implemented in January: PR #342

@alpe12
Copy link

alpe12 commented Apr 22, 2023

Any news on this?

X-Forwarded-Prefix has been implemented in January: PR #342

@alexdelprete @alexjustesen
For me it still doesn't work correctly.
Most urls works. But all urls under /livewire and /img remains at / instead of the configured prefix (/speedtest-tracker/).

image

Nginx.

location ^~ /speedtest-tracker/ {
    proxy_pass http://speedtest-tracker:80/;
    proxy_set_header Host "localhost";
    proxy_set_header X-Forwarded-Prefix /speedtest-tracker;
    proxy_set_header X-Forwarded-Proto $scheme;
    access_log off;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
😖 duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

7 participants