0

I have a Django project which I would like to run (host) in production using Nginx.

I tried to expose the Django project directly using Nginx:

Direct

urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    
    path('', include('dashboard.urls'), name= 'dashboard'),
    path('app1/', include('app1.urls'), name= 'app1'),
    path('app2/', include('app2.urls') ,name='app2')
       ]

default.conf

upstream django_app {
  server django_app:8000;
}

server {
  listen 80;
  listen [::]:80;

  server_name demo.company.com;

  location / {
      proxy_pass http://django_app;
      proxy_redirect off;     
      proxy_set_header Host $host;   
      proxy_set_header X-Real-IP $remote_addr;        
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        
  }

  location /static/ {
    alias /var/www/static/;
  }
}

I have three URLs with one dashboard '' and two Django apps, app1/ and app2/. And the above configuration allows all the application to be accessed on the designated route:

  • http//demo.company.com/
  • http//demo.company.com/app1
  • http//demo.company.com/app2

Now the issue is that I have multiple different projects running, hence I want to expose this project behind a custom location block /custom.

Custom

default.conf (modified)

I have modified the nginx configuration file to the following:

upstream django_app {
  server django_app:8000;
}

server {
  listen 80;
  listen [::]:80;

  server_name demo.company.com;

  location /custom {
      rewrite ^/custom/?(.*) /$1 break;
      proxy_pass http://django_app;
      proxy_redirect off;     
      proxy_set_header Host $host;   
      proxy_set_header X-Real-IP $remote_addr;        
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        
  }

  location /static/ {
    alias /var/www/static/;
  }
}

Here I am rewriting everything from Django with custom location block. Now I can access all the apps, when I type these URLs (hardcoded):

  • http//demo.company.com/custom/
  • http//demo.company.com/custom/app1
  • http//demo.company.com/custom/app2

My app dashboard has a clickable button for app1 and app2, and if I try to go to the app1 by clicking a this button, the location block /custom is not recognized. It changes the URL to http//demo.company.com/app1 and returns page not found error.

May I ask how can I resolve this issue, any advice will be highly appreciated. Thanks in advance!

Mohammad Saad
  • 1,935
  • 10
  • 28
  • Does this answer your question? [nginx location path with proxy\_pass](https://stackoverflow.com/questions/25475021/nginx-location-path-with-proxy-pass) – Ivan Starostin Jun 08 '22 at 11:26
  • @IvanStarostin thank you for the response! Yes I have tried your suggestion. This helps on the home page of the project but once i try to navigate to another app, it doesn't recognize the location block. – Mohammad Saad Jun 08 '22 at 12:33
  • 1
    Still, more like [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem#:~:text=The%20XY%20problem%20is%20asking,trouble%2C%20you%20ask%20about%20Y.). Please try rephrasing your question starting with _what you are trying to achieve_. Not _how_. I don't see why `rewrite`, why `custom`, why `FORCE_SCRIPT_NAME`. – Ivan Starostin Jun 08 '22 at 15:13
  • Thanks @IvanStarostin for the response, I have rewritten the question. Hopefully the problem statement is much clearer now. Do let me know if i need to add more details. Highly appreciate your feedback! – Mohammad Saad Jun 14 '22 at 07:42
  • Perhaps removing `rewrite` could be enough to fix this. Take a look at these questions [one](https://stackoverflow.com/questions/53788577/how-to-serve-subdirectory-as-root-in-nginx-using-django) [two](https://stackoverflow.com/questions/44987110/django-in-subdirectory-admin-site-is-not-working) – Ivan Starostin Jun 14 '22 at 15:13

0 Answers0