Skip to content

Erreur32/nginx-proxy-manager-Bash-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Release Project Stage Project Maintenance License Contributors Issues Stargazers

Nginx Proxy Manager CLI Script V3.0.0 πŸš€

Table of Contents

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

Warning

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

V3.0.0 is out πŸš€

Check the latest release with major improvements and fixes.

Description

πŸ› οΈ This script allows you to efficiently manage Nginx Proxy Manager via its API. It provides advanced features such as proxy host creation, user management, and configuration display, while also integrating a backup system (BACKUP) with a user-friendly interface.

It simplifies task automation, including proxy creation, SSL certificate management, and full reverse proxy administration.

⚠️ The RESTORE function is still under development. 🚧

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

French description: Ce script permet de gérer Nginx Proxy Manager via son API de manière simple et efficace. Il offre des fonctionnalités avancées telles que la création de hosts proxy, la gestion des utilisateurs et l'affichage des configurations, tout en intégrant un système de sauvegarde (BACKUP) avec une interface conviviale.

Il facilite l'automatisation des tÒches courantes, comme l'ajout de proxies, la gestion des certificats SSL et l'administration complète de vos reverse proxies.

⚠️ La fonction RESTORE est encore en développement. 🚧

Reference API

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/npm-api.sh
chmod +x npm-api.sh
# Run the script.
./npm-api.sh

Settings

Important

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

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

# npm-api.conf

## Nginx proxy IP address (your Nginx IP/port)
NGINX_IP="127.0.0.1"
NGINX_PORT="81"

## Existing user (user and password) on NPM
API_USER="admin@example.com"
API_PASS="changeme"

# Optional (only if you want in other /path than script directory)
# DATA_DIR="/path/nginx_backup/dir"

Usage

./npm-api.sh [OPTIONS]
./npm-api.sh  --help
./npm-api.sh  --show-default 

Options

 Options available:                       (see --examples for more details)
   -y                                     Automatic yes prompts!
  --info                                  Display Script Variables Information
  --show-default                         Show  Default settings for host creation
  --check-token                           Check Check current token info
  --backup                                πŸ’Ύ Backup All configurations to a different files in $DATA_DIR

 Proxy Host Management:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  --host-search domain                    Search Proxy host by domain name
  --host-list                             List All Proxy hosts (to find ID)
  --host-show πŸ†”                          Show Full details for a specific host by ID

  --host-create domain -i forward_host -p forward_port [options]

     Required:
            domain                        Domain name (required)
       -i   forward-host                  IP address or domain name of the target server (required)
       -p   forward-port                  Port of the target server (required)

     optional: (Check default settings,no argument needed if already set!)
       -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 (string)

  --host-enable  πŸ†”                       Enable Proxy host by ID
  --host-disable πŸ†”                       Disable Proxy host by ID
  --host-delete  πŸ†”                       Delete Proxy host by ID
  --host-update  πŸ†” [field]=value         Update One specific field of an existing proxy host by ID
                                          (eg., --host-update 42 forward_host=foobar.local)

  --host-acl-enable  πŸ†” access_list_id    Enable ACL for Proxy host by ID with Access List ID
  --host-acl-disable πŸ†”                   Disable ACL for Proxy host by ID
  --host-ssl-enable  πŸ†” [cert_id]         Enable SSL for host ID optionally using specific certificate ID
  --host-ssl-disable πŸ†”                   Disable SSL, HTTP/2, and HSTS for a proxy host

  --cert-list                             List ALL SSL certificates
  --cert-show     domain Or πŸ†”            List SSL certificates filtered by [domain name] (JSON)
  --cert-delete   domain Or πŸ†”            Delete Certificate for the given 'domain'
  --cert-generate domain [email]          Generate Let's Encrypt Certificate or others Providers.
                                           β€’ Standard domains: example.com, sub.example.com
                                           β€’ Wildcard domains: *.example.com (requires DNS challenge)
                                           β€’ DNS Challenge: Required for wildcard certificates
                                             - Format: dns-provider PROVIDER dns-api-key KEY
                                             - Providers: dynu, cloudflare, digitalocean, godaddy, namecheap, route53, ovh, gcloud, ...

  --user-list                             List All Users
  --user-create username password email   Create User with a username, password and email
  --user-delete πŸ†”                        Delete User by username

  --access-list                           List All available Access Lists (ID and Name)
  --access-list-show πŸ†”                   Show detailed information for specific access list
  --access-list-create                    Create Access Lists with options:
                                           β€’ --satisfy [any|all]          Set access list satisfaction mode
                                           β€’ --pass-auth [true|false]     Enable/disable password authentication
                                           β€’ --users "user1,user2"        List of users (comma-separated)
                                           β€’ --allow "ip1,ip2"            List of allowed IPs/ranges
                                           β€’ --deny "ip1,ip2"             List of denied IPs/ranges
  --access-list-delete πŸ†”                 Delete Access List by access ID
  --access-list-update πŸ†”                 Update Access List by access ID with options:
                                           β€’ --name "new_name"            New name for the access list
                                           β€’ --satisfy [any|all]          Update satisfaction mode
                                           β€’ --pass-auth [true|false]     Update password authentication
                                           β€’ --users "user1,user2"        Update list of users
                                           β€’ --allow "ip1,ip2"            Update allowed IPs/ranges
                                           β€’ --deny "ip1,ip2"             Update denied IPs/ranges

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  --examples                             πŸ”– Examples commands, more explicits
  --help                                     πŸ‘‰ It's me

