Handle Google Verification files within NGinx Configuration



Published: 2018-09-11 14:19:42 +0000
Categories: NGinx,

Language

NGinx

Description

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

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;
    }

}

License

BSD-3-Clause

Keywords

nginx, content, generate, HTTP,

Latest Posts


Copyright © 2019 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, yr4pnhounvdybotb.onion and snippets.6zdgh5a5e6zpchdz.onion