amitam75
By:
amitam75

How to confingure nginx for domain and subdomain on AWS with ubuntu

November 8, 2017 117 views
Nginx Ubuntu

I want to configure nginx for domain as well as sub domain on ubuntu as follows

mysite.in pointing to website
demo.mysite.in pointing to demo project
uat.mysite.in pointing to test project

My nginx conf looks like:

server {
listen 80 defaultserver;
listen [::]:80 default
server ipv6only=on;

    server_name mysite.in;
    location / {
            root /home/amita/Website/website-v1;
            try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;

    }

}

server {
listen 8087;
listen [::]:8087 default_server ipv6only=on;

    server_name demo.mysite.in;

    location / {

            root /home/amita/Project/frontend/demo/project/dist;
            try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
    }

}

server {
listen 8088;
listen [::]:8088 default_server ipv6only=on;

    server_name test.mysite.in;

    location / {

            root /home/amita/Project/frontend/test/project/dist;
            try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
    }

}

mysite.in , demo.mysite.in, test.mysite.in all shows up webiste.

I am unable to rectify mistake. Please help !!

2 Answers

Hello,

I have a subdomain setup but I'd do it all in DNS.

  1. server_name domain.com;
  2. server_name test.domain.com;
  3. server_name testing.domain.com

For domain.com I would insert an A DNS record with server IP. For the subdomains, I would create A records to the AWS server IP.

For an example of a nginx config file, this is how I'd do it. You should create a file like this on each server a domain or subdomain is pointing to. For example if domain.com is pointing to server1 and test.domain.com is pointing to server2, a nginx config file will need to exist on server1 and server2.

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain.com www.domain.com;
root /path/to/web/files;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}

It's really more of a PHP configuration that I use on my WordPress setup, but it should point you in the right direction.

Thanks.

  • Thank you alexstine.

    All the above I have mentioned are in the same AWS server.

    Can't I have domain and subdomain in same server ?

    Or how about creating three different files considering my requirement mentioned above like

    mysite.in, uat.mysite.in, demo.mysite.in as three config files

    each one will have port 80 mapped to respective projects, only server name will be different.

Hello,

I set my subdomains up in different files. One domain per file to keep things simple. All you really need to do is change the server name as you said. Then you should enable your new Nginx site configs and restart Nginx. This article should help you get up and running.

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

Hope it helps.

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
Have another answer? Share your knowledge.