Files
commerce/deploy/nginx/nginx.conf
zeekay 1943f1c093 Hanzo Commerce
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans,
usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0.

Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
2026-09-23 21:52:16 -07:00

267 lines
8.5 KiB
Nginx Configuration File

# Hanzo Commerce - NGINX Configuration
# Reverse proxy with blue-green deployment support
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging format with request timing
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
log_format json escape=json '{'
'"time":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"request":"$request",'
'"status":$status,'
'"body_bytes_sent":$body_bytes_sent,'
'"request_time":$request_time,'
'"upstream_response_time":"$upstream_response_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent"'
'}';
access_log /var/log/nginx/access.log main;
# Performance optimizations
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript
application/xml application/xml+rss text/javascript application/x-javascript;
# Rate limiting zones
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
# ===========================================================================
# UPSTREAM SERVERS (Blue-Green)
# ===========================================================================
# Blue deployment
upstream commerce_blue {
server commerce-blue:8001 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# Green deployment
upstream commerce_green {
server commerce-green:8001 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# Active deployment (switch between blue/green)
# Default to blue, override with environment variable
upstream commerce_active {
server commerce-blue:8001 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# ===========================================================================
# MAIN SERVER BLOCK
# ===========================================================================
server {
listen 80;
listen [::]:80;
server_name _;
# Redirect HTTP to HTTPS in production
# Uncomment for production:
# return 301 https://$host$request_uri;
# Health check endpoint (always available)
location /healthz {
access_log off;
return 200 'OK';
add_header Content-Type text/plain;
}
# Main application
location / {
proxy_pass http://commerce_active;
# Proxy headers
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
# Connection keepalive
proxy_set_header Connection "";
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# Buffering
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
# Rate limiting
limit_req zone=api_limit burst=50 nodelay;
limit_conn conn_limit 100;
}
# API endpoints with stricter limits
location /api/ {
proxy_pass http://commerce_active;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
# API-specific timeouts
proxy_connect_timeout 30s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
# Stricter rate limiting for API
limit_req zone=api_limit burst=20 nodelay;
}
# Webhook endpoints (no rate limiting, longer timeouts)
location /webhook/ {
proxy_pass http://commerce_active;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
# Longer timeouts for webhooks
proxy_connect_timeout 30s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}
# ===========================================================================
# HTTPS SERVER BLOCK (Production)
# ===========================================================================
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name _;
# SSL configuration (update paths for production)
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# Modern SSL settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
# Security headers
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# Health check
location /healthz {
access_log off;
return 200 'OK';
add_header Content-Type text/plain;
}
# Main application (same as HTTP)
location / {
proxy_pass http://commerce_active;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
limit_req zone=api_limit burst=50 nodelay;
limit_conn conn_limit 100;
}
location /api/ {
proxy_pass http://commerce_active;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
proxy_connect_timeout 30s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
limit_req zone=api_limit burst=20 nodelay;
}
location /webhook/ {
proxy_pass http://commerce_active;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
proxy_connect_timeout 30s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}
# Include additional server blocks
include /etc/nginx/conf.d/*.conf;
}