Skip to content

Add support for hosting on subdir #1297

Open

Description

@sintech

Currently the only supported way to host inventree project is domain root. http://inventree.com/
It would be more flexible if one could host it on subdir too, e.g. http://example.com/inventree

I thought that changing STATIC_URL and MEDIA_URL will be enough, but it seems that / prefix is used in a lot of scripts and templates including redirect to /index in urls.py

Activity

  1. SchrodingersGat commented on Feb 14, 2021

    @SchrodingersGat
    Member

    Huh, I never considered this. But yeah, that's a problem!

    I'm going to start a branch with support for this. Can you help me out with testing?

    For now, can you please let me know where you are coming across errors due to links not working properly?

    Thanks

  2. SchrodingersGat commented on Feb 14, 2021

    @SchrodingersGat
    Member

    Oh and also the changes that you initially made to STATIC_URL and MEDIA_URL

  3. sintech commented on Feb 14, 2021

    @sintech
    ContributorAuthor

    Sure I can help with testing.
    I tried to deploy inventree on my server using Apache + mod_wsgi.

    I implemented 'url_prefix' config parameter (almost as you did in #1298)
    url_prefix: '/inventree' in config.yaml
    and in settings.py
    URL_PREFIX = CONFIG.get('url_prefix','')
    STATIC_URL = URL_PREFIX+'/static/'
    MEDIA_URL = URL_PREFIX+'/media/'

    And I changed all /part to part in InvenTree/templates/js/part.js just to check it is working.
    I guess there are a lot of places where links should be changed to relative to work properly with subdirs.

    Besides that, I had errors in inventreeCommitHash() and inventreeCommitDate() from version.py because git command was executed in root / directory.
    I fixed it using cwd param:
    return str(subprocess.check_output('git rev-parse --short HEAD'.split(),cwd=os.path.dirname(os.path.realpath(__file__))), 'utf-8').strip()

  4. SchrodingersGat commented on Feb 17, 2021

    @SchrodingersGat
    Member

    @sintech could you please check the branch in #1298 and see what still needs work?

  5. sintech commented on Feb 17, 2021

    @sintech
    ContributorAuthor

    So partially it works but some links are still wrong.

    My server configuration (apache 2.4 + mod_wsgi):

    # InvenTree
    Alias /inventree/static /var/www/inventree/inventree-subdir/inventree_static
    <Directory /var/www/inventree/inventree-subdir/inventree_static>
     Require all denied
     Require ip <my ip>
    </Directory>
    Alias /inventree/media /var/www/inventree/inventree-subdir/inventree_media
    <Directory /var/www/inventree/inventree-subdir/inventree_media>
     Require all denied
     Require ip <my ip>
    </Directory>
    WSGIProcessGroup inventree
    WSGIDaemonProcess inventree python-home=/var/www/inventree/inventree-env home=/var/www/inventree/inventree-subdir/InvenTree python-path=/var/www/inventree/inventree-subdir/InvenTree
    WSGIScriptAlias /inventree /var/www/inventree/inventree-subdir/InvenTree/InvenTree/wsgi.py process-group=inventree
    <Directory /var/www/inventree/inventree-subdir/InvenTree/InvenTree>
       <Files wsgi.py>
           Require all denied
           Require ip <my ip>
       </Files>
    </Directory>
    
  6. SchrodingersGat commented on Mar 10, 2021

    @SchrodingersGat
    Member

    @sintech can you please post your settings.py file after your modifications?

  7. sintech commented on Mar 10, 2021

    @sintech
    ContributorAuthor

    Sure, here is diff:

    # git diff settings.py 
    diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py
    index 98b7fa8..e93abc5 100644
    --- a/InvenTree/InvenTree/settings.py
    +++ b/InvenTree/InvenTree/settings.py
    @@ -146,9 +146,9 @@ SUBPATH_URL = get_setting(
     FORCE_SCRIPT_NAME = SUBPATH_URL
     
     # Web URL endpoint for served static files
    -STATIC_URL = '/static/'
    +STATIC_URL = 'static/'
     
    -API_URL = '/api/'
    +API_URL = 'api/'
     
     if SUBPATH_URL:
         STATIC_URL = os.path.join(SUBPATH_URL, STATIC_URL)
    @@ -170,7 +170,7 @@ STATICFILES_DIRS = [
     STATIC_COLOR_THEMES_DIR = os.path.join(STATIC_ROOT, 'css', 'color-themes')
     
     # Web URL endpoint for served media files
    -MEDIA_URL = '/media/'
    +MEDIA_URL = 'media/'
     
     if SUBPATH_URL:
         MEDIA_URL = os.path.join(SUBPATH_URL, MEDIA_URL)
    @@ -516,3 +516,5 @@ DBBACKUP_STORAGE_OPTIONS = {
     INTERNAL_IPS = [
         '127.0.0.1',
     ]
    +
    +CSRF_COOKIE_PATH = '/inventree'
    
    
  8. rcludwick commented on Jun 15, 2021

    @rcludwick
    Contributor

    Apologies for coming in late. Doesn't nginx/apache support transparently rewriting of urls in the reverse proxy mode?

    https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite

    That would probably be the proper way to handle this.

  9. SchrodingersGat commented on Jun 16, 2021

    @SchrodingersGat
    Member

    @rcludwick I would like to push this problem outside of the scope of the InvenTree server itself.

    I just tried configuring a nginx setup as per your example but could not get it to work. Have you had specific experience with this?

  10. matmair commented on Jun 16, 2021

    @matmair
    Member

    @SchrodingersGat a transparent rewrite /proxy seems like a good idea. But I would leave that to the admin of the webserver, that works with the current setup relatively easy.

  11. SchrodingersGat commented on Jun 16, 2021

    @SchrodingersGat
    Member

    @matmair do you have some more details on this? I'd like to know that it can be made to work well by (for e.g.) nginx, so that I can close out this issue. I'd rather not have InvenTree handle this problem.

    I tried to implement the url rewrite as per the suggestion of @rcludwick above.

    It worked somewhat - a request for localhost:8000/subdir/part/ correctly sent me to /part/ InvenTree view.

    However, the InvenTree page then requested static files from /static/ which the proxy tried to serve from localhost:8000/static/ and this then failed.

  12. rcludwick commented on Jun 17, 2021

    @rcludwick
    Contributor
  13. rcludwick commented on Jun 20, 2021

    @rcludwick
    Contributor

    Apologies for taking a while. proxy_pass on nginx is the way to go here. It rewrites the url before handing it off.

    https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

    Apache might do it too, but it's been around 2 decades since I last touched apache. mod_rewrite maybe? I don't know.

    I would close this and add documentation on at least for nginx.

  14. marklinmax commented on Jul 2, 2021

    @marklinmax

    Sorry to jump in like that, but isn't nginx proxy_pass and url rewrite one-way? The server will effectively receive the request without the subpath, but will not redirect well since it isn't aware of the modification.

    I tried configuring it like that and could reach the server but never got a proper response.

    Maybe I am just doing something wrong?

    So I think that both the INVENTREE_SUBPATH environment variable and nginx proxy_pass / url rewrite are needed.

  15. rcludwick commented on Jul 2, 2021

    @rcludwick
    Contributor
  16. 53 remaining items

  17. SchrodingersGat commented on Nov 12, 2024

    @SchrodingersGat
    Member

    So, are you suggesting we should bump to a later release e.g. 1.1.0? I am OK with that

  18. modified the milestones: 1.0.0, horizon on Dec 28, 2024
  19. modified the milestones: horizon, 1.1.0 on Jul 2, 2025
  20. wolfpackmars2 commented on Sep 3, 2025

    @wolfpackmars2
    Contributor

    I'm glad to see there is a lot of discussion about this topic. I too would like to see this feature.

    Django has a setting "FORCE_SCRIPT_NAME". However, it needs to be supported by the app.

    An example django app that uses this is Paperless-ngx, implemented via settings.py. For funsies, I tried setting INVENTREE_FORCE_SCRIPT_NAME in .env, but no dice as expected. See also https://docs.paperless-ngx.com/configuration/#PAPERLESS_FORCE_SCRIPT_NAME

    I have never been able to get URL rewriting to work. I don't believe URL rewriting would be the correct way to handle this, especially if Django has some provision for this feature.

    The main reason for this in my case is I need only run a small instance of Inventree that is available publicly for my personal use but it doesn't need to be advertised. While security-by-obscurity is not secure by itself, it doesn't hurt as an additional independent layer of security. Mostly, not having my instance publicly indexed adds another hurdle for a malicious botnet to overcome.

    Other advantages to using subdir URLs are reduced administration of the DNS records for subdomains and reduced management of SSL certificates.

    An alternative might be a wildcard DNS record, though I haven't explored this concept.

  21. matmair commented on Sep 3, 2025

    @matmair
    Member

    what you are trying to achieve is not really helped by this sub-path approach, it is very much a false sense of layer of security. also if you are doing manual ssl management you should look into ACME / LEGO

  22. modified the milestones: 1.1.0, 2.0.0 on Oct 17, 2025
  23. newfolder0 commented on Mar 10, 2026

    @newfolder0

    I've been running two InvenTree instances under different subdomains. I'll describe it here, in case it helps anyone.

    1. I've set up the DNS settings with my domain registrar to forward the inventree-test.example.com and inventree.example.com subdomains to my Inventree server (Debian VM), while the TLD example.com still sends people to my main (not inventree) web server.
    2. There's a high level directory with a Caddy server running in Docker, as a pretty minimal/simple reverse proxy. This forwards incoming HTTPS requests to the relevant instance.
    3. Each InvenTree instance is a separate Docker container but both are running on the same cloud-hosted Debian VM.
    4. The two InvenTree instances are stored in sub-directories and brought up as a set of docker containers named "inventree_" based on environment variables set in .env (so that their internal Caddyfiles are identical). In my case these are inventree_test (test server) and inventree_prod (production server).

    I am by no means an expert and it took me a while to work this out. I have no idea if it's the best approach, but it works well for me. I'm happy to share more details or help others if I can.
    If anyone else ends up using it... @matmair, @SchrodingersGat, tell me if you want me to write it up somewhere less ephemeral.

  24. matmair commented on Mar 10, 2026

    @matmair
    Member

    The question is about subdirectory hosting, not subdomain hosting as you are doing (which is fully supported)

  25. newfolder0 commented on Mar 10, 2026

    @newfolder0

    Aaah sorry, my mistake.

  26. Oinosseus commented on Aug 14, 2026

    @Oinosseus

    Hello, I also have the issue that I want to operate InvenTree with a sub-path like https://my.server.tld/inventree
    My setup is an apache with reverse proxy. I have no knowledge about django/gunicorn and could not make it accept the sub-path. Also I had no luck with url/path replacing in the apache config.

    Are there meanwhile any recommendations how to operate InvenTree with sub-path in the URL?

  27. matmair commented on Aug 14, 2026

    @matmair
    Member

    That is not supported and will cause numerous hard to debug issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    User Interfaceenhancementroadmap

    Projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions