2

I'm trying to deploy a Django project on under a sub path of my institute website.

Let's say server IP is: X1.X2.X3.X4.
And xyz.edu/tnp probably points to X1.X2.X3.X4/portal/.

Structure of Django project is like this:

.
|-- project
|   |-- app1
|   |-- app2
|   |-- project_nginx.conf
|   |-- project_uwsgi.ini
|   |-- db.sqlite3
|   |-- manage.py
|   |-- media 
|   |-- README.md
|   |-- static
|   `-- uwsgi_params
|-- README.md
|-- requirements
|   |-- base.txt
|   |-- dev.txt
|   |-- prod.txt
|   `-- test.txt
|-- requirements.txt
|-- static_cdn
|   |-- admin
|   .....

And my URL scheme in project/urls.py looks like this:

 url(r'^app1/', include('app1.urls')),
 url(r'^app2/', include('app2.urls')),
 url(r'^admin/', include(admin.site.urls)),

On my local server, following scheme is working perfectly.
* 127.0.0.1:8000/app1/
* 127.0.0.1:8000/app1/login/
* 127.0.0.1:8000/app1/homepage/
* 127.0.0.1:8000/app2/somepage
* 127.0.0.1:8000/admin/

I want to map my URLs on my deployment server, so that
xyz.edu/tnp/app1 links to X1.X2.X3.X4/portal/app1 and
xyz.edu/tnp/app2 links to X1.X2.X3.X4/portal/app2 xyz.edu/tnp/admin links to X1.X2.X3.X4/portal/admin
and so on.

The problem is the suffix portal in X1.X2.X3.X4/portal/. When I type xyz.edu/tnp/app1 in browser, the error I'm receiving is

Using the URLconf defined in portal.urls, Django tried these URL patterns, in this order:

^app1/
^app2/
^media/(?P.)$
^static/(?P.
)$

The current URL, portal/app1/, didn't match any of these.

Now I think of two approaches:

  1. Manually prepend portal to all urls in project/urls.py

  2. Drop the prefix portal in the portal_nginx.conf file.

First approach isn't working for internal urls. The index page for all apps, i.e. app1, app2 and so on, is working fine but for other urls, they are being rendered as xyz.edu/portal/login instead of xyz.edu/tnp/app1/login.

Second approach, as mentioned here and here, seems promising, but its not working for me. Maybe I'm doing something wrong. Following are my project_nginx.conf and project_uwsgi.ini files.

project_uwsgi.ini

[uwsgi]

chdir           = ...
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = ...

# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = ...
# clear environment on exit
vacuum          = true

project_nginx.conf

upstream django {
    server unix://.../.../project.sock; # for a file socket
    # server 127.0.0.1:8001; 
}

# configuration of the server
server {
    listen      80;
    server_name xyz.edu; # substitute your machine's IP address or FQDN
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /usr/share/nginx/.../project/media;
    }

    location /static {
        alias /usr/share/nginx/.../project/static;
    }

    # Finally, send all non-media requests to the Django server.
    location /portal/ {
        rewrite /portal/(.*) /$1  break;
        uwsgi_pass  django;
        # # the uwsgi_params file you installed
        include     /usr/.../wsgi_param
        # uwsgi_param SCRIPT_NAME /portal/;
        # uwsgi_modifier1 30;

     }
}
Rohan Kumar
  • 41
  • 1
  • 4

0 Answers0