Yesterday my friend ask me if a single NGINX can serve more than one domain name. Of course, it can! A singe NGINX instance can serve more than one domain name. It can also serve subdomains. It is useful if you use one server to serve multiple domain names, although it is not recommended.
In this article, we will configure our NGINX to serve domain mylocalsite.com
and anotherlocalsite.com
.
NGINX Configuration Files
Normally, we created a configuration file for each domain that NGINX serves. So, we need to create a separate configuration for mylocalsite.com
and anotherlocalsite.com
.
Configuration for mylocalsite.com
:
server {
server_name mylocalsite.com;
listen 80;
location /mypage {
default_type text/plain;
return 200 'this response is for my local site';
}
}
/etc/nginx/site-available/mylocalsite.com
.Configuration for anotherlocalsite.com
:
server {
server_name anotherlocalsite.com;
listen 80;
location /mypage {
default_type text/plain;
return 200 'this response is for another local site';
}
}
/etc/nginx/site-available/anotherlocalsite.com
.Enable the configurations by creating a link file to /etc/nginx/site-enabled/
.
ln -s /etc/nginx/site-available/mylocalsite.com /etc/nginx/site-enabled/mylocalsite.com
ln -s /etc/nginx/site-available/anotherlocalsite.com /etc/nginx/site-enabled/anotherlocalsite.com
Test the configuration.
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload if the configs is valid.
service nginx reload
Try accessing both domains. You will get the response based on the configuration.