Issues with url's server proxy

Hello, I have set up JupyterHub in a Kubernetes cluster, running alongside a Django application.

In my Kubernetes configuration, I’ve defined a custom configuration file (jupyter_server_config.py) to manage server proxies. Specifically, I’ve configured a server named ‘viewer’ with certain settings, including a base URL of ‘https://apps.domain.com/hub’.

extraFiles:
  custom_config:
    mountPath: /usr/local/etc/jupyter/jupyter_server_config.py
    stringData: |
      c = get_config()
      c.ServerProxy.servers = {
          'viewer': {
              'command': [
                  'python',
                  'manage.py',
                  'runserver',
                  '0.0.0.0:7777',
              ],
              'port': 7777,
              'absolute_url': False,
              'base_url':'https://apps.domain.com/hub'
          },
      }

In my Django application’s settings, I’ve configured FORCE_SCRIPT_NAME to be ‘/viewer’. However, when I navigate through the app by clicking buttons, the URLs are incorrect. For example, I’m getting https://apps.domain.com/viewer/projectadd instead of the expected https://apps.domain.com/hub/user/<user>/viewer/projectadd.

How can I ensure that the URLs generated are correct and include the intended base URL (https://apps.domain.com/hub/user/<user>/)?

I’m not sure how django configures these things, but if you have hardcoded /viewer where you should probably have /user/:username/viewer. You can use {base_url} as an argument in command to your command, to get the prefix needed that should be prepended to your URL handlers.

Thank you, minrk! I’ve found a solution by storing the username in an environment variable and then retrieving it within the Django settings.

To achieve the desired configuration, you can save the username as an environment variable and then retrieve it within Django settings as follows:

config.yaml:

singleuser:
extraEnv:
CURRENT_USERNAME: “{username}”

settings.py

import os

Retrieve the username from environment variables or default to ‘admin’ if not found

username = os.getenv(‘CURRENT_USERNAME’, ‘admin’)

Define static URL and script name based on the retrieved username

STATIC_URL = f’/hub/user/{username}/style/’
FORCE_SCRIPT_NAME = f’/hub/user/{username}/viewer’

This setup ensures dynamic handling of the username within your Django settings, allowing flexibility and customization based on the environment.

The whole prefix (/user/{username}/) is available as $JUPYTERHUB_SERVICE_PREFIX, if you want to use that (it should not start with /hub/, unless that’s set as your JupyterHub.base_url, in which case the main hub page would be /hub/hub/).