Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx serving Django in subdirectory - admin login is redirecting

I have to serve a Django app from a subdirectory (hostname/service/). So far I'm able to get to the Admin Login prompt (hostname/service/admin/login/?next=/admin/), but after successfully logging in I'm redirected to (hostname/admin/login/) and get a 404.
How can I keep the correct subdirectory and get inside the Admin panel?

Here's the nginx.conf server block:

server {
    listen 80;
    root  /usr/share/nginx/html/;
    charset utf-8;

    location /service {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://djangoapp:8000/;
      proxy_set_header X-Real-IP $remote_addr;
    }

    location /static {
        alias /usr/src/app/static;
    }
}

Edit: These are the urlpatterns from urls.py:

from django.conf.urls import url, include
from django.contrib import admin

# A custom view of my own
from app.views import AdvanceCustomSearchView

urlpatterns = [
  url(r'^search/$', AdvanceCustomSearchView(), name='index_search'),
  url(r'^app/', include('app.urls')),
  url(r'^admin/', admin.site.urls),
]
like image 671
jcDuenas Avatar asked Feb 20 '18 20:02

jcDuenas


1 Answers

You need to add the FORCE_SCRIPT_NAME setting to your settings.py as such:

FORCE_SCRIPT_NAME = '/service'

mind that there's no trailing slash at the end.

like image 166
fixmycode Avatar answered Sep 22 '22 00:09

fixmycode