Nginx/Virtual Host/
2022-10-17T09:36:12.946650Z
Published on
Hosting multiple websites on a single web server is called a virtual host. But in Nginx, the concept is called Server blocks that use the server_name and listen to directives to bind to TCP sockets. Also in certain scenarios, if you don’t want to edit the default Nginx files you can set up a virtual host to avoid editing the default configuration file.
A visual explanation of the virtual host is given below in the image:
Ubuntu server
Domain name
Map ubuntu server public IP to the domain name’s DNS.
Check out this tutorial, to link (map) your newly purchased domain name with the ubuntu server.
Check out this tutorial, to install Nginx on Ubuntu.
Create a new file with your domain name under /etc/nginx/sites-available
1cd /etc/nginx/sites-available
2vim <Domain_Name>
3
4For example:
5vim iternerays.com
Add the following content to the file, this
Note:* Change <Domain_Name> with your domain name (for example: iternerays.com)
1server {
2 listen 80;
3 listen [::]:80;
4
5 server_name <Domain_Name>;
6
7 root /var/www/<Domain_Name>;
8 index index.html;
9
10 location / {
11 try_files $uri $uri/ =404;
12 }
13}
The sample domain configuration file link given below.
https://github.com/episyche/nginx_virtual_host_example.git
Create a symbolic link from sites-available to sites-enabled
1sudo ln -s /etc/nginx/sites-available/<Domain_Name> /etc/nginx/sites-enabled/
2
3For example:
4sudo ln -s /etc/nginx/sites-available/iternerays.com /etc/nginx/sites-enabled/
After executing the command a symbolic link will be created. An example output screenshot is given below:
Create a directory under /var/www with the domain name.
1cd /var/www
2mkdir <Domain_Name>
3
4For example:
5mkdir iternarys.com
Navigate to the domain directory created above step.
1cd /var/www/<Domain_Name>
2
3For example:
4cd /var/www/iternarys.com
Create index.html under your domain directory (i.e /var/www/<Domain_Name>)
1touch index.html
2echo"<h1>Hello World</h1>" >> index.html
Restart Nginx
1sudo systemctl restart nginx
check your browser, by visiting your domain name (for example: iternerays.com ). The reference output screenshot is given below.
Comments