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
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 fieldsconfig 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:
You can proxy pass traffic.
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.