Handle Google Verification files within NGinx Configuration (NGinx)

When adding a site to Google's Webmaster tools (or analytics etc), they'll usually ask you to publish a verification file - and to leave it there.

I run a distributed edge, which means a request back to origin whenever Google decides to check my verification file is still there - not a massive overhead, but still feels wasteful. I handle select other resources at the edge too, so decided to have NGinx generate a response directly rather than fetching a file from origin

This snippet shows how to generate a HTTP 200 response and set the body content from with NGinx's configuration - you can do this for any file, but I'd only ever use it for very small static files personally

Details

Snippet

location /[verification filename] {
        return 200 '[file content]\n';
}

Usage Example

server {
    listen       80;
    server_name  www.example.invalid; 
    root /var/www/html;

    location /googleabcdef1234567890.html {
            return 200 'google-site-verification: googleabcdef1234567890.html\n';
    }

    location / {

        # I'd actually proxy back to origin here, but to keep the example 
        # concise, look for the file on the filesystam
        try_files $uri $uri/ =404;
    }

}