Profile photo

from miller import blake

Hello, I'm a Seattle-based site reliabililty engineering manager, I get paid to do what I love, I like Python, I'm in an abusive relationship with JavaScript, I'm a fan of good design, and I don't think things always have to be so stupid.
You can follow me @bltmiller, subscribe via RSS, and email me.

How to Smoke Test Nginx Server Block Routes

When your Nginx routes start getting complex it can be time-consuming to makes heads or tails of the configs. I wanted an easy and automated way to make sure that my Nginx server blocks were routing correctly to my backend apps/websites and replying with the right response code, a simple 200 OK in most cases would suffice. Call it functional testing if you like. So I whipped up a simple Python script to make it happen.

You need PyYAML and Requests in order to use this; it assumes that Nginx is listening on standard HTTP (80) and/or HTTPS (443) ports; and it should be run on the same server on which Nginx is running (localhost):

This works by reading a simple YAML file for determing which URLs to test. The YAML file is a dictionary of lists where the key of each list is the server name to be tested and the indices of each list are URIs to be tested. The following example (endpoints.yaml) would test example.com/alice, example.com/bob/marley, example.com/charlie, example.org/david, and example.org/:

example.com:
  - alice
  - bob/marley
  - charlie
example.org:
  - david
  - ""

Essentially, the Python script is the command line equivalent to:

# get http://example.com/alice from localhost and only print out the URL and status code
curl -H 'Host: example.com' -w "%{url_effective} - %{http_code}" -s -o /dev/null http://localhost/alice

and now, running the Python script gives you an at-a-glance look at how things are lined up:

$ python smoke.py endpoints.yaml
200 http://example.org/david
200 http://example.org/
304 http://example.com/alice
404 http://example.com/bob/marley
200 http://example.com/charlie

So I can quickly see that there might be something funky going on with Alice and Bob up there. It also supports HTTPS requests by passing the --proto=https option:

$ python smoke.py endpoints.yaml --proto=https
200 https://example.org/david
200 https://example.org/
304 https://example.com/alice
404 https://example.com/bob/marley
200 https://example.com/charlie

or if you want to test both, pass the --proto=all option.


comments powered by Disqus

Copyright © 2020, Blake Miller. All rights reserved. | Sitemap ยท RSS