| Crates.io | nginx-x402 |
| lib.rs | nginx-x402 |
| version | 1.2.0 |
| created_at | 2025-11-17 18:18:47.621151+00 |
| updated_at | 2025-11-28 15:14:37.599368+00 |
| description | Pure Rust Nginx module for x402 HTTP micropayment protocol |
| homepage | https://polyjuice.io |
| repository | https://github.com/polyjuicelab/Nginx-X402 |
| max_upload_size | |
| id | 1937298 |
| size | 3,706,436 |
Pure Rust implementation of Nginx module for x402 HTTP micropayment protocol.
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
# 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
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;
}
}
}
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 over x402_network if 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) or pass (default: error)x402_metrics on|off - Enable Prometheus metrics endpointAdd 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 histogramscrape_configs:
- job_name: 'nginx-x402'
static_configs:
- targets: ['your-nginx-server:80']
metrics_path: '/metrics'
cargo test
All tests run without requiring nginx source or a running nginx instance.
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)AGPL-3.0