How it works...

We start by loading the CJSON and Lua OpenResty DNS modules and initiating the DNS module by setting the nameservers to 8.8.8.8 (Google's free open resolver).

Then, we parse the GET argument, named domain. Through the NGINX API, Lua can call this directly via the domain name. If the get variable you wanted was named shop, you could have called it via ngx.var.arg_shop.

This is then validated by ensuring the variable is set (for example, the GET argument was passed) and then checking for a basic domain. The formats of the regular expressions within Lua are slightly different to the more common Perl-Compatible Regular Expressions (PCRE), but the concept remains the same. We ensure that the domains start with alphanumeric characters (using %w); they should contain at least one dot (.) and alphanumeric characters. While it's not a perfect validator, the advent of all the new Top-Level Domains (TLDs) has made this considerably harder to do.

After ensuring the domain is valid, we run a query, specifying the query type to be MX. If we receive a result, this is encoded via the CJSON module to return the results as JSON code.