Доступ к сайту администратора Django, развернутому на URL, не являющемся корневым, с помощью mod_wsgi

I have a DRF 3.12.4 (with Django 3.2.3) app deployed on apache httpd 2.2.15 with mod_wsgi 4.6.5 running on Python 3.7. I want my app to be served on non-root URL, e.g. prefixed by /alert.

С следующими настройками в wsgi.conf:

...
Alias /alert/static/ /var/www/djangoapps/achtung_alert/static/
 WSGIDaemonProcess achtung_alert user=infa group=infa threads=25 display-name=%{GROUP} home=/var/www/djangoapps/achtung_alert python-home=/var/www/djangoapps/achtung_alert/venv python-path=/var/www/djangoapps/achtung_alert:/var/www/achtung_alert
 WSGIScriptAlias /alert /var/www/achtung_alert/wsgi.py process-group=achtung_alert application-group=%{GLOBAL}
 WSGIProcessGroup achtung_alert


 <Location /alert >
  WSGIProcessGroup achtung_alert
  WSGIPassAuthorization On
 </Location>
...

и urls.py проекта:

from django.contrib import admin
from django.urls import path, include
from rest_framework.authtoken import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('achtung_alert.urls')),
    path('api-auth/', include('rest_framework.urls')),
    path('api-token-auth/', views.obtain_auth_token),
]

and no FORCE_SCRIPT_NAME in settings.py I am able to access all endpoints in my achtung_alert app, api-auth/, api-token-auth/ - all prefixed by /alert. But if I try to access /alert/admin/ I'm getting the following error from Django's debug page:

Page not found (404)
Request Method: GET
Request URL:    https://mysite/alert/admin/
Using the URLconf defined in achtung_alert_project.urls, Django tried these URL patterns, in this order:

admin/
openapi [name='openapi-schema']
swagger-ui/ [name='swagger-ui']
^events/$ [name='event-list']
... 

The current path, alert/admin/, didn’t match any of these.

Если я сделаю опечатку в URL, то префикс "/alert" будет удален из текущего пути, как и ожидалось:

Page not found (404)
Request Method: GET
Request URL:    https://mysite/alert/admin111/
Using the URLconf defined in achtung_alert_project.urls, Django tried these URL patterns, in this order:

admin/
openapi [name='openapi-schema']
swagger-ui/ [name='swagger-ui']
^events/$ [name='event-list']
...
The current path, admin111/, didn’t match any of these.

В качестве обходного пути я создал пользовательский корневой urls.py:

from django.urls import path, include

urlpatterns = [
  path('alert/', include('achtung_alert_project.urls')),
]

И установите "/" в качестве пути URL в WSGIScriptAlias.

With this changes everything works as expected: I am able to access admin site at /alert/admin and all API endpoints in project's urls.py.

Является ли мое решение с пользовательским корневым urls.py правильным? Можно ли настроить mod_wsgi для обслуживания приложений Django с URL, не являющихся корневыми, с работающим админским сайтом без подобных обходных путей?

Вернуться на верх