# NX - Nginx Site Manager A lightweight, colorized Bash utility to manage Nginx virtual hosts using the `sites-available` / `sites-enabled` pattern. It replaces the need for manual `ln -s` and `rm` commands with a simple, human-readable interface. ## Features * **Container Support**: Works in containerized contexts via environment variable configuration. * **Tree-view Listing**: Automatically groups subdomains under their parent domains using ASCII logic. ``` $ sudo nx list SITE STATUS ---------------------------------------- ---------- default Disabled ● example.com Enabled └── ● knot.example.com Enabled └── ● pds.example.com Enabled └── statusphere.example.com Disabled ● example.net Enabled └── ● staging.example.net Enabled └── wiki.example.net Disabled └── ws.example.net Disabled ``` * **Status Indicators**: Color-coded feedback for "Enabled" (Green) and "Disabled" sites. * **Smart Suffixes**: Automatically handles .conf extensions so you don't have to type them. * **Integrated Testing**: Prompts to run `nginx -t` and reload the service after every edit or toggle. * **Config Preview**: Uses batcat (with syntax highlighting) if installed, otherwise falls back to cat. ```bash $ sudo nx print knot.example.com ───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── │ File: /etc/nginx/sites-available/knot.example.com.conf ───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ server { 2 │ listen 443 ssl; 3 │ listen [::]:443 ssl; 4 │ server_name knot.example.com; 5 │ 6 │ ssl_certificate /etc/ssl/acme/example.com/fullchain.pem; 7 │ ssl_certificate_key /etc/ssl/acme/example.com/key.pem; 8 │ 9 │ access_log /var/log/nginx/example.access.log main; 10 │ 11 │ location / { 12 │ proxy_pass http://127.0.0.1:5555; 13 │ proxy_http_version 1.1; 14 │ 15 │ proxy_set_header Upgrade $http_upgrade; 16 │ proxy_set_header Connection $connection_upgrade; 17 │ 18 │ proxy_set_header Host $host; 19 │ proxy_set_header X-Real-IP $remote_addr; 20 │ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 21 │ proxy_set_header X-Forwarded-Proto $scheme; 22 │ } 23 │ } ───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ``` ## Installation To install nx system-wide while keeping the source code in this repository for easy updates: 1. Move the repository to a permanent location (e.g., your home dir or a projects dir): ```bash cd ~ git clone https://tangled.org/sullen.net/nx cd nx ``` 2. Make the script executable: ```bash chmod +x nx ``` 3. Create a symbolic link in your PATH: ```bash sudo ln -s $(pwd)/nx.sh /usr/local/bin/nx ``` 4. Ensure that your main Nginx config (e.g., `/etc/nginx/nginx.conf`) attempts to load in configurations at `/etc/nginx/sites-enabled/*` (or wherever you'd like to store your configurations). ```nginx # ... http { # ... include /etc/nginx/sites-enabled/*; } ``` ## Usage Most commands require sudo to modify Nginx directories and reload the service. ```bash NX - Nginx Site Manager A simple utility to manage Nginx configurations. USAGE: nx [arguments] COMMANDS: list, l List all sites. Supports --enabled or --disabled filters. enable Enable a site by creating a symlink. disable Disable a site by removing the symlink. edit, e Open site config in $EDITOR. cat, print, p Preview site config using batcat or cat. EXAMPLES: sudo nx list sudo nx enable example.com sudo nx disable pds.example.com sudo nx e example.com sudo nx print knot.example.com ENVIRONMENT: EDITOR Currently set to nano NX_SITES_AVAILABLE_PATH Currently set to /etc/nginx/sites-available NX_SITES_ENABLED_PATH Currently set to /etc/nginx/sites-enabled NX_RELOAD_CMD Currently set to systemctl reload nginx NX_TEST_CMD Currently set to nginx -t ``` ## Environment Variables The edit command respects your preferred editor. You can set this in your `~/.bashrc` or `~/.zshrc`: ```bash export EDITOR='emacs' ``` Alternative `sites-available` and `sites-enabled` directories can be configured using the following environment variables. ```bash export NX_SITES_AVAILABLE_PATH= export NX_SITES_ENABLED_PATH= ``` Alternative `test` and `reload` commands can be configured using the following environment variables. The following example demonstrates how this might look in a containerized context. ```bash export NX_RELOAD_CMD='systemctl --user reload nginx' export NX_TEST_CMD='podman exec nginx nginx -t' ``` **Note**: To ensure your environment variables are picked up when running with sudo, use the -E flag: ```bash sudo -E nx enable example.com ``` ## License [MIT](/LICENSE.txt)