Search code examples
djangonginxamazon-ec2amazon-route53

AWS Route53 + Nginx + Django, host 2 applications with CNAME


I am trying to host 2 applications in aws EC2 instances. I am using dockerlized nginx, django. The 2 apps are 2 separate django containers. I added record

Record A => www.main.com XXX.XXX.XXX.XXX
Record CNAME => www.aap2.main.com www.main.com

Now, how should I configure Nginx so that if user access with URL www.main.com, it is served with app1 and if user access with URL www.aap2.main.com, it is served with app2?

Also can I use the SSL certificate which I got for www.main.com?

I tried playing around with nginx.conf file but did not have luck so far.


Solution

  • You can proxy pass traffic.

    server {
      listen         80;
      server_name    www.main.com;
        location / {
          proxy_pass  http://<main app container name or ip>:<main app container port>;
       }
    }
    
    server {
      listen         80;
      server_name    www.aap2.main.com;
        location / {
          proxy_pass  http://<aap2 container name or IP>:<aap2 container port>;
       }
    }
    

    nginx will listen to port 80 for both applications and route traffic based on the hostname. You can change the listening port as per your wish. You need to spin up a container with --name and spin up nginx with --link <connected container> to work with container name inside nginx conf.