How to set Nginx to return 410 (Gone) HTTP response status code for certain pages?

Usually, a normal working website returns 200 (OK) HTTP response status code. If a page doesn't exist a browser will get 404 (Not found) code. If you have deleted some pages that were already indexed by Google search engine you will be getting crawl errors in your search console. That may have a negative effect on search results with relative to your website keywords, so you can lose some traffic in the future. To fix this issue you can use 410 (Gone) HTTP response code, which means that certain page doesn't exist anymore. That will tell crawl robot to mark this a page as gone and that particular address will not be used anymore.

What is Nginx?

Nginx — is a free open source web server created to serve high-loaded projects. Almost 24% of all websites are working under Nginx to the moment. The web server runs under Linux-based operation systems like Ubuntu Server. To start we need to have an access to a command line terminal of the target server. Usually, that is done with an SSH connection. Here is an example of the command that allows to remotely connect to a VPN server:

ssh username@121.129.23.141

Replace username with your user, for example, root. Replace 121.129.23.141 with an IP-address of your remote server.

Where are config files located?

The config files of Nginx in Ubuntu Server are usually located under /etc/nginx/ directory. General settings are stored in /etc/nginx/nginx.conf. Each website has it's own config file. They are located in /etc/nginx/sites-available/ directory. For example, our website is configured with /etc/nginx/sites-available/godmodeuser-com file. You can start editing a config file with the command:

sudo nano /etc/nginx/sites-available/yourwebsite-name

Type in a root password and press Enter. If you are not sure about the name of a config you may list all available files with the command:

ls /etc/nginx/sites-available

Setting up the config to return 410 code

First, you need to have a list of all deleted web pages you want to set to the 410 (Gone) status. Next, find a place between an index and a first location commands. Now insert location command like this for each URL:

location /some-deleted-page-url { return 410; }

If you want to remove a file just add an extension:

location /documents/study.pdf { return 410; }

If you want to remove a range of pages, for example, all pages under /forum/ (/forum/viewtopic.php?f=3&t=66, /forum/viewforum.php?f=18, /forum/memberlist.php etc):

location ^~ /forum { return 410; }

The example of a production config file:

Example of a working Nginx config file

Please note the part of the config marked with green color. That part uses all commands that were explained above. Now save config file and exit back to the command line. If you use Nano editor press Ctrl + X and press Y to confirm saving.

How to restart Nginx web server?

Before we can see changes in a browser we need to reload config files:

sudo nginx -s reload

Type in a root password and press Enter. The reload signal will be sent to a Nginx background process. New config files will be loaded.

How to check an HTTP response code?

Go to a URL of the deleted page. You should see 410 (Gone) response status code:

Nginx HTTP response status code in Google Chrome

This page is generated by Nginx, but that is just an HTML code. To check an HTTP response status code you need to use a third-party extension. In Google Chrome you may use HTTP Headers extension. Install the extension and reload the page. Now click on the extension icon:

HTTP Headers extension for Google Chrome


28 april 2018 19 july 2018 1263


More like this

Quakespasm. User manual. How to install, setup and play Quake 1

How to remove a color banding (steps) and smooth a gradient in Photoshop?

How to batch rename multiple files in Windows?

How to play Terraria multiplayer with Steam friends

How to switch back to an old «New document» window in Adobe Photoshop CC 19?


Comments