strapi icon indicating copy to clipboard operation
strapi copied to clipboard

fix: server and admin url configuration

Open wladyslawczyzewski opened this issue 1 year ago β€’ 8 comments

Hi. While trying to launch Strapi 5 for my new website in subpath configuration I noticed that isn't working. Turns out that others also have issues with this. I did some investigation and found that configuration is not loaded properly causing the issues when launching Strapi in subpath configuration (e.g. https://domain.com/cms).

What does it do?

  1. update the logic to load the correct admin.path based on admin.url (admin.path is required in build context to set the ctx.basePath which is used to put the resources for admin panel)
  2. fix the prefix value for admin panel's APIs
  3. fixed configuration loading, as config object is mutable, so using it for setting server.absoluteUrl and admin.absoluteUrl should be done before mutating the config object in loadConfiguration function

Why is it needed?

Strapi 5 when running in subpath of the main website domain is not working.

How to test it?

Tested on local machine by checking the admin panel load correctly with different pair of configuration for server.url and admin.url:

server.url admin.url working?
http://localhost:1337 undefined βœ…
http://localhost:1337 http://localhost:1337/admin βœ…
http://localhost:1337 /admin βœ…
http://localhost:1337 /dashboard βœ…
http://localhost:1337 http://localhost:1337/dashboard βœ…
http://localhost:1337 http://localhost:1337/cms/dashboard ❌ (to fix this case I think we need to introduce new config key admin.path)
http://localhost:1337/cms undefined βœ…
http://localhost:1337/cms http://localhost:1337/cms/admin βœ…
http://localhost:1337/cms /admin βœ…
http://localhost:1337/cms /dashboard βœ…
http://localhost:1337/cms http://localhost:1337/cms/dashboard βœ…
/cms undefined βœ…
/cms http://localhost:1337/cms/admin βœ…
/cms /admin βœ…
/cms /dashboard βœ…
/cms http://localhost:1337/cms/dashboard βœ…
/cms http://localhost:1337/dashboard ❌
undefined undefined βœ…
undefined http://localhost:1337/admin βœ…
undefined /admin βœ…
undefined /dashboard βœ…
undefined http://localhost:1337/dashboard βœ…
undefined http://localhost:1337/cms/dashboard ❌

Related issue(s)/PR(s)

Related issues:

#21452 #21479

NB. For Strapi 5 when it's run in subpath configuration (http://domain.com/cms) if you used reverse proxy like NGINX - you don't need to rewrite the request uri, just pass the request to the Strapi's instance origin/upstream.

wladyslawczyzewski avatar Sep 27 '24 22:09 wladyslawczyzewski

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
contributor-docs βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Oct 17, 2024 0:15am

vercel[bot] avatar Sep 27 '24 22:09 vercel[bot]

CLA assistant check
All committers have signed the CLA.

strapi-cla avatar Sep 27 '24 22:09 strapi-cla

Updated the test cases in the PR description. Looks like at this moment there's no chance to run the Strapi admin panel in different subpath not related to the server.url.

wladyslawczyzewski avatar Sep 28 '24 09:09 wladyslawczyzewski

Hi @wladyslawczyzewski thanks for your contribution - we'll take a look very soon πŸ™

hanpaine avatar Oct 01 '24 15:10 hanpaine

Thanks for the PR and even moreso for the detailed information! I'll check this out and hopefully we will get it merged soon.

In the meantime, could you fix the reported linting errors?

  /home/runner/work/strapi/strapi/packages/core/core/src/configuration/urls.ts
    2:10  error  'strings' is defined but never used  @typescript-eslint/no-unused-vars

  /home/runner/work/strapi/strapi/packages/core/core/src/services/server/admin-api.ts
    10:66  error  Unnecessary escape character: \/  no-useless-escape

innerdvations avatar Oct 15 '24 07:10 innerdvations

hi @innerdvations , thanks for your review – I've fixed the lint errors, could you please check one more time?

wladyslawczyzewski avatar Oct 17 '24 12:10 wladyslawczyzewski

@innerdvations looks like this could be an issue on V4 too - can you check if so, and dupe to v4 please?

