{ title: "Test nginx Configuration Directives" blurb: "We use MoonScript and some Lua packages to write tests for the directives in our `nginx` configuration files." } $index ## Introduction [`nginx`](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#rewrite-uris-in-requests) configuration can contain any number of important directives (redirects and rewrites, for example) that need to be verified for correctness. We can write tests for directives and run them against a test server to ensure they are correct. To do this, we'll use... - [MoonScript](https://moonscript.org) and (by extension) [Lua](https://www.lua.org/) programming languages - `nginx` we'll get from [OpenResty](https://openresty.org/en/), a web platform created by Chinese developer, [Yichun Zhang](https://agentzh.org/) - the [Busted testing framework](https://lunarmodules.github.io/busted/) - the Lua package manager, [LuaRocks](https://luarocks.org/) - a fantastic little library, [`luajit-curl`](https://bitbucket.org/senanetworksinc/luajit-curl/src/master/), from Japanese developer [SENA Networks, Inc](https://www.sena-networks.co.jp) - another great library, written by volunteers, [LuaSocket](https://github.com/lunarmodules/luasocket) - our favorite container manager, [Docker Engine](https://docs.docker.com/engine/) ## Setup Since we require LuaRocks, we'll use a Buildpack tag, which comes with it already installed. ```console $ docker pull openresty/openresty:bookworm-buildpack ``` Start a server on `localhost`: ```console $ docker run --rm -it -p 80:80 openresty/openresty:bookworm-buildpack ``` We can visit `localhost` in our browser and we should see the OpenResty splash page.  ## Get `nginx` running First, let's [prepare the directory layout](https://openresty.org/en/getting-started.html#prepare-directory-layout). ```console $ mkdir -p logs/ conf/conf.d/ html/ ``` Next, we copy over [the default `nginx` config file](https://github.com/openresty/docker-openresty?tab=readme-ov-file#nginx-config-files). ```console $ docker run --rm -it -w /opt -v $PWD:/opt openresty/openresty:bookworm-buildpack \ cp /etc/nginx/conf.d/default.conf /opt/conf.d/ ``` Then, we update the root directive in `default.conf`: ::: filename-for-code-block `conf/conf.d/default.conf` ::: ```diff location / { - root /usr/local/openresty/nginx/html; + root /var/www; index index.html index.htm; ``` Now, let's add an index file. ::: filename-for-code-block `html/index.html` ::: ```html