This guide walks you through replacing the default Nginx welcome page with your own website files. You’ll edit the Nginx server block configuration to point to your site’s directory, then reload Nginx to apply the change. You’ll need basic command-line familiarity and access to your server’s files.
What You’ll Need
- A server (or local machine) with Nginx installed and running.
- Root or sudo access to edit configuration files and manage the Nginx service.
- Your website files (HTML, CSS, JavaScript, images, etc.) placed in a directory on the server (e.g.,
/var/www/mysite). - A text editor (like nano, vim, or a GUI editor) to modify the configuration.
Step-by-Step
- Locate your website files. If you haven’t already, upload or copy your site’s files to a directory on the server. A common location is
/var/www/mysite. Ensure the directory has read permissions for the Nginx user (usuallywww-dataon Debian/Ubuntu,nginxon RHEL/CentOS). - Find the Nginx configuration file. The main configuration is usually at
/etc/nginx/nginx.conf, but the site-specific settings are typically in a separate file. On Debian/Ubuntu, look in/etc/nginx/sites-available/(the default site is oftendefault). On RHEL/CentOS, check/etc/nginx/conf.d/or the mainnginx.conffor aserverblock. - Open the configuration file for editing. Use a command like
sudo nano /etc/nginx/sites-available/default(adjust path as needed). - Edit the
serverblock. Inside the configuration, find theserverblock that handles your domain or IP. Look for therootdirective. Change its value to the path of your website directory. For example:root /var/www/mysite; - Set the index file (optional). Ensure the
indexdirective includes your main file, usuallyindex.htmlorindex.php. For example:index index.html index.htm; - Remove or comment out the default welcome page location. In the default configuration, there is often a
location /block that points to the default Nginx page. You can either delete that block or comment it out by adding#at the beginning of each line. If you’re using a custom configuration, this step may not be necessary. - Test the configuration. Before reloading, run
sudo nginx -tto check for syntax errors. You should see a message like “syntax is ok” and “test is successful”. - Reload Nginx. Apply the changes by running
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). This reloads the configuration without dropping connections. - Verify the result. Open your browser and visit your server’s IP or domain. You should see your website instead of the default Nginx welcome page. If you don’t, check the troubleshooting section below.
Common Mistakes
- Wrong root path: Typing the path incorrectly (e.g., missing a leading slash) will cause a 404 error.
- Forgetting to set index: If your main file is not named
index.htmland theindexdirective doesn’t include it, Nginx may show a directory listing or a 403 error. - Editing the wrong server block: If you have multiple server blocks, you might edit the one for a different domain. Check the
server_namedirective. - Not testing before reload: A syntax error in the configuration can prevent Nginx from restarting, taking your site down.
- File permissions: If the Nginx user cannot read your website files, you’ll get a 403 Forbidden error. Ensure the directory and files are readable.
Tips
- Use a separate configuration file for each site (e.g.,
/etc/nginx/sites-available/mysite) and enable it with a symlink tosites-enabledfor easier management. - Always back up your configuration before making changes:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak - If you’re using PHP, add a
location ~ \.php$block to pass PHP requests to PHP-FPM. - Consider using
sudo nginx -Tto dump the full effective configuration and verify your changes are applied. - Clear your browser cache after reloading to avoid seeing the old page.
Troubleshooting
You still see the default welcome page. This usually means the configuration wasn’t reloaded, or you edited the wrong file. Run sudo nginx -T to see the active configuration and check the root directive. Also, ensure you’re visiting the correct URL (the one matching server_name).
You get a 403 Forbidden error. This indicates a permissions problem. Check that the directory and files are readable by the Nginx user. You can test with sudo -u www-data ls /var/www/mysite. If not readable, change permissions with sudo chmod -R 755 /var/www/mysite and sudo chown -R www-data:www-data /var/www/mysite (adjust user/group to match your Nginx user).
You get a 404 Not Found error. The root path is likely incorrect, or the index file is missing. Double-check the root path and that your index.html exists. Also, ensure the index directive includes the correct filename.
Nginx fails to reload. If sudo nginx -t reports an error, fix the syntax before reloading. Common issues include missing semicolons, mismatched braces, or invalid directives.
Frequently Asked Questions
Where is the default Nginx welcome page located?
The default page is usually at /usr/share/nginx/html/index.html on Debian/Ubuntu, or /usr/share/nginx/html/ on RHEL/CentOS. The configuration points to this directory, so changing the root directive is enough.
Do I need to restart or reload Nginx?
Reload is sufficient and safer. It applies the new configuration without dropping active connections. Restart is only needed if you change the main nginx.conf in a way that requires a full restart (rare).
Can I have multiple websites on one Nginx server?
Yes. Create separate server blocks (or separate configuration files) for each domain, each with its own root and server_name. Enable them by including the files or symlinking to sites-enabled.
What if my site uses a framework like React or Vue that needs a build step?
Build your site locally (e.g., npm run build) and upload the resulting static files (usually in a dist or build folder) to your server. Point the root to that folder.
How do I change the port Nginx listens on?
In the server block, change the listen directive. For example, listen 8080;. Then access your site via http://your-server:8080. Make sure the port is open in your firewall.
Why do I see a directory listing instead of my site?
This happens when the index directive doesn’t include your main file, or the file is not present. Add index index.html; (or your filename) to the location / block, and ensure the file exists.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It