How to configure NGINX to run Django app in a subpath?

1,015 views
Skip to first unread message

Luca Moiana

unread,
Nov 26, 2017, 2:56:05 PM11/26/17
to Django users
Hi,

Sorry for the slight OT but after days of search I have no clue.


Following this DigitalOcean tutorial I was able to setup Django + Postgres + Gunicorn + NGINX running Django on my root,

Now I'd like to change and have something like this:

mydomain.com -> static HTML
mydomain.com/pmapp -> Django app

I created two server blocks in NGINX, one for my static website:

server {


        listen 80;


        listen [::]:80;




        root /var/www/html/officinecartografiche.it;


        index index.html index.htm index.nginx-debian.html;




        server_name www.officinecartografiche.it, officinecartografiche.it, 207.154.206.172;




        location / {


                try_files $uri $uri/ =404;

       }

}


 and one for my django app:

server {


    listen 80;


    listen [::]:80;


    server_name www.officinecartografiche.it, officinecartografiche.it, 207.154.206.172;


    location = /favicon.ico { access_log off; log_not_found off; }


    location /static/ {


        root /home/geouser/pmapp;


    }




    location /pmapp {


        #include proxy_params;


        proxy_pass http://unix:/home/geouser/pmapp/pmapp.sock;


        proxy_redirect http://www.officinecartografiche.it:8000/pmapp/ http://$host/pmapp/;


        proxy_set_header SCRIPT_NAME /pmapp;


    }


}


and added this to settings.py:

FORCE_SCRIPT_NAME = '/pmapp'



WHAT AM I DOING WRONG??

THANKS FOR YOUR HELP!!!

mohammad k

unread,
Nov 26, 2017, 3:06:56 PM11/26/17
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1ee39d2-fd04-4727-88e1-e130def5dd37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matemática A3K

unread,
Nov 26, 2017, 3:42:41 PM11/26/17
to django...@googlegroups.com
I don't know / remember how to do it in Gunicorn, but here is an example with uWSGI:
https://stackoverflow.com/questions/35792409/nginx-serving-django-in-a-subdirectory-through-uwsgi

The main thing is that you will have to deal with "/pmapp" prefix in your routing / urls. You can deal with this inside Django or have it stripped before it gets to Django (by nginx or gunicorn).

HTH :)

Etienne Robillard

unread,
Nov 27, 2017, 5:10:50 AM11/27/17
to django...@googlegroups.com

I have not been able to solve this problem with uWSGI. Apparently, uWSGI is not using the same internal routing semantics than FastCGI.

See: https://forum.nginx.org/read.php?2,275684,275706

Etienne
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Matemática A3K

unread,
Nov 27, 2017, 5:29:01 AM11/27/17
to django...@googlegroups.com
That's good to know, I tried to do it some time ago, it was an attempt of a "sort-of-tennats" which at the didn't go so I stop doing it. At that moment, I remember trying with gunicorn without success and I was near success with uWSGi but didn't make it at that time. I supposed that know would be solved as the s-o question reports success.

Seems that the easiest way to go is to prefix all urls in Django with the mount point - provided that no url is hardcoded in the django project - and use a "regular" nginx conf...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Luca Moiana

unread,
Nov 27, 2017, 6:21:41 AM11/27/17
to Django users
Thanks for your replly, I'll look into the link you suggested.
As for the routing / urls adding FORCE_SCRIPT_NAME = '/pmapp' to settings.py isnt' enough?
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Luca Moiana

unread,
Nov 27, 2017, 6:24:04 AM11/27/17
to Django users
Thanks for your reply, although it sounds hard for a noob like me.

Just to understand, the trick should be done on Gunicorn or on NGINX ?
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Luca Moiana

unread,
Nov 27, 2017, 6:30:39 AM11/27/17
to Django users
Thanks, I read you tutorial but I don't get where I should set my subpath?

thanks a lot
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Luca Moiana

unread,
Nov 27, 2017, 6:32:46 AM11/27/17
to Django users
Googling around I found this page http://albertoconnor.ca/hosting-django-under-different-locations.html but ain't working for me
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1ee39d2-fd04-4727-88e1-e130def5dd37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhLuJEm4kE1D1FHMApW8nYQv%2BWi1Xt9R7o07bgOwqLGsjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Matemática A3K

unread,
Nov 27, 2017, 3:28:55 PM11/27/17
to django...@googlegroups.com
Something like this should work:
https://stackoverflow.com/a/20998131/8930660
and change STATIC_URL and MEDIA_URL

Also, the FORCE_SCRIPT_NAME should also work, but if it doesn't, the first is the most "manual" thing you can do, I think...

Then you have no need to have 2 nginx confs, just put that the proxy_pass "starts" in your prefix instead of root

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Luca Moiana

unread,
Nov 29, 2017, 3:45:16 PM11/29/17
to Django users
Following this tutorial

I came up with this NGINX server:

upstream app_name {

   server unix:/home/geouser/pmapp/pmapp.sock fail_timeout=10;

}


server {


   listen 80;

  root /home/geouser/pmapp;

  server_name www.officinecartografiche.it, officinecartografiche.it, 207.154.206.172;



   location /pmapp/ {

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

     proxy_set_header Host $http_host;

     proxy_redirect off;

     proxy_pass http://app_name/;


}

}


and I can access the "it works" page from: http://my domain/pmapp

But, If I try http://my domain/pmapp/admin

I have error 404

then if I use www.mydomain/pmapp I have "DisallowedHost at /" error but I added all possibile spell of my site, including IP, on 'ALLOWED_HOSTS' in settings.py

Any help?

Etienne Robillard

unread,
Nov 29, 2017, 4:56:37 PM11/29/17
to Luca Moiana, django...@googlegroups.com

Whats wrong in using FastCGI for your specific use-case ?

Etienne

For more options, visit https://groups.google.com/d/optout.

Luca Moiana

unread,
Nov 30, 2017, 4:42:05 AM11/30/17
to Django users
Nothing wrong, but havign set up the system I didn't want to change software cause I'm not that savvy.
Anyway, I ended up hiring someone on Fivrr and  got the job done. I was closed, but not closed enough

Thank for you help !!!
Reply all
Reply to author
Forward
0 new messages