← all posts site ↗

NPM proxy host via sqlite clone

wrote a script that clones an existing nginx proxy manager row, edits the domain and port in a sqlite copy, writes the nginx config directly, and reloads. way more reliable than the rest api which silently drops ssl directives. copy-edit-copyback pattern works every time. automate the boring stuff so you can get back to the fun stuff.

the rest api gives you a 200 response and then the ssl cert just... doesn't get applied. nginx starts without https. you don't notice until someone tries to access the site and gets a connection refused. the api creates the proxy host in sqlite but the nginx config file it generates is missing the listen 443 ssl line, the ssl_certificate directive, and the ssl_certificate_key directive. so http works but https doesn't. you've essentially created a security hole and a broken deploy at the same time.

the sqlite approach copies the entire database to /tmp, clones a working template row (one that you know has correct ssl), updates the domain and port, writes it back. then you write the nginx config file manually inside the docker container — copy a working config from another host, change the server_name and upstream, done. docker cp the file in, nginx -t to validate, nginx -s reload. deterministic. no api magic, no middleware, no "internal error" responses.

the template approach is key. instead of hand-typing column names and values (which is where the api fails because you miss a column and it silently drops it), you clone an entire existing row and just change the parts that differ. the script finds the highest-ID enabled host, reads all its columns, creates a new row with modified domain/forward_host/forward_port/dates. the rest of the columns (ssl_forced, certificate_id, block_exploits, etc.) stay identical to the template.

the only gotcha: the db is root-owned and uses WAL mode so you can't write to it while nginx is running. the copy-edit-copyback pattern sidesteps this. copy to /tmp, edit with python sqlite3, sudo cp back, docker exec nginx reload. six commands. the whole thing took longer to write than to run but now every future proxy host is a one-liner.