nginx-pretty
v1.0.9
Published
git init
Downloads
972
Maintainers
Readme
🛡️ nginx-pretty
A CLI utility to safely format and apply NGINX configuration files using the nginxbeautifier package (same as bash.sh). Never modifies original files unless you confirm; shows diff and validates with nginx -t.
🚀 Features
- 📄 Run on default config only with
--default/-d; custom path via--file. Running with no target shows help (safer). - 🛡️ Never modifies the original unless confirmed
- 🔍 Shows unified diff before applying
- 🧪 Runs
nginx -tto validate before reload - 🧰 Works with
sudo, in disconnected or hardened servers - 🧱 CLI structure is standalone, can be bundled with
pkg
🧱 Directory Structure
nginx-pretty/
├── cli.js # Main script (uses nginxbeautifier)
├── bin/
│ └── nginx-pretty # Optional wrapper bash script
├── README.md
├── package.json⚙️ Usage
Global install (recommended)
Install globally with npm. Run as your user (so your PATH/nvm is used); the CLI uses sudo only when copying to the config file and when running nginx -t:
npm install -g nginx-pretty
nginx-pretty --default # or -d to use /etc/nginx/sites-available/defaultRunning nginx-pretty with no options shows help only; use --default/-d or --file <path> to target a config.
If you use nvm and install with npm install -g nginx-pretty, do not run with sudo nginx-pretty (sudo uses root’s PATH and won’t see nvm). Run nginx-pretty as your user; you may be prompted for your password when the tool runs sudo cp and sudo nginx -t.
As Bash Script (with embedded Node)
./bin/nginx-prettyVia npx
npx nginx-prettyIf Bundled as a Binary (using pkg)
./nginx-prettyOptions
| Option | Description |
|--------|-------------|
| --default, -d | Use the default config file (/etc/nginx/sites-available/default) |
| --file, -f <path> | Config file path |
| --check-only | Show diff only, do not prompt or apply |
| --dry-run | Same as --check-only |
| --no-prompt | Apply without prompting (for CI; use with caution) |
| --help, -h | Show help |
Running nginx-pretty with no options (or with only --help/-h) shows help and exits; you must pass --default/-d or --file <path> to run on a config.
Help
nginx-pretty # shows help (no target)
nginx-pretty --help # or -h📜 Example Flow
🧪 Running nginx-pretty...
📄 Copying original file to: /tmp/tmp_nginx_formatted.conf
🆕 Ensuring temp file is writable...
🎨 Formatting the copied config...
🔍 Showing unified diff (ORIGINAL vs FORMATTED):
...
diff output...
⚠️ Do you want to apply the formatted config? (yes/no): yes
🚀 Config test passed. You can now reload NGINX
Run: sudo service nginx reload to apply the changes🛠️ Build Instructions (Optional)
To package this into a true standalone binary:
npm install
npm install -g pkg
pkg cli.js --output nginx-prettyOr use the npm script:
npm run build:pkg✅ This produces a native binary: no Node required on the target machine.
🧪 How to test
From the project directory (no global install needed):
1. Help
node cli.js --help2. Dry-run (no nginx required)
Use a temporary config so nothing is modified. You should see a unified diff and “Dry run / check-only: not applying.”
echo 'server { listen 80; root /var/www; }' > /tmp/test-nginx.conf
node cli.js --file /tmp/test-nginx.conf --dry-run3. Optional: real NGINX config
If you have NGINX and a config (e.g. /etc/nginx/sites-available/default), run with --dry-run first to only view the diff:
sudo node cli.js --file /etc/nginx/sites-available/default --dry-runWithout --dry-run, the CLI will prompt to apply; answer yes only if you want to overwrite the file.
🔐 Security Notes
- This CLI never overwrites original config unless approved.
- Temporary files are cleaned and written to
/tmp. - Requires
sudofor NGINX reload steps. - Uses
spawnwith array arguments only; no user input is passed to shell.
📦 Dependencies
nginxbeautifier(npm dependency; same formatter as bash.sh)- Node.js 14+
diffon PATH (for unified diff; standard on Linux)
📜 License
MIT © You
