# Setting Up a Custom 404 Page on Nginx

> A short guide on how to set up a custom 404 page on Nginx.

- URL: https://zeljic.com/blog/setup-nginx-404-page/
- Author: Đorđe Zeljić
- Published: 2017-05-05
- Updated: 2022-08-24
- Tags: nginx, unix, 404

A 404 page is a standard response code in HTTP telling the user, in effect, that they've clicked on a broken link. Setting up a custom 404 page on Nginx is a simple and straightforward process.

## Create Your 404 Page
Design a custom `404.html` page and save it in your website's root directory.

For example `/var/www/your-site/public_html/404.html`.

## Edit Nginx Configuration
Open your Nginx configuration file, commonly found at one of the following locations:
- `/etc/nginx/nginx.conf`
- `/etc/nginx/sites-available/your-site.conf`.

Open file with your favorite text editor and add the following lines:

```nginx
server {
	...
	error_page 404 /404.html;
	location = /404.html {
		root /var/www/your-site/public_html;
		internal;
	}
	...
}
```

*Replace `/var/www/your-site/public_html` with your website's root directory.*

### Test Configuration
Be careful when editing your Nginx configuration file.
A single typo can cause your website to go down.

Run `sudo nginx -t` to check for syntax errors in your configuration.

### Reload Nginx
If the test is successful, reload Nginx with `sudo service nginx reload`.

## Done!
Your custom 404 page is now set up on Nginx. Test it with a non-existent URL.</code>
