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:
- Setting Ip And Port Nginx Setting IP and Port Nginx What is Nginx? Nginx is a web server that is used to host websites and applications. It is designed to be efficient, reliable, fast and…
- Nginx Listen Multiple Ip Addresses Nginx Listen Multiple Ip Addresses What is Nginx? Nginx is a open-source web server created by Igor Sysoev and first publicly released in 2004. Since its release, Nginx has become…
- 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…
- 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 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…
- Reverse Proxy Nginx Php Node Js As Same Port Reverse Proxy Nginx Php Node Js As Same Port What is Reverse Proxy? Reverse proxies are a type of software which allows a system to make an indirect connection between…
- Selinux Enable Php Fpm Nginx Centos 7 Selinux Enable Php Fpm Nginx Centos 7 What is Selinux Enable Php Fpm Nginx Centos 7? Selinux Enable Php Fpm Nginx CENTOS 7 is an easy-to-use web server and operating…
- Nginx One Port Multiple Backend Nginx One Port Multiple Backend Introduction Nginx is one of the most popular web servers used today. It is a powerful, reliable and efficient web server. Nginx is also able…
- 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…
- Nginx Redirect Http To Https With Port Nginx Redirect HTTP To HTTPS With Port Understanding the Basic Terms, Nginx and Redirection Nginx is a popular open source web server commonly used for hosting static web content, providing…
- Nginx Port 3000 To 80 Digitalocean Nginx Port 3000 To 80 Digitalocean What is Nginx? Nginx is an open source web server that is designed to provide a better experience when hosting a website. It is…
- Nginx Proxy To Port 8080 Nginx Proxy To Port 8080 What is Nginx? Nginx is an open-source, high-performance web server developed in 2002 by Igor Sysoev and released publicly in 2004. It is a very…
- 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…
- 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…
- 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…
- 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…
- Nginx No Port In Upstream NGINX No Port In Upstream What Is an Upstream in Nginx? An "upstream" in Nginx is a server or group of servers that accept requests and pass them on for…
- Nginx Tcp Multiple Port Forwarding Nginx Tcp Multiple Port Forwarding What is TCP Port Forwarding? TCP port forwarding is a network action that enables a computer to redirect communications that are normally sent over the…
- Easyphp Httpd.Conf Location Nginx Easyphp Httpd.Conf Location Nginx What is Nginx? Nginx is an open source web server and proxy server that is mainly used for serving static content and also provides reverse proxying…
- Virtual Host Nginx Ubuntu 16.04 Virtual Host Nginx Ubuntu 16.04 Introduction A virtual host (also known as Virtual Private Server or VPS) is a service that allows a single physical server to host multiple websites.…
- Nginx Failed Address Already In Use Nginx Failed Address Already In Use What is Nginx? Nginx is an open-source web server and proxy service used for hosting webpages and other services. It is built to provide…
- How To Install Nginx On Debian 10 How To Install Nginx On Debian 10 Overview In this guide, we will show how to install Nginx on a Debian 10 server. Nginx (pronounced “engine-x”) is an open source…
- Docker Compose Nginx Reverse Proxy Docker Compose Nginx Reverse Proxy Introduction to Docker Compose and Nginx Docker Compose is a powerful tool used for automating the deployment of application services using multiple Docker containers. It…
- Subdomain Nginx Timeout During Connect Likely… Subdomain Nginx Timeout During Connect Likely Firewall Problem What Is a Subdomain in Nginx? A subdomain in Nginx is a domain that exists under a primary domain that typically has…
- Nginx Multiple Domains Same Port Only Get One Domain Nginx Multiple Domains Same Port Only Get One Domain Understanding the Problem You just recently set up your Nginx web server and you're trying to set up multiple domains on…
- Nginx Responding To Any Domain Name Nginx Responding To Any Domain Name Understanding Nginx Nginx (pronounced “Engine X”) is a free, open-source, high-performance web server designed for better performance and scalability. It is used to serve…
- Nginx Proxy_Pass Tcp Connection Nginx Proxy_Pass Tcp Connection What is the Nginx Proxy_Pass Module? Nginx Proxy_Pass is an open source web server that is widely used in the development of websites. It is a…
- Where Default Root Folder Nginx Where Default Root Folder Nginx? What Is Nginx Nginx is an open source web server designed for high concurrency, performance and low memory consumption. The official website defines it as…
- 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 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…