I have my Django app and I have following use cases:
- Local development. Since my app is mostly simple, I prefer usual
manage.py runserver
approach. So I expect my app to work for this case. - Production environment. We ahve one web server and host tools under different URLs, for example
https://myserver.com/tool1
,https://myserver.com/tool2
etc. So I'd like to put my app in this structure - (Theoretical) Apart of use case 2, I may want to host the app under its own domain, like
https://tool.myserver.com
But when I tried to do this, I got an issue with static files (sigh!), because if I have STATIC_URL
as relative path STATIC_URL='static/'
then it doesn't work for "nested" pages (i.e. if I'm on myserver.com/tool1/page
then static URL will map to myserver.com/tool1/page/static
which is not correct).
From the other hand, if I use absolute path STATIC_URL='/static/'
then it doesn't work for case 2 at all, because Django app knows nothing about /tool1
part of URL where it's located.
I can use two different variants for STATIC_URL
depending on the environment and hardcode STATIC_URL='/tool1/static/'
, but then the same code will not work for case 3...
How should I handle this situation?
UPD
Actually I realized that it's more like general nginx+backend question than Django one. Because at the end of the day webpage generated by backend most probably will have src="/static/...."
(unless I add some hacks to backend to insert prefix /tool1). And I wonder how is it being handled usually? There is a way to replace actual HTML content in nginx, but it will really affect performance...
UPD2
Seems like a lot of people misread my quesiton thinking that the only issue is in static files. However it was just an example, because as Ivan corretly mentioned, there is the same issue with links. Usually on the page I have links like a href="/category/post?id=1"
. And obviously when site is opened as my.domain/tool1
this will resolve to my.domain/category...
which is wrong (I want it to point to my.domain/tool1/category...
.)