hanpaine avatar Oct 18 '24 12:10 hanpaine

@innerdvations looks like this could be an issue on V4 too - can you check if so, and dupe to v4 please?

I think @alexandrebodin and @Convly are sorting out the discrepencies between the two, but if not then I'll do it

innerdvations avatar Oct 18 '24 13:10 innerdvations

The fix works fine on production for me, but I noticed that the media library assets links are broken in the admin panel context. Not sure why it does that, for now I workaround this with a nginx rewrite rule (which is weird since I don't need rewrite rules for the admin panel itself).

raphaelbadawi avatar Oct 27 '24 11:10 raphaelbadawi

The fix works fine on production for me, but I noticed that the media library assets links are broken in the admin panel context. Not sure why it does that, for now I workaround this with a nginx rewrite rule (which is weird since I don't need rewrite rules for the admin panel itself).

That's probably the security middleware csp settings, you'll need to set those.

derrickmehaffy avatar Oct 28 '24 16:10 derrickmehaffy

Hello, colleagues. Sorry for bother you. Could someone kindly give updates related to this bug fix, any plans to merge it? As far as I know it almost ready for merging.

mad-zephyr avatar Nov 07 '24 20:11 mad-zephyr

Hi all, thanks for your patience on this work - we'll be re-picking it up this week πŸ™

hanpaine avatar Nov 12 '24 11:11 hanpaine

Hello, any updates on when this will be merged?

Matb85 avatar Nov 18 '24 15:11 Matb85

Hello all,

To share some context,

Right now this PR cannot be merged. It does seem to work but it does so by adding internal prefixing, which creates unexpected behaviors. The goal in the short term is to offer a way to make strapi work behind a reverse-proxy like we supported in v4.

To be fully transparent we have a dedicated workshop tomorrow on it πŸ™

alexandrebodin avatar Nov 18 '24 15:11 alexandrebodin

I wonder how that's going.

Moonlight63 avatar Nov 19 '24 06:11 Moonlight63

Hey, we took some time to go over this with @alexandrebodin and test the different scenario + decide where we wanted to go.

1st, you can find below a table you shared in your original PR that we completed (on top of the result for your PR) with the behavior in v4 (pre vite update) + the behavior when using Alex's PR.

server.url admin.url Community PR v4.16 (❌ <=> requires reverse proxy) PR Alex
http://localhost:1337 undefined βœ… βœ… βœ…
http://localhost:1337 http://localhost:1337/admin βœ… βœ… βœ…
http://localhost:1337 /admin βœ… βœ… βœ…
http://localhost:1337 /dashboard βœ… βœ… βœ…
http://localhost:1337 http://localhost:1337/dashboard βœ… βœ… βœ…
http://localhost:1337 http://localhost:1337/cms/dashboard ❌ βœ… βœ…
http://localhost:1337/cms undefined βœ… ❌ ❌ (vite serves too many things / TBF later)
http://localhost:1337/cms http://localhost:1337/cms/admin βœ… ❌ same as above
http://localhost:1337/cms /admin βœ… ❌ same as above
http://localhost:1337/cms /dashboard βœ… ❌ same as above
http://localhost:1337/cms http://localhost:1337/cms/dashboard βœ… ❌ same as above
/cms undefined βœ… ❌ same as above
/cms http://localhost:1337/cms/admin βœ… ❌ same as above
/cms /admin βœ… ❌ same as above
/cms /dashboard βœ… ❌ same as above
/cms http://localhost:1337/cms/dashboard βœ… ❌ same as above
/cms http://localhost:1337/dashboard ❌ ❌ same as above
undefined undefined βœ… βœ… βœ…
undefined http://localhost:1337/admin βœ… βœ… βœ…
undefined /admin βœ… βœ… βœ…
undefined /dashboard βœ… βœ… βœ…
undefined http://localhost:1337/dashboard βœ… βœ… βœ…
undefined http://localhost:1337/cms/dashboard ❌ βœ… βœ…

As you can see, your PR allows to handle far more possibilities while Alex's make it ISO with what we had in v4.

After discussing it, we came to the conclusion that our goal with setting server URLs is to match reverse proxies configurations rather than automatically modifying how we serve our APIs.

Handling the proxying ourselves could cause breaking change issues for plugin maintainers depending on constant endpoints as well as for users, whereas delegating the server URL redirection to a reverse proxy allows for more flexibility.

To summarize, we will move forward with Alex's PR for now to keep consistency & flexibility with v4, but we're open to discuss a feature request around setting the server URL & handling the paths directly in Strapi.

Below I'm sharing a very simple caddy file that we used for our tests & that create a reverse proxy on localhost:1338/cms -> localhost:1337.

:1338

handle_path /cms/* {
	uri strip_prefix /cms
	reverse_proxy localhost:1337
}

respond "Not Found" 404

log logger {
	output stdout
	format console
}

Convly avatar Nov 19 '24 15:11 Convly

hi @Convly thanks for detailed explanation. I see your point and I totally trust you in such decisions as you're both the maintainers of this project and you know better this project then me (it was actually my first time I ran strapi).

Using reverse proxy is a quite good solution and I think it's good option for the most use cases, however, there're situations where the reverse proxy can't do url rewrites, for instance, AWS' Load Balancers - so there's no option to run the strapi in subpath and subdomain installation is only way to get that work.

Feel free to close this PR as soon as you decide the issue is solved.

wladyslawczyzewski avatar Nov 19 '24 20:11 wladyslawczyzewski

Interesting, I haven't used AWS ALBs in a long time, but isn't it possible to place a reverse proxy in between your ALB & your application server? Otherwise, I believe it's possible to do full redirects for simple use cases

In the meantime, I'll close this PR just so that it's removed from our backlog. Thanks for your contribution, greatly appreciated!

Convly avatar Nov 22 '24 13:11 Convly

@wladyslawczyzewski definitly make a feature request with the explanation of your usecase that won't work with the reverse proxy so the product team can see it πŸ™

alexandrebodin avatar Nov 22 '24 15:11 alexandrebodin

I guess I am confused about how to make a correct configuration for serving the admin panel on a different subdomain from the api server given the double / bug that this PR seems to have partially fixed. I have been trying for a few days to off and on to get my strapi instance to server at api.example.com, and put the admin panel on admin.example.com, but it seems like this is not possible because that requires the admin panel to be pointing to root /, which doesn't work. If I host the admin panel at admin.example.com/admin, then everything is fine of course, but that is not the goal. Are we just saying flat out that this use case or setup is not and will not be supported/possible? I am doing this using a "different servers" config, hosting the built admin SPA in a simple nginx server. Edit: It's even more irritating given that I could just set a full domain name as the url for the admin panel, which would solve the issue entirely, but the fqdn gets parsed out to a relative path anyway so it doesn't work.

Moonlight63 avatar Nov 24 '24 10:11 Moonlight63

I guess I am confused about how to make a correct configuration for serving the admin panel on a different subdomain from the api server given the double / bug that this PR seems to have partially fixed. I have been trying for a few days to off and on to get my strapi instance to server at api.example.com, and put the admin panel on admin.example.com, but it seems like this is not possible because that requires the admin panel to be pointing to root /, which doesn't work. If I host the admin panel at admin.example.com/admin, then everything is fine of course, but that is not the goal. Are we just saying flat out that this use case or setup is not and will not be supported/possible? I am doing this using a "different servers" config, hosting the built admin SPA in a simple nginx server. Edit: It's even more irritating given that I could just set a full domain name as the url for the admin panel, which would solve the issue entirely, but the fqdn gets parsed out to a relative path anyway so it doesn't work.

We are only prioritizing another PR not leaving the issue open. We shared the experimental version in the original issue you can find it here for testing. It should work like the v4 with a reverse proxy configuration. https://github.com/strapi/strapi/issues/21184#issuecomment-2490644833

alexandrebodin avatar Nov 26 '24 09:11 alexandrebodin

For any further discussion please go to https://github.com/strapi/strapi/issues/21184 so we have a single source of truth for the issue πŸ™

alexandrebodin avatar Nov 26 '24 09:11 alexandrebodin