Search Our Database
How to change the default file for your website (Nginx)
Introduction
This guide is for website administrators and developers using the Nginx web server who want to change the default landing page of their website. By default, Nginx looks for files like index.html or index.php when visitors access your domain. However, you may want to set a different file as the default. This guide explains how to modify the Nginx server block configuration to define a new default page.
Prerequisites
- Access to the Nginx server configuration (root or sudo privileges)
- Basic knowledge of Nginx and server block configuration
- A working website hosted on an Nginx server
Step-by-Step Guide
1. Access your server via SSH with root or sudo privileges:
ssh root@your_server_ip
2. Navigate to the Nginx configuration directory. The main configuration file is usually located in /etc/nginx/nginx.conf, but the virtual host configuration file (server block) for your domain is typically found in /etc/nginx/sites-available/your_domain.
3. Open the server block configuration file for your domain in a text editor. For example:
sudo vi /etc/nginx/sites-available/your_domain
4. Find the location block where the root directory is defined. This is typically within a section that looks like this:
server { listen 80; server_name your_domain.com www.your_domain.com; root /var/www/your_domain; index index.html index.htm index.php; }
5. Modify the index directive to include the file you want as the default. For example, if you want to set default.html as the default file, update the index line as follows:
index default.html index.html index.htm index.php;
6. Save the configuration file in the text editor by using the command below:
:wq
7. Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
8. If the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
Conclusion
By following these steps, you can easily change the default file that loads when users visit your website on an Nginx server. If you encounter any issues or need additional assistance, please contact our support team at support@ipserverone.com.