This tutorial explains how to add a subdomain on nginx server to your already existing and running nginx server.
How to add a subdomain in nginx using terminal
Nginx treats a new subdomain as a new entity, so basically the process of adding new subdomain is similar to adding a new domain name to your already running nginx server.
The basic steps to get this done are as follow:
1. Add subdomain in dns management
Add the new subdomain in your dns management panel. If you’re using cloudflare for example, then you can add it there as an “A” record.
If you need to do that manually on the nginx server (if you’re using it as a dns server too), then add this to the dns zone file:
subdomain.domain.com. 1800 IN A 111.222.333.444
Change the subdomain to your subdomain and domain to your domain. Also change the ip address 111.222.333.444 to your servers ip address.
2. Create config file in sites-available
Now we need to create the config file for our new domain or subdomain in the sites-available folder on our nginx server directory.
We can simply copy the config file of any other domain already running. You can use default file too if you have made it as sample and seems easy to be changed in your case.
To create a new copy of nginx configuration file for your subdomain or domain, run this command on terminal:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/subdomain
Replace subdomain with the subdomain name that you want to use.
3. Edit the nginx configuration file
Open the newly created nginx configuration file in the sites-available folder and edit it.
We mainly need to change the path and server_name only in it. Look for the first two lines in the following code snippet and change the part under it to match your server path and subdomain name.
listen 80 default_server; #listen [::]:80 default_server ipv6only=on; root /var/www/domain/subdomain/public_html; index index.php index.html index.htm; server_name subdomain.domain.com;
Save the file and exit.
4. Copy sites-available file to sites-enabled
This is an important step and if missed, results in confusion for many people as to why the new addon domain isn’t working.
To copy the newly created config file for the new subdomain to the sites-enabled directory, use the following command in terminal. Make sure that you edit the path of both sites-available and site-enabled directories if they’re different on your server.
sudo ln -s /etc/nginx/sites-available/subdomain /etc/nginx/sites-enabled/
Change the subdomain with the new subdomain name or the filename that you choose above.
5. Restart nginx
Restart your nginx server to enable the newly created subdomain.
sudo service nginx restart
Let me know if you encounter any issue while adding a new subdomain or domain to your server running nginx. This tutorial was written with ubuntu 14.04 in view, however this should work on all other linux distro and other versions of ubuntu too.
I tried to add a subdomain usercontent on my server but it results in 404
server {
listen 80;
server_name usercontent.sportsapp.co.in http://www.usercontent.sportsapp.co.in;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
this is my sites-enabled/usercontent
You have to do that in sites-available and then create a symlink for it using
sudo ln -s /etc/nginx/sites-available/subdomain /etc/nginx/sites-enabled/