# How to Send Bulk 410 Headers and 301 Redirects in Nginx

Google's Webmaster Tools warns you when there are a lot of 404 Not Found pages found on your website. If you've just deleted a lot of spam from a site, Google might keep recrawling those URLs as long as they send 404 headers.

If you want to stop Google from adding those removed pages back to Google Webmaster Tools, you can send 410 headers for the removed URLs.

It took me a while to figure out how to do this with nginx, but I finally came up with this solution:

Create a new file called /etc/nginx/header-maps.conf. In that file map each gone URL to a variable, in this case the number 1 (chosen arbitrarily).

map $request_uri$gone_var {
/path-one       1;
/path-two       1;
/path-three     1;
/path-four      1;
}


Then in your main conf file for your domain, like /etc/nginx/sites-available/example.com (replacing example.com with your domain):

include header-include.conf;
server {
# other things here...

if ($gone_var) { return 410$gone_var;
}

# other things here...
}


You can also map redirects in the same file, for example:

# header-include.conf
map $request_uri$gone_var {
/path-one       1;
/path-two       1;
/path-three     1;
/path-four      1;
}

map $request_uri$redirect_uri {
/path-five      /path-five.html;
/path-six       /path-six.html;
/path-seven     /path-seven.html;
}

# Your domain's conf file
server {
# other things here...

# if there is a mapped $gone_var set for the current path if ($gone_var) {
return 410 $gone_var; } # if there is a$redirect_uri set for the current URI:
if ($redirect_uri) { return 301$redirect_uri;
}

# other things here...
}


If you get an error about map_hash_bucket_size being to small, open the /etc/nginx/nginx.conf file (or wherever the http block lives) and increase it something like this: map_hash_bucket_size 256