Multiple Block Server With Same Port In Nginx Configuration
Introduction to Multiple Server Block
Nginx is an extremely powerful and useful web server. One of its most powerful features is its ability to run multiple server blocks on the same port with different domain names. This feature allows developers to create multiple server blocks for different websites, without changing the underlying port configurations. Doing so can greatly reduce the configuration overhead and make maintenance easier. In this article, we will discuss how to configure multiple server blocks with same port in Nginx.
Configure Multiple Server blocks with same port
The first step in configuring multiple server blocks with same port is to open the Nginx configuration file. This file is usually located in the /etc/nginx/ directory, and its name is usually nginx.conf. From within the file, you will need to define two server blocks with different domains for each one. The syntax should look something like this:
http {
server {
listen 80;
server_name www.example1.com;
...
}
server {
listen 80;
server_name www.example2.com;
...
}
}
The above code shows two server blocks, each one listening on port 80, with a different domain name defined for each one. Now it’s time to configure the actual server blocks. This is done by adding directives inside each of the two server blocks.
For example, you can set up different document root directories for each server block, as follows:
http {
server {
listen 80;
server_name www.example1.com;
root /var/www/example1;
...
}
server {
listen 80;
server_name www.example2.com;
root /var/www/example2;
...
}
}
Here, the first server block has a document root directory of /var/www/example1, while the second server block has a document root directory of /var/www/example2. Depending on the domain name requested by the client, Nginx will serve files from the corresponding directory.
Apart from setting up the document root directory, you can also configure other settings for each server block, such as the proxy_pass directive, the rewrite rules, and so on.
Once you have finished configuring the server blocks with the desired settings, you should save the configuration file and reload or restart Nginx to activate the changes.
Nginx Server Block Parameters
In addition to configuring the document root directory, you can also configure other more specific settings for each server block. These settings are specified by making use of several server block parameters, such as:
- server_name: this is the main parameter used to differentiate the server blocks. It defines the domain name that will be associated with the given server block.
- listen: this sets the port number on which Nginx listens for incoming requests. By default, it is set to port 80.
- root: this is the document root directory. It defines the directory where Nginx will look for files requested by clients.
- proxy_pass: this is used to set up a proxy server, which can be used to forward requests to another server.
- location: this is used to set up rules for handling requests for specific locations in the file system.
- rewrite: this is used to set up URL rewriting rules in order to redirect requests to different pages.
These parameters can be used to further customize the configuration for each server block.
FAQs
Q1. How does Nginx decide which server block to respond to?
Nginx uses the server_name parameter to decide which server block to respond to. If a request matches the domain specified in the server_name parameter, Nginx will respond with the settings found in that server block. This means that different settings can be configured for different domain names.
Q2. Do I need to restart Nginx after making changes to the configuration file?
Yes, you need to restart Nginx after making changes to the configuration file in order for these changes to take effect.
Q3. Can I run multiple server blocks on different ports?
Yes, it is possible to run multiple server blocks on different ports. The syntax for this is slightly different from the one used for multiple server blocks on same port.
Conclusion
In this article, we have discussed how to configure multiple server blocks with same port in Nginx. We have also covered some of the parameters used to further customize the server blocks, such as the listen, root, proxy_pass, location, and rewrite parameters. Finally, we have also briefly answered some common questions related to this topic.
Thank you for reading this article. Please read our other articles for more information about Nginx configurations.
Related Posts:
- Ubuntu 16 Nginx Subdomain Refused To Connect Ubuntu 16 Nginx Subdomain Refused To Connect What is a Nginx Subdomain? An Nginx subdomain is a domain created under a main domain name and hosted on a web server…
- Nginx Static Location For Multiple Django Nginx Static Location For Multiple Django What is Nginx? Nginx is an open source web server written in C that is well-known for its performance and speed. Nginx is one…
- Forward Nginx To Another Subdomain Forward Nginx To Another Subdomain What is Nginx? Nginx is an open-source, high-performance web server originally developed by Igor Sysoev. Since its initial release in 2004, Nginx has become one…
- Listen Directive In Www.Conf Nginx Listen Directive In WWW.conf Nginx What is a Listen Directive? The listen directive is a configuration directive that informs the Nginx server to accept requests on a network port. It…
- Start Nginx Service Centos 7 Start Nginx Service Centos 7 Before You Start: Server and Requirements If you are running a website or a web application on Centos 7, chances are you will be using…
- Setup Virtual Hosts In Nginx Setup Virtual Hosts In Nginx Understanding The Virtual Hosts Concept The Virtual Hosts concept is an important part of installing Nginx. It allows you to host multiple websites on a…
- Nginx Centos 7.6 Virtual Host Nginx Centos 7.6 Virtual Host Introduction to Nginx Virtual Hosts Virtual Hosts, also called Virtual Servers, are a very important function of web hosting. They allow multiple websites to run…
- Nginx Rewrite Deny Access Except Nginx Rewrite Deny Access Except What is Nginx Rewrite Rules? Nginx rewrite rules are a powerful tool for customizing your website's behavior. When a request comes in, Nginx will check…
- Nginx Proxy_Pass Multiple Locations Nginx Proxy_Pass Multiple Locations What is the Nginx Proxy_Pass Directive? The Nginx Proxy_Pass directive is a configuration setting in the Nginx web server software that allows the server to properly…
- Configure Nginx As Proxy Server Configure Nginx As Proxy Server Introduction Nginx is a software application used for serving dynamic web pages and web content. It is an open source, lightweight and highly modular web…
- Not Redirect To Https With Nginx Reverse Proxy And Certbot Not Redirect To Https With Nginx Reverse Proxy And Certbot Introduction HTTPS is the secure version of the HTTP (Hypertext Transfer Protocol) that encrypts communication over the web. Having an…
- Nginx Configure Multiple Sites Differen Port Nginx Configure Multiple Sites Different Port Introduction to Nginx Nginx is a web server software developed by Igor Sysoev in 2002. It’s a high performance web server with a large…
- Nginx Stream Port Forwarding With Domain Nginx Stream Port Forwarding With Domain Introduction Port Forwarding is usually used to allow traffic from the Internet to reach the internal network. To achieve this, port forward requires router…
- Nginx 1.10.2 Eror NGINX 1.10.2 Eror What is Nginx? Nginx is a high-performance web server used to serve static content, host websites, provide reverse proxying services, and run web applications. It has become…
- How To Setting Nginx For Codeigniter How To Setting Nginx For CodeIgniter What is CodeIgniter and How Does it Work? CodeIgniter is a powerful PHP web programming platform. This open source software framework is greatly preferred…
- How To Config Nginx Upstream On Centos 7 How To Config Nginx Upstream On Centos 7 What is Nginx? Nginx is an open source, high performance web server and reverse proxy developed by Igor Sysoev in 2004. It…
- Mac Os X Nginx Conf Location Mac OS X Nginx Conf Location What is Nginx? Nginx is a high-performance web server and reverse proxy originally written for Linux, but now it’s also available for Mac OS…
- Ubuntu Install Web Server Nginx Ubuntu Install Web Server Nginx Step 1: Install the Nginx Package The first step when installing Nginx on Ubuntu is to install the Nginx package from the Ubuntu repository. This…
- How To Add Domain In Nginx How To Add Domain In Nginx Introduction To Nginx Nginx (Internet Information Server) is a popular open-source web server that is able to serve a variety of content, from simple…
- Install Nginx And Php Scract In Docker Install Nginx And PHP Script in Docker What is Docker? Docker is a popular platform for creating, running, and managing applications in a lightweight container system. Originally released as an…
- Laravel Nginx Default Multiple Site Laravel Nginx Default Multiple Site What is Nginx? Nginx is a popular open source web server used for hosting websites on the internet. It is designed for high-traffic websites and…
- Nginx Grant Access To Port Nginx Grant Access To Port Introduction Nginx is an open source web server that is incredibly popular, fast, and easy to use. It is used for hosting websites and applications,…
- Nginx Php Fpm 7.2 Laravel Nginx Php Fpm 7.2 Laravel Introduction to Nginx Nginx (pronounced “engine-x”) is a free, open-source web server software. It’s quickly becoming one of the most popular web servers, used by…
- Nginx Listen Port 8080 With Ssl Nginx Listen Port 8080 With SSL Understanding Nginx Nginx (pronounced Engine-X) is a high-performance web server that is used for serving static content such as images, stylesheets and JavaScript. It…
- Nginx Forward Multiple Port To One Tcp Nginx Forward Multiple Port To One Tcp Overview Nginx and How It Works Nginx is a web server software used to host websites and web applications. It is an open…
- Nginx Emerg Bind To 0.0.0.0 5601 Failed 13 Permission Denied Nginx Emerg Bind to 0.0.0.0 5601 Failed 13 Permission Denied What is Nginx? Nginx is an open source web server created by Igor Sysoev in 2004. It can function as…
- Nginx Server Blocks Doesn't Work Nginx Server Blocks Doesn't Work What is Nginx Server Blocks? Nginx Server Blocks are a way of configuring the Nginx web server to match specific domains and serve different content…
- Perfect Server Ubuntu 18.04 Nginx Perfect Server Ubuntu 18.04 Nginx 1. Introduction Ubuntu is one of the most popular Linux distributions. It is popular due to its user friendliness and availability of excellent free software…
- Virtualhost Nginx Ubuntu 16.04 Virtualhost Nginx Ubuntu 16.04 Introduction to Virtualhost Virtualhost is a software configuration option in web servers including Apache, Nginx, and more that allows a web server to host multiple web…
- Setting Serverblock For Domain Using Nginx On Ubuntu 18.04 Setting ServerBlock For Domain Using Nginx On Ubuntu 18.04 Introduction Nginx is a powerful open-source web server that can be used for serving static, dynamic websites and applications. Nginx is…