Nikhil Patil
Nikhil Patil

Reputation: 93

How to setup nginx to create a sub-domain?

I have one domain myartistbook.in, which is working fine. I want to create a sub-domain for it named adminpanel.myartistbook.in. The steps I followed are:

  1. Added sub-domain name to /etc/hosts

     127.0.0.1 localhost
    ***.***.***.180 myartistbook
    ***.***.***.180 adminpanel.myartistbook


  1. edited /etc/nginx/nginx.conf:
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    include /usr/share/nginx/modules/*.conf;
    events {
        worker_connections 1024;
    }
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  myartistbook.in www.myartistbook.in;
        root         /root/krim.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

   server {
    listen 80;
    root /root/sites/adminpanel.com;
    server_name adminpanel.myartistbook.in www.adminpanel.myartistbook.in;
    index index.html;

    location / {
        try_files $uri $uri/;   
        }
    }
}

  1. Added a file named adminpanel.com inside /etc/nginx/sites-available.
server {
          listen 80;
          server_name www.adminpanel.myartistbook.in;
          server_name ***.***.***.180;
          root /root/sites/adminpanel.com;
          index index.html;

          access_log /var/log/nginx/adminpanel.com.access.log;
          error_log /var/log/nginx/adminpanel.com.error.log;
          location / {
            try_files $uri /index.html =404;
          }
        }

  1. Linked the above server block into /etc/nginx/sites-enabled

sudo ln -s /etc/nginx/sites-available/adminpanel.com /etc/nginx/sites-enabled/adminpanel.com

  1. Restarted nginx with

sudo service nginx restart

While accessing the site I'm getting error adminpanel.myartistbook.in’s server IP address could not be found. Am I missing any step?

Upvotes: 0

Views: 635

Answers (1)

johnsing
johnsing

Reputation: 141

Together with server-side configurations involving domains and subdomains in server_name, you need to make sure that your DNS records for myartistbook.in are properly configured as well.

Have you added a DNS record to point adminpanel.myartistbook.in to your server? You can either add an A record or a CNAME record for this. Looking at a quick check using whatsmydns.net for adminpanel.myartistbook.in, it seems you missed this step. Please check the documentation of your domain registrar on how to do this.

After adding the records, do a quick check again using whatsmydns.net if your CNAME/A record has taken effect.

In your step 3, modify the server_name to adminpanel.myartistbook.in without the www, unless this is actually your intention to use the whole www.adminpanel.myartistbook.in, which I don't think is the case here because you attempted to access it without the www:

adminpanel.myartistbook.in’s server IP address could not be found.

Also, it's probably good to use a different server {} block when referring to IP addresses as server_name then redirect to the domain/subdomain. However, redirecting from IP Address going to your domain/subdomain may be not be the priority now. Suggest to remove that line in the meantime.

Then run a quick nginx -T to check for errors in your configuration. If the check is successful, reload your nginx web server and try accessing the subdomain again.

Hope that helps!

Upvotes: 1

Related Questions