Examples

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

 🌐 Host Creation:
   # Basic host creation
   ./npm-api.sh --host-create domain.com -i IP -p PORT [-b true/false] [-c true/false] [-w true/false] [-h true/false]

   # Create host with SSL certificate and enable SSL (all-in-one)
   ./npm-api.sh --host-create domain.com -i IP -p PORT [options] --cert-generate --host-ssl-enable -y

   # Create host with SSL certificate and enable SSL (with specific domain)
   ./npm-api.sh --host-create domain.com -i IP -p PORT [options] --cert-generate domain.com --host-ssl-enable -y

   # Create host with custom options
   ./npm-api.sh --host-create example.com -i 192.168.1.10 -p 8080 \
     -f https \         # Forward scheme
     -b true \          # Block exploits
     -c true \          # Enable caching
     -w true \          # Enable websocket
     -h true \          # Enable HTTP/2
     -y                 # Auto confirm

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

 πŸ” Information and Status:
   ./npm-api.sh --info                      # Show configuration and dashboard
   ./npm-api.sh --show-default              # Show default settings
   ./npm-api.sh --check-token               # Verify token validity
   ./npm-api.sh --host-search domain.com    # Search for a specific domain
   ./npm-api.sh --host-list                 # List all hosts
   ./npm-api.sh --host-list-full            # List hosts with details
   ./npm-api.sh --host-show 42              # Show specific host details

 πŸ”’ SSL Management:
   # List all certificates
   ./npm-api.sh --list-ssl-cert                 
   # Generate standard Let's Encrypt certificate
   ./npm-api.sh --cert-generate domain.com [email] [dns_provider] [dns_credentials] [-y]
   # Generate wildcard certificate with Cloudflare
   ./npm-api.sh --cert-generate "*.example.com" \
     --cert-email admin@example.com \
     --dns-provider cloudflare \
     --dns-credentials '{"dns_cloudflare_email":"your@email.com","dns_cloudflare_api_key":"your_api_key"}'

   # Delete certificate
   ./npm-api.sh --delete-cert domain.com        
   # Enable SSL for host
   ./npm-api.sh --host-ssl-enable HOST_ID            
   # Generate certificate and enable SSL for existing host
   ./npm-api.sh --cert-generate domain.com --host-ssl-enable -y

 🌟 Complete Examples with Wildcard Certificates:
   # Create host with wildcard certificate using Cloudflare DNS
   ./npm-api.sh --host-create "*.example.com" -i 192.168.1.10 -p 8080 \
     --cert-generate "*.example.com" \
     --cert-email admin@example.com \
     --dns-provider cloudflare \
     --dns-credentials '{"dns_cloudflare_email":"your@email.com","dns_cloudflare_api_key":"your_api_key"}' \
     --host-ssl-enable -y

   # Same with DigitalOcean DNS
   ./npm-api.sh --host-create "*.example.com" -i 192.168.1.10 -p 8080 \
     --cert-generate "*.example.com" \
     --cert-email admin@example.com \
     --dns-provider digitalocean \
     --dns-credentials '{"dns_digitalocean_token":"your_token"}' \
     --host-ssl-enable -y

   # Same with GoDaddy DNS
   ./npm-api.sh --host-create "*.example.com" -i 192.168.1.10 -p 8080 \
     --cert-generate "*.example.com" \
     --cert-email admin@example.com \
     --dns-provider godaddy \
     --dns-credentials '{"dns_godaddy_key":"your_key","dns_godaddy_secret":"your_secret"}' \
     --host-ssl-enable -y

 πŸ›‘οΈ Access Control Lists:
   # List all access lists
   ./npm-api.sh --list-access                   
   # Show detailed information for specific access list
   ./npm-api.sh --access-list-show 123  
   # Create a basic access list
   ./npm-api.sh --access-list-create "office" --satisfy any
   # Create access list with authentication
   ./npm-api.sh --access-list-create "secure_area" --satisfy all --pass-auth true
   # Create access list with users
   ./npm-api.sh --access-list-create "dev_team" --users "john,jane,bob" --pass-auth true
   # Create access list with IP rules
   ./npm-api.sh --access-list-create "internal" --allow "192.168.1.0/24" --deny "192.168.1.100"
   # Create comprehensive access list
   ./npm-api.sh --access-list-create "full_config" \
     --satisfy all \
     --pass-auth true \
     --users "admin1,admin2" \
     --allow "10.0.0.0/8,172.16.0.0/12" \
     --deny "10.0.0.50,172.16.1.100"
   
   # Update an existing access list
   ./npm-api.sh --access-list-update 42        
   # Delete an access list (with confirmation)
   ./npm-api.sh --access-list-delete 42        
   # Delete an access list (skip confirmation)
   ./npm-api.sh --access-list-delete 42 -y     
   # Enable ACL for a host
   ./npm-api.sh --host-acl-enable 42,5         # Enable ACL ID 5 for host 42
   # Disable ACL for a host
   ./npm-api.sh --host-acl-disable 42          # Disable ACL for host 42

 πŸ‘₯ User Management:
   ./npm-api.sh --create-user newuser password123 user@example.com
   ./npm-api.sh --delete-user 'username'
   ./npm-api.sh --list-users

 πŸ”§ Advanced Examples:
   # Custom Nginx configuration
   ./npm-api.sh --host-create example.com -i 192.168.1.10 -p 8080 \
     -a 'proxy_set_header X-Real-IP $remote_addr;'

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

   # Update specific fields
   ./npm-api.sh --update-host 42 forward_scheme=https
   ./npm-api.sh --update-host 42 forward_port=8443

 
 πŸ”– Full options:
   ./npm-api.sh --host-create 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

