What is default_server in Nginx
In a server block, when the default_server flag is added on a listen directive, Nginx will declare that server as the default server. After that, the Nginx will utilize the default server for handling the requests when their HTTP Host header remains unmatched with any other server blocks. The default_server flag can only be added once in a server block with any IP:port combination specified as a parameter of the listen directive. However, the default_server flag can be utilized several times on different combinations of IP:port.
Before using default_server in Nginx, you have to install Nginx on your system if you do not have it already!
How to install Nginx in CentOS
Firstly, open up your CentOS terminal by pressing “CTRL+ALT+T” and then write out the below-given command:
The error-free output declares that Nginx is successfully installed on your system.
How to enable Nginx in CentOS
Now, utilize the below-given command for enabling Nginx on the CentOS system:
After that, start the Nginx service:
How to set firewall rules for Nginx in CentOS
The next thing we are going to do is set the firewall settings to permit the external connections for the Nginx, running on port 80 by default. The firewall-cmd is the command that is utilized for managing permanent and runtime firewalld configuration.
For permanently enabling the HTTP connections on port 80, write out the below-given command in your CentOS terminal:
To verify if the HTTP firewall service was correctly added to the system, execute this command:
Now, you have to reload the firewall service:
How to set up servers in Nginx
In the Nginx configuration file, the server configuration directives, such as the server name and TCP port are specified in the server block {}. The listen directive instructs Nginx to listen for HTTP connections at the specified IP and TCP port. When Nginx handles a request, the server name directive instructs it to select a specific server from a list of numerous server blocks.
If you want to set up a virtual server in Nginx, then in your nano editor, open the Nginx configuration file “/etc/nginx/nginx.conf”:
Your Nginx configuration file will look like this:
Your Nginx configuration file should contain at least one server directive for defining a virtual server. While processing a request, Nginx first determines which virtual server is going to handle the request. In the http context, a virtual server is defined by a server directive, such as:
server {
# Server configuration
}
}
In the server block, you can configure all of the settings related to the particular server:
Multiple server directives are added to the http context for defining multiple virtual servers. The “listen” directive in the server configuration block is utilized for adding the IP address and port on which the server listens for the requests. IPv4 and IPv6 addresses are permitted and IPv6 addresses should be added inside of square brackets.
The configuration of a server listening on port 8080 and IP address 127.0.0.1 is shown in the example below:
server {
listen 127.0.0.1:8080;
# Additional server configuration
}
}
How to set up a default server in Nginx
In the Nginx configuration file, the default_server option specifies the default server to which a client request with an unknown domain and an empty host field will be forwarded. For instance, when a client writes out the server IP address into a browser or has many domains, such as linuxhint.com, test1.linuxhint.com, and test2.linuxhint.com, not all of them are mentioned in the Nginx configuration file.
If you have not added the “default_server” parameter to any virtual server, the first server will be considered the default. However, you can explicitly specify a default_server as follows:
listen 80 default_server;
#...
}
Nginx configuration will consider this server as its “default_server”:
You can also add another configuration parameter such as the name of the server and the directory which contains all of the configuration files:
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
}
Save the added lines in the “/etc/nginx/nginx.conf” file by pressing “CTRL+O”:
Now, test the Nginx configuration file and its syntax by executing the “nginx” command with the “-t” option:
After successful testing, restart the Nginx service so that your system will take effect from the added changes:
Conclusion
In the Nginx configuration file, the server block specifies a virtual server for handling specific types of requests. After setting up various server blocks, administrators choose blocks for the connection. This selection is based on the IP address, port, and domain name of the request. However, you can also specify a default_server to handle the incoming requests. In this post, we have explained default_server and how you set up virtual servers, including the default server in Nginx.