Configuring Nginx to Display 404 Page for Unconfigured Domains and Server IP Address

| Servers | 11 seen

In today's digital age, it is crucial for website owners to have a properly configured web server.

This not only ensures a smooth and seamless user experience but also protects the website from potential security threats.

One common issue that website owners face is incorrectly indexed links in search engines like Google. In this article, we will provide a simple guide on how to configure Nginx to display a 404 page for unconfigured domains and your server's IP address.

The first step in this process is to create a dummy or default domain folder. This folder will serve as a redirect for all incorrect requests made to the website. To create this folder, open the terminal and enter the following commands:

sudo mkdir -p /srv/www/default sudo usermod -a -G www-data admin sudo chown -R www-data:www-data /srv/www sudo chmod -R 775 /srv/www

Once the dummy domain folder has been created, the next step is to configure Nginx server blocks for this folder. Open the Nginx configuration file by entering the following command in the terminal:

sudo nano /usr/local/nginx/sites-available/default

In the Nginx configuration file, paste the following code:

server{ listen 80 default; root /srv/www/default; }

Next, enable the domain by creating a symbolic link between the available and enabled folders in Nginx:

sudo ln -s /usr/local/nginx/sites-available/default /usr/local/nginx/sites-enabled/default

The final step is to create a basic HTML index.html file and upload it to the /srv/www/default folder. This file will display the content that will be shown when accessing the server's IP address or an unconfigured domain.

After uploading the index.html file, restart Nginx by entering the following commands in the terminal:

sudo /etc/init.d/nginx stop sudo /etc/init.d/nginx start

Now, try accessing the server's IP address in a web browser. You should see the content from the index.html file that you created.

In conclusion, configuring Nginx to display a 404 page for unconfigured domains and server IP addresses is a simple process that can help to protect your website from potential security threats and ensure a smooth user experience.