./npm-api.sh --backup

πŸ’Ύ Backup Operations

Schema of the backup directory:

# Full backup of all configurations
./npm-api.sh --backup

# This will create a backup in the following structure:
πŸ“ data/
└── πŸ“ backups/
    └── πŸ“ [IP]_[PORT]/
        β”œβ”€β”€ πŸ“ .access_lists/                    # Access list configurations
        β”œβ”€β”€ πŸ“ .Proxy_Hosts/                     # All proxy 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.json                  # Complete backup file
        └── πŸ“ token/  
            β”œβ”€β”€ πŸ“„ token.txt                     # Authentication token
            └── πŸ“„ expiry.txt                    # Token expiry date        

πŸ”„ Backup Contents

  1. Proxy Hosts (/.Proxy_Hosts/)

    • Individual host configurations
    • Nginx configurations
    • Complete host list with timestamps
  2. SSL Certificates (/.ssl/)

    • Certificates and private keys
    • Certificate metadata
    • Chain certificates
  3. Access Lists (/.access_lists/)

    • Access list configurations
    • Client authorizations
    • Access rules
  4. Users (/.user/)

    • User accounts
    • Permissions
    • Authentication settings
  5. Settings (/.settings/)

    • Global NPM settings
    • System configurations
    • Default parameters

