8 releases (5 stable)
| 1.2.0 | Nov 28, 2025 |
|---|---|
| 1.0.4 | Nov 26, 2025 |
| 0.1.4 | Nov 18, 2025 |
#14 in #x402
3MB
4.5K
SLoC
nginx-x402
Pure Rust implementation of Nginx module for x402 HTTP micropayment protocol.
Features
- ✅ 100% Rust implementation using ngx-rust
- ✅ Auto-detects system nginx version and downloads matching source
- ✅ Type-safe Nginx API bindings
- ✅ Payment verification and 402 response handling
- ✅ Prometheus metrics support
Quick Start
Build from Source
Linux:
sudo apt-get install -y libclang-dev
export NGX_CONFIGURE_ARGS="--without-http_rewrite_module"
cargo build --release
macOS:
xcode-select --install
eval $(./setup-build-env.sh)
cargo build --release
The build script automatically detects your system nginx version and downloads matching source. Optionally set NGINX_SOURCE_DIR to use a specific nginx source.
macOS (Manual):
export LIBCLANG_PATH="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib"
export SDKROOT=$(xcrun --show-sdk-path)
cargo build --release
Install from Package (Debian/Ubuntu)
# Download latest release
ARCH=$(dpkg --print-architecture)
LATEST_URL=$(curl -s https://api.github.com/repos/polyjuicelab/Nginx-X402/releases/latest | grep "browser_download_url.*${ARCH}\.deb" | cut -d '"' -f 4 | head -1)
wget "$LATEST_URL" -O nginx-x402.deb
# Install
sudo dpkg -i nginx-x402.deb
sudo apt-get install -f
sudo ln -s /etc/nginx/modules-available/x402.conf /etc/nginx/modules-enabled/x402.conf
sudo systemctl reload nginx
Configuration
IMPORTANT: Module Loading
You have two options to load the module. Choose ONE method only:
Option 1: Using modules-enabled (Recommended for Debian/Ubuntu)
sudo ln -s /etc/nginx/modules-available/x402.conf /etc/nginx/modules-enabled/x402.conf
This automatically loads the module via /etc/nginx/modules-enabled/x402.conf.
Option 2: Manual load_module in nginx.conf
Add this line to your /etc/nginx/nginx.conf (at the top level, before http block):
load_module /usr/lib/nginx/modules/libnginx_x402.so;
⚠️ DO NOT use both methods - this will cause "module already loaded" errors.
After loading the module, add x402 configuration to your nginx.conf:
http {
server {
location /protected {
x402 on;
x402_amount 0.0001;
x402_pay_to 0xYourWalletAddress;
x402_facilitator_url https://x402.org/facilitator;
}
}
}
Configuration Directives
x402 on|off- Enable/disable payment verificationx402_amount <amount>- Payment amount (e.g., "0.0001")x402_pay_to <address>- Recipient wallet addressx402_facilitator_url <url>- Facilitator service URLx402_description <text>- Payment descriptionx402_network <network>- Network identifier (e.g., "base", "base-sepolia")x402_network_id <chainId>- Network chainId (e.g., 8453 for Base Mainnet, 84532 for Base Sepolia). Takes precedence overx402_networkif both are specified.x402_resource <path>- Resource path (default: request URI)x402_asset <address>- Custom token/contract address (optional, defaults to USDC for the network)x402_asset_decimals <decimals>- Token decimals (default: 6 for USDC, typically 18 for ERC-20 tokens). Required when using custom tokens to ensure correct amount calculation.x402_timeout <seconds>- Facilitator API timeout (1-300, default: 10)x402_facilitator_fallback <mode>- Fallback on error:error(500) orpass(default:error)x402_metrics on|off- Enable Prometheus metrics endpoint
Monitoring
Prometheus Metrics
Add a /metrics location:
location /metrics {
x402_metrics on;
access_log off;
}
Available metrics:
x402_requests_total- Total requests processedx402_payment_verifications_total- Verification attemptsx402_payment_verifications_success_total- Successful verificationsx402_payment_verifications_failed_total- Failed verificationsx402_responses_402_total- 402 responses sentx402_facilitator_errors_total- Facilitator errorsx402_verification_duration_seconds- Verification latency histogramx402_payment_amount- Payment amount histogram
Prometheus Configuration
scrape_configs:
- job_name: 'nginx-x402'
static_configs:
- targets: ['your-nginx-server:80']
metrics_path: '/metrics'
Testing
cargo test
All tests run without requiring nginx source or a running nginx instance.
Environment Variables
NGINX_SOURCE_DIR- Path to nginx source (optional, auto-detected if not set)NGINX_BINARY_PATH- Path to system nginx binary (optional, for signature extraction)NGX_CONFIGURE_ARGS- Nginx configure arguments (recommended:--without-http_rewrite_module)LIBCLANG_PATH- Path to libclang (required on macOS)SDKROOT- macOS SDK path (required on macOS)
How It Works
- Request arrives → Nginx calls Rust handler
- Rust handler → Verifies payment via facilitator service
- Payment verified → Allows request or sends 402 response
License
AGPL-3.0
Dependencies
~26–46MB
~597K SLoC