skip to Main Content

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.

2

Answers


  1. You have to use two server blocks and you have to use two different ports for two websites because you can’t run two different websites on the same port. Let’s assume you want to host domain-one and domain-two on the same ec2 instance server so you can use port 8000 for domain-one and use port 8001 for domain-two.

    Also don’t forget to point ec2 instance server ip on your domain. As u said u already did it but here I also want to explain it for others people so they can learn:

    You need to point your server’s IP address to yourdomain.com and www yourdomain.com. If you haven’t set it up for the www version, people won’t be able to find your website when they search for it using www.

    go to your domain DNS settings

    config for set yourdomain.com

    choose A record then put @ as hostname then puts the server ip in value fields

    config for set www version

    choose A record then put www as hostname then puts the server ip in value fields

    Nginx configuration for hosting two sperate domains on same server:

    server {
         listen 8000;
         listen [::]:8000;
         server_name domain-one.com www.domain-one.com;
    
         root /var/www/domain-one.com/html;
    
         index index.html index.htm;
    
         location / {
              try_files $uri $uri/ =404;
         }
    }
    
    
    server {
         listen 8001;
         listen [::]:8001;
         server_name domain-two.com www.domain-two.com;
    
         root /var/www/domain-two.com/html;
    
         index index.html index.htm;
    
         location / {
              try_files $uri $uri/ =404;
         }
    }
    
    Login or Signup to reply.
  2. 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 application and route traffic based on hostname. You can change listening port as your wish. you need to sping up container with --name and spin up nginx with --link <connected container> to work with container name inside nginx conf.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search