TechSavy
TechSavy

Reputation: 1340

Deploy one django project with multiple app on multiple subdomains on AWS EC2

I have one django project which contains 2apps namely, admin and api. The admin app is dependent on api app to access the models.

I have 2 subdomains like: admin.xxxx.com and api.xxxx.com.

This project is currently deployed in AWS EC2 using gunicorn + nginx.

UPDATE

All admin requests pass to : some.ip.address.0:8000/admin/, and all api requests pass through some.ip.address.0:8000/

Is there any way I can point my some.ip.address.0:8000/admin/ to admin.xxxx.com and some.ip.address.0:8000/ to api.xxxx.com?

UPDATE 2:

myproject_nginx.conf file:

upstream myproject_backend_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).
  server unix:/home/ubuntu/myproject_backend/myproject_backend.sock fail_timeout=0;

}

server{
    listen 80;
    listen [::]:80;
    server_name admin.mydomain.in;

    location / {
      proxy_pass http://13.***.***.***:8000/admin/;
    }
    location /static/ {
       alias   /home/ubuntu/myproject_backend/static/;
     }

    location /media/ {
       alias   /home/ubuntu/myproject_backend/media/;
     }

 }


server {

    listen 80;

    server_name 13.***.***.***:8000  api.mydomain.in www.api.mydomain.in;

    client_max_body_size 4G;

    location /static/ {
        alias   /home/ubuntu/myproject_backend/static/;
    }

    location /media/ {
        alias   /home/ubuntu/myproject_backend/media/;
    }

    location / {

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $http_host;

        proxy_redirect off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://myproject_backend_server;
            break;
        }
    }

}

myproject urls.py file:

from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve

urlpatterns = [
    re_path(r'^', include('api_app.urls')),
    ...
    path('admin/', include('admin_app.urls')),

    ...

    re_path(r'^static/(?P<path>.*)$', serve,
            {'document_root': settings.STATIC_ROOT}),
    re_path(r'^media/(?P<path>.*)$', serve,
            {'document_root': settings.MEDIA_ROOT}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It opening my admin login page, but I try to login it says: /admin/admin not found on this server.

Please suggest what's wrong?

Upvotes: 1

Views: 943

Answers (2)

Ahtisham
Ahtisham

Reputation: 10116

As I can understand you want to display an admin page when the user types this address http://admin.mydomain.in in their browser and this admin page is handled by your django app so you are using nginx to proxy to http://13.***.***.***:8000/admin/ from where your admin page can be accessed.

But the problem here is that your app doesn't know how to do that. So it needs a middleman ( which in your case is Gunicorn ) that is specifically meant for this purpose. Moreover nginx cannot directly communicate with your django app simply because it was not meant to serve dynamic content.

So to fix this problem you need to configure gunicorn so that it listen to address http://13.***.***.***:8000 to where nginx will forward the request. And then run gunicorn on this address with parameter as your app name. You can read the second answer to the post serving a request from gunicorn to configure your gunicorn file.

Upvotes: 1

pgrzesik
pgrzesik

Reputation: 2427

Yes, in order to do that you have to point two of the domains to EC2 instance hosting your django application (or ELB if you're using one) and configure Nginx so it redirects requests from one domain to admin and from other to api path.

Upvotes: 1

Related Questions