# ip2nginx
--- ## π‘ Project Overview **ip2nginx** is a lightweight and secure system for dynamically updating NGINX reverse proxy configurations based on public IP address changes, typically reported by edge devices like **pfSense**. It ensures that NGINX always routes traffic through the correct IP, even in dynamic environments. --- ## π Table of Contents - [π‘ Project Overview](#-project-overview) - [π What is ip2nginx?](#-what-is-ip2nginx) - [π‘ Example Use Case](#-example-use-case) - [βοΈ Features](#-features) - [π Project Structure](#-project-structure) - [π Update Process: update.php](#-remote-update-api-updatephp) - [π Update Process: updater.php](#-update-process-updaterphp) - [π Environment Setup: check_env.php](#-check_envphp-environment-setup) - [π Cron Setup with run.sh](#-cron-setup-runsh) - [π Usage Example from pfSense](#-pfsense-shell-example) - [β Example Entry in token.json](#-example-tokenjson) - [β Example Entry in meta.json](#-example-metajson) - [π‘ Security](#-security-highlights) - [β Requirements](#-requirements) - [π License](#-license) - [π€ Author](#-author) --- ## π What is ip2nginx? ip2nginx is a self-hosted system that allows you to make services running on your home network (like a NAS, internal web apps, or router interface) accessible under your own domain β even if your public IP address changes. Unlike third-party solutions other dynamic DNS providers, this tool gives you full control and privacy by updating your reverse proxy configuration directly, without modifying DNS records or relying on external providers. How It Works: Your home device (e.g. pfSense or another gateway) periodically sends its current public IP to your server via an API request. The server updates the reverse proxy configuration (nginx.conf) with the new IP. The proxy_pass directive is updated inside the specific location block for the target domain. NGINX is reloaded automatically to apply changes. --- ## π‘ Example Use Case You want to host https://home.example.com and route it to a web interface at your home (like pfSense or a Raspberry Pi), but your IP changes regularly due to your ISP. With ip2nginx, the server automatically updates the NGINX reverse proxy so your domain continues working β securely and without dynamic DNS services. --- ## βοΈ Features - Accepts remote updates via `update.php` using **token-authenticated** requests. - Supports both `POST` and `GET`, though **POST is preferred** to avoid token caching. - Updates only the `proxy_pass` line in the relevant `location` block of `nginx.conf`. - Automatically marks entries in `meta.json` as `"changed": 1` when input changes. - Logs all changes to `log.json` with timestamps. - Automatically reloads NGINX: `nginx -t && systemctl reload nginx` (requires root). - Built-in abuse protection: failed requests tracked and blocked. - `.htaccess` ensures that only `update.php` is externally accessible. --- ## π Project Structure ``` ip2nginx/ βββ index.php # Shared configuration and fallback error handler βββ update.php # Receives incoming remote IP update requests βββ updater.php # CLI-only: applies changes to nginx.conf if marked βββ run.sh # Wrapper script for cron automation βββ check_env.php # Environment validator and bootstrapper βββ .htaccess # Blocks unauthorized access, routes traffic βββ data/ β βββ meta.json # Stores current configuration state per domain β βββ token.json # Stores allowed tokens (auth) β βββ log.json # Stores audit log of changes β βββ blocklist.json # Temporarily blocked IPs (48h ban) β βββ failures.json # Tracks failed attempts per IP ``` --- ## π Remote Update API: `update.php` Supports **POST** (preferred) and **GET** methods. | Parameter | Required | Description | |------------|----------|-----------------------------------------------------------------------------| | `name` | β | Identifier (e.g. `domain1.to.com`) | | `token` | β | Secret token assigned for this name | | `ip` | β | New public IP (default: auto-detected from request) | | `domain` | β | Backend domain to proxy to (default: same as IP) | | `port` | β | Port number (default: 443 for https, 80 for http) | | `protocol` | β | One of `http` or `https` (default: `http`) | | `location` | β | NGINX location block path to update (default: `/`) | Any change in `ip`, `domain`, `port`, or `protocol` triggers `"changed": 1` in `meta.json`. If **any parameter is received via GET**, then `ip` and `domain` will be overridden with the **clientβs real IP** for security. --- ## π§© Update Process: `updater.php` To apply updates made via `update.php`: 1. Load all entries from `meta.json` 2. Check for entries marked `"changed": 1` 3. Find `/var/www/vhosts/system/