πŸ” Token Management

The token/ directory contains:

  • Authentication tokens
  • Token expiry information
  • One file per NPM instance

--host-update

update specific fields of an existing proxy host

The --host-update 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:

./npm-api.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 ./npm-api.sh --info

info

./npm-api.sh --info

 πŸ” Checking system dependencies and directories...
 βœ… All dependencies and directories are properly set up
    β”œβ”€β”€ System tools: OK
    β”œβ”€β”€ Directories : OK
    └── Permissions : OK

 πŸ”‘ Checking token validity...
 βœ… Token is valid
 πŸ“… Expires: 2026-03-14T10:24:56.267Z

 Script Info:  3.0.0
 Script Variables Information:
 Config      : /home/tools/Project/nginx_proxy/npm-api.conf
 BASE  URL   : http://127.0.0.1:8099/api
 NGINX  IP   : 127.0.0.1
 USER NPM    : user@mail.com
 BACKUP DIR  : /home/tools/Project/nginx_proxy/data/127_0_0_1_8099

 πŸ“‚ Backup Locations:
  β€’ Backup: /home/tools/Project/nginx_proxy/data/127_0_0_1_8099/backups
  β€’ Token: /home/tools/Project/nginx_proxy/data/127_0_0_1_8099/backups/token/

 πŸ“Š NGINX - Proxy Manager - Dashboard πŸ”§
 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚  COMPONENT      β”‚ STATUS  β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ 🌐 Proxy Hosts  β”‚ 11      β”‚
 β”‚ β”œβ”€ Enabled      β”‚ 9       β”‚
 β”‚ └─ Disabled     β”‚ 2       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ πŸ”„ Redirections β”‚ 1       β”‚
 β”‚ πŸ”Œ Stream Hosts β”‚ 0       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ πŸ”’ Certificates β”‚ 1       β”‚
 β”‚ β”œβ”€ Valid        β”‚ 1       β”‚
 β”‚ └─ Expired      β”‚ 0       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ πŸ”’ Access Lists β”‚ 1       β”‚
 β”‚ └─ Clients      β”‚ 0       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ πŸ‘₯ Users        β”‚ 3       β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
 β”‚ ⏱️  Uptime       β”‚ 2 days  β”‚
 β”‚ πŸ“¦ NPM Version  β”‚ 2.12.3  β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

 πŸ’‘ Use --help to see available commands
    Check --examples for more help examples

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:

  ./npm-api.sh --host-list

  πŸ‘‰ List of proxy hosts (simple)
    ID     Domain                               Status    SSL    Certificate Domain
    14     example.com                           enabled  ✘
    15     example.titi                          enabled  ✘
    1      domain.com                            disable  8      domain.com
    11     titi.eu                               enabled  ✘
    12     toutou                                disable  ✘
    13     toutoux                               enabled  ✘

--host-ssl-enable

Enable SSL for the Host

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

./npm-api.sh --host-ssl-enable 1
Other Exemple command:

Host proxy info command --host-show id

 ./npm-api.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
}

Important Notice: Repository History Rewritten

⚠️ Action Required for All Contributors (or cloned repo.)

We have performed a force push (git push --force) on this repository to remove sensitive data from the history. As a result, the commit history has been rewritten, and your local copy may be out of sync.

πŸ› οΈ What You Need to Do? To avoid any issues, please follow these steps to update your local repository:

git fetch --all
git reset --hard origin/main  # Replace 'main' with your branch name if different

If you have local changes that you don't want to lose, consider making a backup before running these commands.

❓ Why Was This Done? This action was necessary to remove sensitive data from the repository's history and ensure better security.

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

  • πŸ™ Special thanks to zafar-2020 for his valuable help with testing and reporting issues during the development of version 3.0.0!

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