Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active July 10, 2024 11:07
Show Gist options
  • Save rauhs/6cc3a59de01d907bcadac95a908a9b83 to your computer and use it in GitHub Desktop.
Save rauhs/6cc3a59de01d907bcadac95a908a9b83 to your computer and use it in GitHub Desktop.
SHELL=/bin/bash
NGINX:=$(abspath software/nginx/sbin/nginx)
CONF:=$(abspath conf/nginx.conf)
OPENRESTY_V:=1.9.15.1
OPENRESTY_F:=openresty-$(OPENRESTY_V)
OPENRESTY_RUN_DIR:=$(PWD)/openresty
.PHONY: def_target run reload stop auto-reload
def_target : null
run :
mkdir -p $(OPENRESTY_RUN_DIR)/{logs,run}
$(NGINX) -c $(CONF) -p $(OPENRESTY_RUN_DIR) -g "daemon off;"
# Not sure if "notify-send" is Ubuntu only: It creates an OSD notification on your desktop.
reload :
@echo "---------------- Reloading `date` ----------------";
@$(NGINX) -c $(CONF) -p $(OPENRESTY_RUN_DIR) -s reload 2>&1 | tee /dev/tty \
| grep '\[emerg\]' | while read line ; do notify-send "$$line" ; done
stop :
@echo "---------------- Stopping `date` ----------------";
$(NGINX) -c $(CONF) -s stop
auto-reload :
@yes|gawk '\
{cmd = "inotifywait --quiet --recursive --format %w%f -e close_write,moved_to,create conf"; \
cmd |& getline; close(cmd); } \
{print} \
/(conf\/.*)|(lib\/init.lua)/ {system("make --no-print-directory reload")};';
# In the line above you can edit which files to watch with a simple regex.

Overview

You have openresty (or nginx) running locally and you're editing the config. You want to reload the nginx process automatically every time you save the config file. You may also auto reload when you save a lua script file.

It assumes you have a the following directory structure but you can easily modify the commands to suit your setup:

./Makefile
./conf/your-confs.conf
./lib/your-lua-libs.lua
./openresty/   -- Your openresty PWD
./software/nginx/  -- Your nginx binaries (I install them with stow in /software/stow/openresty-19.15.1/)

If your config has an error ([emerg]-type error) it will create an on screen notification (may be Ubuntu only)

Commands

The Makefile shown runs the nginx command and creates a watcher process that reloads every time you save the config file in your editor:

make run

And in a separate terminal:

make auto-reload

Compatibility

Currently this is Linux (ubuntu) only, but it shouldn't be difficult to adapt this for MacOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment