Skip to content

Erreur32/nginx-proxy-manager-Bash-API

Repository files navigation

Release Project Stage Project Maintenance License Contributors Issues Stargazers

Nginx Proxy Manager CLI Script

Table of Contents

  1. Description
  2. Reference API
  3. Prerequisites
  4. Installation
  5. Settings
  6. Usage
  7. Options
  8. Examples
  9. Screens
  10. TODO

Warning

The --restore command is disabled (a fix is in progress).

V2.6.0 introduced some issues. A fix has been tested and pushed, but user feedback is required to ensure everything works as expected with V2.7.0.

Description

πŸ› οΈ This BASH script enables the management of Nginx Proxy Manager through its API.

πŸ”‘ Automatically generates and manages tokens, ensuring their validity, so you don't have to worry about token expiration.

βš™οΈ Provides functionalities such as creating and deleting proxy hosts, managing users, displaying configurations, creating BACKUPS, and more.

French description:

Ce script permet de gΓ©rer Nginx Proxy Manager via l'API. Il fournit des fonctionnalitΓ©s telles que la crΓ©ation de hosts proxy, la gestion des utilisateurs, et l'affichage des configurations avec creation de BACKUP ! La fonction RESTORE n'est pas encore terminΓ©e.

Reference

https://github.com/NginxProxyManager/nginx-proxy-manager/tree/develop/backend/schema

Prerequisites

The excellent NPM (Nginx Proxy Manager)

Required basic dependencies.

The script will automatically check if they are installed and will download them if necessary:

  • curl
  • jq

Installation

wget https://raw.githubusercontent.com/Erreur32/nginx-proxy-manager-Bash-API/main/nginx_proxy_manager_cli.sh
chmod +x nginx_proxy_manager_cli.sh
# Create a config file nginx_proxy_manager_cli.conf in same directory (to keep your config safe) check below.
echo -e "## Nginx proxy IP address (your Nginx IP)\nNGINX_IP=\"127.0.0.1\"\nAPI_USER=\"existingUser@mail.com\"\nAPI_PASS=\"password\"\nBASE_DIR=\"$(pwd)\"" > nginx_proxy_manager_cli.conf
./nginx_proxy_manager_cli.sh --info

Note

With the new V2.0.0, some command arguments have been changed to be simpler, and need to set BASE_DIR variable to store Tokens and Backups.

Settings

Important

(Optional) You can create a configuration file named nginx_proxy_manager_cli.conf with these 4 required variables.

To ensure the script is functional, edit these 4 variables (mandatory).

# nginx_proxy_manager_cli.conf

## Nginx proxy IP address (your Nginx IP)
NGINX_IP="127.0.0.1"
## Existing user (user and password) on NPM
API_USER="admin@example.com"
API_PASS="changeme"
# Path to store .txt files and Backups
BASE_DIR="/path/nginx_proxy_script/data"

Usage

./nginx_proxy_manager_cli.sh [OPTIONS]
./nginx_proxy_manager_cli.sh  --help
./nginx_proxy_manager_cli.sh  --show-default 

Note

New in version 2.6.0:

  • πŸ“Š New dashboard (by default)
  • πŸ” Improved token management
  • πŸ“‹ Enhanced command display and options
  • 🎨 More user-friendly interface with icons and colors

NEW dashboard

πŸ“Š NGINX - Proxy Manager - Dashboard πŸ”§
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

🌐 Proxy Hosts:
   β€’ Total:    42
   β€’ Enabled:  38
   β€’ Disabled: 4

πŸ”„ Redirections:
   β€’ Total: 5

πŸ”Œ Stream Hosts:
   β€’ Total: 3

πŸ”’ SSL Certificates:
   β€’ Total:   15
   β€’ Valid:   12
   β€’ Expired: 3

πŸ‘₯ Users:
   β€’ Total: 4

πŸ†™ Server Status:
   β€’ Uptime: 15 days
   β€’ Version: 2.4.0

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ’‘ Use --help to see available commands

Options

 🌐 Host proxy creation:
   -d DOMAIN_NAMES                       Domain name (required for creating/updating hosts)
   -i FORWARD_HOST                       IP address or domain name of the target server (required for creating/updating hosts)
   -p FORWARD_PORT                       Port of the target server (required for creating/updating hosts)
   -f FORWARD_SCHEME                     Scheme for forwarding (http/https, default: http)
   -c CACHING_ENABLED                    Enable caching (true/false, default: false)
   -b BLOCK_EXPLOITS                     Block exploits (true/false, default: true)
   -w ALLOW_WEBSOCKET_UPGRADE            Allow WebSocket upgrade (true/false, default: true)
   -l CUSTOM_LOCATIONS                   Custom locations (JSON array of location objects)
   -a ADVANCED_CONFIG                    Advanced configuration (block of configuration settings)
   -y                                    Automatic yes prompts, yes sir!

πŸ“¦ Backup and Restore:
   --backup                             Backup all configurations to a file
   --backup-host id                     Backup a single host configuration and its certificate (if exists)

 πŸ”§ Miscellaneous:
   --check-token                         Check if the current token is valid
   --create-user user pass email         Create a user with a username, password and email
   --delete-user username                Delete a user by username
   --host-delete id                      Delete a proxy host by ID
   --host-show id                        Show full details for a specific host by ID
   --show-default                        Show default settings for creating hosts
   --host-list                           List the names of all proxy hosts
   --host-list-full                      List all proxy hosts with full details
   --list-users                          List all users

   --host-search hostname                Search for a proxy host by domain name
   --host-enable id                      Enable a proxy host by ID
   --host-disable id                     Disable a proxy host by ID
   --host-ssl-enable id                  Enable SSL, HTTP/2, and HSTS for a proxy host
   --host-ssl-disable id                 Disable SSL, HTTP/2, and HSTS for a proxy host
   --list-ssl-cert                       List All SSL certificates availables (JSON)
   --generate-cert domain email          Generate certificate for the given domain and email
   --delete-cert domain                  Delete   certificate for the given domain
   --list-access                         List all available access lists (ID and name)
   --host-acl-enable id,access_list_id   Enable ACL for a proxy host by ID with an access list ID       
   --host-acl-disable id                 Disable ACL for a proxy host by ID
   --update-host id field=value          Modify any field on existing entry host
   --help                                Display this help

Examples

  πŸ“¦ Backup First !
   ./nginx_proxy_manager_cli.sh --backup

 🌐 Host Creation:
   ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 (check default values below)
   ./nginx_proxy_manager_cli.sh --info
   ./nginx_proxy_manager_cli.sh --show-default
   ./nginx_proxy_manager_cli.sh --create-user newuser password123 user@example.com
   ./nginx_proxy_manager_cli.sh --delete-user 'username'
   ./nginx_proxy_manager_cli.sh --host-list
   ./nginx_proxy_manager_cli.sh --host-ssl-enable 10

 πŸ€– Automatic operations (no prompts):
   ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -y
   ./nginx_proxy_manager_cli.sh --host-delete 42 -y
   ./nginx_proxy_manager_cli.sh --host-ssl-enable 10 -y   

 πŸ” Information and Status:
   ./nginx_proxy_manager_cli.sh --info                   # Show script configuration and status
   ./nginx_proxy_manager_cli.sh --check-token            # Verify token validity
   ./nginx_proxy_manager_cli.sh --host-search domain.com # Search for a specific domain

 πŸ”„ Host Management:
   # Enable/Disable hosts
   ./nginx_proxy_manager_cli.sh --host-enable 42
   ./nginx_proxy_manager_cli.sh --host-disable 42

 πŸ›‘οΈ Access Control Lists:
   ./nginx_proxy_manager_cli.sh --list-access                   # List all access lists
   ./nginx_proxy_manager_cli.sh --host-acl-enable 42,5          # Enable ACL ID 5 for host 42
   ./nginx_proxy_manager_cli.sh --host-acl-disable 42           # Disable ACL for host 42

 πŸ”’ SSL Management:
   ./nginx_proxy_manager_cli.sh --list-ssl-cert                 # List all SSL certificates
   ./nginx_proxy_manager_cli.sh --delete-cert domain.com        # Delete certificate for domain

 πŸ”„ Update Specific Fields:
   # Update individual fields without recreating the entire host
   ./nginx_proxy_manager_cli.sh --update-host 42 forward_scheme=https
   ./nginx_proxy_manager_cli.sh --update-host 42 forward_port=8443
   ./nginx_proxy_manager_cli.sh --update-host 42 block_exploits=true
   ./nginx_proxy_manager_cli.sh --update-host 42 allow_websocket_upgrade=true

 πŸ”§ Advanced Example:
   ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -a 'proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;'

 πŸ›‘οΈ Custom Certificate:
   ./nginx_proxy_manager_cli.sh --generate-cert example.com user@example.com 
   # Note: This will generate a Let's Encrypt certificate only

 πŸ›‘οΈ  Custom locations:
   ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'

πŸ”– Full options:
   ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -f https -c true -b true -w true -a 'proxy_set_header X-Real-IP $remote_addr;' -l '[{"path":"/api","forward_host":"192.168.1.11","forward_port":8081}]'

--backup

./nginx_proxy_manager_cli.sh --backup

Schema of the backup directory:

πŸ“ data/                                         # Root directory
β”œβ”€β”€ πŸ“ backups/                                  # Backup directories
β”‚   └── πŸ“ [IP]_[PORT]/                          # NPM Instance (IP:PORT)
β”‚       β”œβ”€β”€ πŸ“ .access_lists/                    # Access Lists configurations
β”‚       β”œβ”€β”€ πŸ“ .Proxy_Hosts/                     # Host configurations
β”‚       β”‚   β”œβ”€β”€ πŸ“ [DOMAIN]/                     # Directory for each domain
β”‚       β”‚   β”‚   β”œβ”€β”€ πŸ“ logs/                     # Log directory
β”‚       β”‚   β”‚   β”œβ”€β”€ πŸ“ ssl/                      # SSL directory
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ certificate_meta.json # Certificate metadata
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ certificate.pem       # Certificate
β”‚       β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ chain.pem             # Chain of certificates
β”‚       β”‚   β”‚   β”‚   └── πŸ“„ private.key           # Private key
β”‚       β”‚   β”‚   β”œβ”€β”€ πŸ“„ nginx.conf                # Nginx configuration
β”‚       β”‚   β”‚   └── πŸ“„ proxy_config.json         # Proxy configuration
β”‚       β”‚   β”œβ”€β”€ πŸ“„ all_hosts_[DATE].json         # List of all hosts
β”‚       β”‚   └── πŸ“„ all_hosts_latest.json         # Symlink to latest backup
β”‚       β”œβ”€β”€ πŸ“ .settings/                        # NPM settings
β”‚       β”œβ”€β”€ πŸ“ .ssl/                             # SSL certificates
β”‚       β”œβ”€β”€ πŸ“ .user/                            # User configurations
β”‚       β”œβ”€β”€ πŸ“„ full_config_[DATE].json           # Full backup
β”‚       └── πŸ”— full_config_latest.json           # Symlink to latest backup
β”‚
└── πŸ“ token/                                    # Token directory
    β”œβ”€β”€ πŸ“„ token_[IP]_[PORT].txt                 # Authentication token
    └── πŸ“„ expiry_[IP]_[PORT].txt                # Token expiry date

--update-host

update specific fields of an existing proxy host

The --update-host command allows you to update specific fields of an existing proxy host in Nginx Proxy Manager without recreating it.

Simply specify the proxy host ID and the field you want to update, like this:

./nginx_proxy_manager_cli.sh --update-host 42 forward_host=new.backend.local
Field Name Type Description
domain_names array List of domains handled by this proxy.
forward_host string The destination (backend) hostname or IP.
forward_port integer The destination port (e.g., 8000, 443).
forward_scheme string The scheme: http or https.
enabled boolean Whether the proxy is enabled (true or false).
ssl_forced boolean Redirect all HTTP requests to HTTPS.
certificate_id integer The ID of the SSL certificate to use.
meta.letsencrypt_agree boolean Agree to Let's Encrypt TOS (true or false).
meta.dns_challenge boolean Use DNS challenge for SSL cert (true or false).
allow_websocket_upgrade boolean Enable WebSocket support (true or false).
http2_support boolean Enable HTTP/2 (true or false).
caching_enabled boolean Enable caching (true or false).
block_exploits boolean Block known exploits (true or false).
advanced_config string Custom Nginx directives (multiline string).
locations array Custom location blocks (advanced use).

Verifying the Configuration

Some info of settings in the script with ./nginx_proxy_manager_cli_.sh --info

info

./nginx_proxy_manager_cli_.sh --info

Script Info:  2.3.5

Script Variables Information:
  BASE_URL    http://127.0.0.1:81/api
  NGINX_IP    127.0.0.1
  API_USER    admin@example.com
  BASE_DIR    /path/to/nginx_proxy
  BACKUP_DIR  /path/to/nginx_proxy/backups
  BACKUP HOST 40
  Token NPM   /path/to/nginx_proxy/token/token_127.0.0.1.txt

How to activate SSL ?

By following these steps, you can enable SSL for your proxy host for the first time using Let's Encrypt.

--host-list

List all Host in one command and show Β΄idΒ΄ , Β΄statusΒ΄ and Β΄SSLΒ΄ status:

./nginx_proxy_manager_cli.sh --host-list

  πŸ‘‰ List of proxy hosts (simple)
  ID     Domain                               Status    SSL
  1      toto.fun                              enabled  ✘
  2      titi.fun                              disable  βœ…
  3      tutu.fun                              enabled  βœ…

--host-ssl-enable

Enable SSL for the Host

Assuming the host ID is 1, you would enable SSL for the host as follows:

./nginx_proxy_manager_cli.sh --host-ssl-enable 1

SSl is enable successfully, check again with --host-list

 ./nginx_proxy_manager_cli.sh --host-list

  πŸ‘‰ List of proxy hosts (simple)
  ID     Domain                               Status    SSL
  1      toto.fun                              enabled  βœ…
  2      titi.fun                              disable  βœ…
  3      tutu.fun                              enabled  βœ…
Other Exemple command:

Host proxy info command --host-show id

 ./nginx_proxy_manager_cli_.sh --host-show 1

 πŸ‘‰ Full details for proxy host ID: 59...

{
  "id": 10,
  "created_on": "2024-07-11 13:16:34",
  "modified_on": "2024-07-13 09:42:40",
  "owner_user_id": 1,
  "domain_names": [
    "test.domain.com"
  ],
  "forward_host": "127.0.0.1",
  "forward_port": 80,
  "access_list_id": 0,
  "certificate_id": 81,
  "ssl_forced": 1,
  "caching_enabled": 0,
  "block_exploits": 1,
  "advanced_config": "",
  "meta": {
    "letsencrypt_agree": true,
    "letsencrypt_email": "",
    "nginx_online": true,
    "nginx_err": null
  },
  "allow_websocket_upgrade": 1,
  "http2_support": 1,
  "forward_scheme": "http",
  "enabled": 1,
  "locations": [],
  "hsts_enabled": 1,
  "hsts_subdomains": 0
}

Screens:

screen-nginx-proxy-default

TODO:

  • add setting for ADVANCED configuration in npm location / { ... }
  • Add documentation on certain functions
  • ADD: a configuration function for Custom Locations
  • Backup all settings from NPM
  • Add automatic confirmation with -y parameter
  • Clean/minimize output when using -y parameter for better script integration
  • Creation of ACCESS list through CLI
  • Restore Function not working properly, need to find FIX

Credits & Thanks

Special thanks to:

  • @ichbinder for implementing the -y parameter for automatic confirmations

License

MIT License - see the LICENSE.md file for details

About

This BASH script enables the management of Nginx Proxy Manager through its API.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages