pythonpython-3.xdjangodjango-adminkong

Django admin page doesn't open, when I use Kong gateway


I use Kong gateway to make Django project accessible at localhost/python. I can see Django start page, but when I try to open localhost/python/admin it redirects to localhost/admin/login/?next=/admin/, so that isn't Django project anymore. Project is just files from python -m startproject with connected db. What can I do to fix that? Thanks

This is how Kong service and route looks like:

{
  "enabled": true,
  "tls_verify": null,
  "tls_verify_depth": null,
  "port": 3000,
  "protocol": "http",
  "path": null,
  "client_certificate": null,
  "write_timeout": 60000,
  "connect_timeout": 60000,
  "retries": 5,
  "read_timeout": 60000,
  "id": "8da72803-ffea-4424-a568-31faa3782a6d",
  "name": "python-service",
  "host": "python-service",
  "updated_at": 1711390446,
  "created_at": 1711390446,
  "tags": null,
  "ca_certificates": null
}

route:

{
  "next": null,
  "data": [
    {
      "strip_path": true,
      "regex_priority": 0,
      "methods": null,
      "request_buffering": true,
      "response_buffering": true,
      "protocols": [
        "http",
        "https"
      ],
      "service": {
        "id": "8da72803-ffea-4424-a568-31faa3782a6d"
      },
      "https_redirect_status_code": 426,
      "tags": null,
      "paths": [
        "/python"
      ],
      "headers": null,
      "id": "2c1fb24a-21dd-42f5-9784-a3bf95c6f7dc",
      "path_handling": "v0",
      "created_at": 1711390464,
      "updated_at": 1711390464,
      "name": null,
      "snis": null,
      "sources": null,
      "hosts": null,
      "preserve_host": false,
      "destinations": null
    }
  ]
}

Solution

  • Your issue lies with the strip_path: true value in your Kong route settings.

    When set to true, Kong will strip the path that matches your route from the URL before forwarding it to your upstream service (Django in this case). This is why when you try to access localhost/python/admin, Kong strips the '/python' and only '/admin' is left, which doesn't exist in your Django project.

    Therefore, you should change the strip_path value to false as follows:

    {
      "next": null,
      "data": [
        {
          "strip_path": false,
          ...
          "paths": [
            "/python"
          ],
          ...
        }
      ]
    }
    

    With strip_path set to false, the initial request to localhost/python/admin will be forwarded to your Django project as /python/admin, and Django will be able to handle the routing correctly.

    After making this change, remember to reload your Kong configuration to apply the changes.

    Please note that this would also mean adjusting your Django project's URL configuration to handle the additional '/python' in the path, as Django will now see it as part of the path. So ensure your Django URLs are correctly setup.