0
0
Nginxdevops~5 mins

Default server handling in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When multiple websites or services run on the same server, the web server needs to know which site to show if a visitor does not specify a site name. Default server handling tells the web server which site to show by default when no specific site matches the visitor's request.
When you host multiple websites on one server and want to control which site visitors see if they use the server's IP address or an unknown domain.
When you want to avoid showing an error page if a visitor types a wrong or missing domain name.
When you want to provide a simple landing page or message for all unmatched requests.
When you want to improve security by not revealing other hosted sites when the domain is unknown.
When you want to log or track requests that do not match any configured site.
Config File - nginx.conf
nginx.conf
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    root /var/www/default;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

This configuration defines a default server block for Nginx.

  • listen 80 default_server; tells Nginx this server block is the default for IPv4 port 80.
  • listen [::]:80 default_server; does the same for IPv6.
  • server_name _; matches any hostname not matched by other server blocks.
  • root /var/www/default; sets the folder where the default website files are stored.
  • location / handles all requests by trying to serve files or returning a 404 if not found.
Commands
Create the directory to hold the default website files.
Terminal
sudo mkdir -p /var/www/default
Expected OutputExpected
No output (command runs silently)
Create a simple index.html file to show a welcome message on the default site.
Terminal
echo '<html><body><h1>Welcome to the default site</h1></body></html>' | sudo tee /var/www/default/index.html
Expected OutputExpected
<html><body><h1>Welcome to the default site</h1></body></html>
Test the Nginx configuration for syntax errors before reloading.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new default server configuration without downtime.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Check that the default site is served when accessing the server without a specific hostname.
Terminal
curl -s http://localhost
Expected OutputExpected
<html><body><h1>Welcome to the default site</h1></body></html>
Key Concept

If you remember nothing else from this pattern, remember: the default server block catches all unmatched requests and shows a fallback site or message.

Common Mistakes
Not setting the 'default_server' flag on any listen directive.
Nginx will not know which server block to use as default, so unmatched requests may cause errors or unexpected sites to show.
Add 'default_server' to the listen directive in the server block you want as default.
Using an incorrect or missing root directory for the default server.
Nginx cannot find the files to serve, resulting in 404 errors.
Ensure the root path exists and contains an index.html or other valid files.
Not testing the Nginx configuration before reloading.
Syntax errors can cause Nginx to fail to reload, leading to downtime.
Always run 'nginx -t' to verify configuration before reloading.
Summary
Create a server block with 'default_server' in the listen directive to catch unmatched requests.
Set a valid root directory with an index.html file to serve a default page.
Test the Nginx configuration with 'nginx -t' before reloading to avoid errors.
Reload Nginx to apply changes without downtime.
Verify the default site by accessing the server without a specific hostname.