0

I am serving a django app with a combination of nginx reverse proxy and waitress. In the nginx configuration the app is linked via a location:

location /app/ {
            proxy_pass http://localhost:8686/;
    }

While the app runs on via waitress on port 8686.

Now, if I go to the domain.com/app, I the index page is served correctly. Though, my django html template contains the following link:

 <p> You are not logged in.</p> <a href="/accounts/login"><button>Login</button></a>

When I press that button I get to

domain.com/accounts/login

but it should be

domain.com/app/accounts/login

I wonder how to change the code so that it works independently of where the app is linked.

In urls.py the urls are included like this:

urlpatterns:  = [...,
    path('accounts/', include('django.contrib.auth.urls'))]
Soerendip
  • 7,684
  • 15
  • 61
  • 128

1 Answers1

1

Define the url in urls.py (most probably you've already done this) and then use reverse in templates:

<a href="{% url 'foo:bar' %}"><button>Login</button></a>

Then rewrite URLs in nginx to make your app think that you're accessing /accounts/login instead of /app/accounts/login:

location /app/ {
    rewrite ^/app(.*)$ $1 last;
    proxy_pass http://localhost:8686/;
}

Docs:

  1. https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#std:templatetag-url
  2. https://www.nginx.com/blog/creating-nginx-rewrite-rules/
Max Malysh
  • 29,384
  • 19
  • 111
  • 115
  • The url is included with urlpatterns = [ path('admin/', admin.site.urls), path('accounts/', include('django.contrib.auth.urls'))] – Soerendip Aug 20 '19 at 22:21
  • Actually, you may ignore the `urls.py` advice. Everything you need to solve your problem is URL rewrite. – Max Malysh Aug 20 '19 at 22:23