EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Nginx Tutorial Nginx Server Block
Secondary Sidebar
Nginx Tutorial
  • Basic
    • Nginx Add_header
    • Nginx FastCGI
    • Nginx WAF
    • Nginx Forward Proxy
    • Nginx Ingress Annotations
    • Nginx Location Directive
    • Nginx User
    • Nginx Port
    • Nginx Auth_request
    • Nginx Autoindex
    • Nginx if else
    • Nginx uWSGI
    • Nginx X-Forwarded-For
    • Nginx GUI
    • NGINX Unit
    • NGINX if
    • Nginx default_server
    • Nginx worker_connections
    • Nginx Authentication
    • Nginx Gateway Timeout
    • Nginx Error_page
    • Nginx Server Block
    • Nginx Forbidden
    • Nginx Modules
    • Nginx Max Upload Size
    • Nginx vs LiteSpeed
    • Nginx multiple server_name

Nginx Server Block

Introduction to Nginx Server Block

The Nginx server block allows us to run multiple websites on a single machine. By using server blocks, we can specify the root site document, which was used to create a separate security policy for each site. We need to use a different SSL certificate for every site. The root document is the directory where domain name website files are stored and served in the request-response.

Nginx Server Block

Overview

The Block of server is referred to as the virtual host of the nginx server and the feature of the nginx server, allowing us to host multiple websites on a single server. As opposed to setting up and configuring the server for every domain and hosting a number of websites on a single machine will save our money and time. Domains are isolated and separate from each other. The virtual host of nginx is faster on multiple websites on single machines. Each website contains the individual documentation of the site; we need to create directory structures that are individual for storing the data.

How to Install Nginx Server Block?

When we need to host more than one website or domain on our nginx server, for the same, we need to configure the server block to encapsulate all of our configurations of the domain. It will become synonymous with the virtual host files of apache; it will serve the same purpose.

A record is nothing but the record of DNS, which points to the domain’s name for the public server’s IP address.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)

Below steps shows how to install it:

1. In the first step, we create the directory for our domain which contains all the settings which were related to our domain. After making the directory, we assign the ownership using the $USER variable of the environment. It will give the ownership of the directory to the currently logged user. After changing the ownership, we assign the appropriate permissions to the data directory for users and groups as follows. Finally, after correctly configuring the consent of the username, we use the same in the domain.

Code:

sudo mkdir -p /var/www/nginx.info/html
sudo chown -R $USER:$USER /var/www/nginx.info/html
sudo chmod -R 755 /var/www/nginx.info

Output:

create the directory for our domain

2. After creating the directory and providing the appropriate permission for the user directory, in this step, we make the html file for testing purposes; this file is used to serve the content used to display on the web browser when we are calling the browser.

Code:

<html>
<head>
<title> Nginx server block </title>
</head>
<body>
<h1> Nginx server block is allows us to run the multiple website on single machine.</h1>
</body>
</html>

Output:

html file for testing purposes

3. After creating the index.html file, we make the nginx server block in this step. The default server block of the nginx server is serving the default file of html. Therefore, we are creating a server block file as follows.

Code:

  server {
listen 80;
  listen [::]:80;
  root /var/www/nginx.info/html;
index index.html index.htm index.nginx-debian.html;
server_name nginx.info www.nginx.info;
location / {
  try_files $uri $uri/ =404;
}
access_log /var/log/nginx/nginx.info.access.log
error_log /var/log/nginx/nginx.info.error.log;
 }

Output:

creating the index.html file

4. After creating it in this step, we enable the nginx server blocks.

Code:

ln -s /etc/nginx/sites-available/nginx.info /etc/nginx/sites-enabled/

Output:

enable the nginx server blocks

5. After enabling the Block of the nginx server, we need to reboot the nginx server to take the effect of the changed port. Before restarting the nginx server, we execute the nginx –t command to check whether the nginx configuration file contains any error or not.

Code:

nginx –t
service nginx restart

Output:

execute the nginx –t command

Configuration

At the time of using it, we need to use the configuration of encapsulation details.

Below steps shows how to configure it:

1. In the first step, we are setting up the root directories of the root document; by default, ubuntu has enabled the nginx block.

Code:

mkdir -p /var/www/nginx.com/html
mkdir -p /var/www/nginx1.com/html

Output:

setting up the root directories

2. After creating the directory, we change the ownership of the user and group, and also, we are changing the umask value for the specified directory.

Code:

chown -R $USER:$USER /var/www/nginx.com/html
chown -R $USER:$USER /var/www/nginx1.com/html
chmod -R 755 /var/www

Output:

change the ownership of the user and group

3. After defining the directory’s privileges and giving an umask in this step, we create the sample pages for each site. In the below example, we are making the index.html file as follows.

Code:

<html>
<head>
<title> Nginx server block </title>
</head>
<body>
<h1> Nginx server block </h1>
</body>
</html>

Output:

making the index.html file

4. After creating the index.html file, we are making the server block files for every domain as follows. In the below example, we are building two server blocks as follows.

Code:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/nginx.com
cp /etc/nginx/sites-available/nginx.com /etc/nginx/sites-available/nginx1.com

Output:

server block files for every domain

5. After creating the server block in this step, we enable the server block for restarting the nginx server.

Code:

ln -s /etc/nginx/sites-available/nginx.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/nginx1.com /etc/nginx/sites-enabled/

Output:

enable the server block for restarting the nginx server

6. After enabling the server block in this step, we check the configuration and restart the server.

Code:

nginx –t
service nginx restart

Output:

check the configuration and restart the server

7. Now, we are configuring the host file as follows.

configuring the host file

Example of Nginx Server Block

Different examples are mentioned below:

But first, we are creating the directory and permitting it.

Code:

mkdir -p /var/www/block.info/html
chmod -R 755 /var/www/block.info

Output:

creating the directory

1. Create the index.html file.

Code:

<html>
<head>
<title> Nginx block </title>
</head>
<body>
<h1> Nginx server block</h1>
</body>
</html>

Output:

Create the index.html file

2. Creating server block files.

Code:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/block.com

Output:

Creating server block files

3. Enable the server block.

Code:

ln -s /etc/nginx/sites-available/block.com /etc/nginx/sites-enabled/

Output:

Enable the server block

4. Restart the server.

Code:

nginx –t
service nginx restart

Output:

Restart the server

5. Configure the host file.

Configure host file

Conclusion

For installing an nginx server block, records need to be defined in a domain name for the hosting provider. Block of server is referred to as the virtual host of the nginx server and the feature of the nginx server, allowing us to host multiple websites on a single server.

Recommended Articles

This is a guide to Nginx Server Block. Here we discuss the introduction and how to install the Nginx server block with configuration and examples. You may also have a look at the following articles to learn more –

  1. Nginx Add_header
  2. Nginx Location Directive
  3. Nginx WAF
  4. Nginx FastCGI
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more