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 multiple server_name
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 multiple server_name

Introduction to Nginx multiple server_name

Nginx multiple server_name is defined by the server name’s directive and determines which server block is used for this request. We can define the nginx server name using wildcard names, exact names, or regular expressions. While searching a virtual server by using its name, if the name matches more than one variant, e.g., both regular expression and name of a wildcard will be matching.

Nginx multiple server_name

Overview of Nginx multiple server_name

Multiple server_name is hosted on nginx by using a block of the server. If we choose VPS as our hosting platform, all the websites will reside on a single server; also, it will give us the flexibility to take control of ourselves. Nginx is the web server used as a load balancer, reverse proxy, and http cache. We can do all the hosting configuration by using configuration files; we need to create two separate configuration files to specify each server name. Nginx lookup starts from the block of http, through a block of one or more servers, followed by a location directive.

How to Set Nginx multiple server_name?

The below steps show how to set the multiple server_name of nginx. But first, we are installing the nginx server.

1. In the first step, we need to install the nginx server in our system. We can install the nginx server by using rpm, and source, as well as by using the binary installation.

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)

Code:

apt-get install nginx

Output:

install the nginx server

2. After installing the server of nginx, we need to start the nginx server by using the “service nginx start” command. After starting the nginx service, we can check the status of the service. We have installed the nginx version as 1.18 as follows.

Code:

service nginx status
nginx –V

Output:

service nginx start

3. After creating the directory and providing the umask value to the same now, we are opening the nginx configuration file to add the configuration of the nginx autoindex as follows.

add the configuration of the nginx autoindex

4. Now, we are creating the web root directory for the first domain. In this step, we create a directory and the configuration file.

Code:

mkdir -p /var/www/server1/html
vi /etc/nginx/sites-available/server1.conf

Output:

create a directory and the configuration file

5. After creating the web root directory, we add the server configuration for the first domain. In the below example, we added the listen port as 80 and defined the first server name as server1.com. The directive of root will specify the root location for our website.

Code:

server {
  listen 80;
  listen [::]:80;
  server_name server1.com www.server1.com;
  root /var/www/server1.com/html;
  index index.html index.htm;
  location / {
  try_files $uri $uri/ =404;
 }
}

Output:

first server name as server1.com

6. We are now creating the second domain’s web root directory. In this step, we create a directory and the configuration file.

Code:

mkdir -p /var/www/server2/html
vi /etc/nginx/sites-available/server2.conf

Output:

second domain's web root directory

7. After creating the web root directory of the second domain now, we are adding the server configuration for the second domain as follows. In the below example, we are adding the listen port as 80 and defining the second server name as server2.com.

Code:

server {
  listen 80;
  listen [::]:80;
  server_name server2.com www.server2.com;
  root /var/www/server2.com/html;
  index index.html index.htm;
  location / {
  try_files $uri $uri/ =404;
 }
}

Output:

server name as server2.com

8. After adding the configuration of the first and second domains in this step, we restart the nginx server.

Code:

service nginx restart

Output:

restart the nginx server

Use of multiple server Block

The Nginx server contains multiple server blocks. While searching the virtual server by its name, suppose we choose by using the following order of precedence.

  • Exact name
  • The name of the longest wildcard starts from the asterisk, e.g., “*.server.com”.
  • The name of the longest wildcard is ended with an asterisk, e.g., “mail.com.”
  • First regular expression.

The wildcard name contains the asterisk at the time of the name starting and ending. We can specify multiple names by using regular expressions. The regular expression we are using by nginx was compatible with those using the programming language of Perl. When using a regular expression with multiple server blocks, the server’s name must start with the tilde character.

If suppose the server name does not start with the tilde character, then it will be treated as the exact name or the expression containing the asterisk or wildcard. The below example shows the use of multiple server blocks as follows.

Code:

http {
      index index.html;
server {
       server_name www.server1.com;
       access_log logs/server1.access.log main;
       root /var/www/server1.com/htdocs;
}
server {
       server_name www.server2.com;
       access_log logs/server2.access.log main;
       root /var/www/server2.com/htdocs;
       }
}

Output:

use of multiple server blocks

How to Configure Nginx multiple server_name?

Below steps shows how we can configure the nginx multiple server_name as follows:

1. First, we create the web root directory for the first domain. In this step, we create a directory and the configuration file.

Code:

mkdir -p /var/www/domain1/html
vi /etc/nginx/sites-available/domain1.conf

Output:

create a directory and the configuration file.

2. After creating the web root directory now, we are adding the server configuration for the first domain as follows.

Code:

server {
        listen 80;
        listen [::]:80;
        server_name domain1.com www.domain1.com;
        root /var/www/domain1.com/html;
        index index.html index.htm;
        location / {
   try_files $uri $uri/ =404;
 }
}

Output:

server configuration for the first domain

3. We are now creating the second domain’s web root directory. In this step, we create a directory and the configuration file.

Code:

mkdir -p /var/www/domain2/html
vi /etc/nginx/sites-available/domain2.conf

Output:

create a directory and the configuration file

4. After creating the web root directory of the second domain now, we are adding the server configuration for the second domain as follows.

Code:

server {
        listen 80;
        listen [::]:80;
        server_name domain2.com www.domain2.com;
        root /var/www/domain2.com/html;
        index index.html index.htm;
        location / {
    try_files $uri $uri/ =404;
  }
}

Output:

server configuration for the second domain

5. Restart the nginx server.

Code:

service nginx restart

Output:

Restart the nginx server

Conclusion

Nginx lookup starts from the block of http, through a block of one or more servers, followed by a location directive. Nginx multiple server_name is defined by the server name’s directive and determines which block of the server is used for this request.

Recommended Articles

This is a guide to Nginx multiple server_name. Here we discuss the introduction, how to set nginx multiple server_name? Use & configure multiple server blocks. 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