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

Urldecode string (LUA)
FFMPEG Convert YUV444p to YUV420p (BASH)
Forcing FFMPEG to honour segment length in HLS Stream Creator (BASH)
Force CURL to place request to a specific IP (BASH)
Set a variable only if it is undefined (Javascript)
Ignore query string with Nginx caching proxy (NGinx)
Get breakdown of UK and non-UK votes for a Parliamentary Petition (Python)
Decompress Mozilla Firefox jsonlz4 bookmark backup files (Python)
Setting Git author details for a single repo (Git)
Checking Virtual Machine Resource Allocations with Libvirt (BASH)

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