{ title: "Test nginx Configuration Directives" blurb: "Write tests for `nginx.conf` directives and run them against a test server." } $index ## Introduction [`nginx`](https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#rewrite-uris-in-requests) config file `nginx.conf` can contain any number of important directives (redirects and rewrites, for example) that need to be verified for correctness. We can write `specs` for directives and run them against a running test server to ensure they are correct. 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 ``` Visit `localhost` in browser. 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 edit `default.conf` to change `root /usr/local/openresty/nginx/html;` to `root /var/www;`: ::: 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
$$name