MogensR's picture
Create config/nginx.conf
3c486b8
# BackgroundFX Pro Nginx Configuration
# Production-ready configuration with caching, compression, and security
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# Load dynamic modules
load_module modules/ngx_http_image_filter_module.so;
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging
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"';
access_log /var/log/nginx/access.log main buffer=32k flush=5s;
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 100M;
# SSL Settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
# Gzip Settings
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript
application/json application/javascript application/xml+rss
application/rss+xml application/atom+xml image/svg+xml
text/x-js text/x-cross-domain-policy application/x-font-ttf
application/x-font-opentype application/vnd.ms-fontobject
image/x-icon;
# Cache Settings
proxy_cache_path /var/cache/nginx/proxy levels=1:2 keys_zone=proxy_cache:100m
max_size=10g inactive=60m use_temp_path=off;
proxy_cache_path /var/cache/nginx/images levels=1:2 keys_zone=images:100m
max_size=50g inactive=30d use_temp_path=off;
# Rate Limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=upload:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
# Upstream Servers
upstream api_backend {
least_conn;
server api1:8000 max_fails=3 fail_timeout=30s;
server api2:8000 max_fails=3 fail_timeout=30s backup;
keepalive 32;
}
upstream web_backend {
least_conn;
server web1:3000 max_fails=3 fail_timeout=30s;
server web2:3000 max_fails=3 fail_timeout=30s backup;
keepalive 32;
}
upstream websocket_backend {
ip_hash;
server ws1:8001;
server ws2:8001;
}
# Redirect HTTP to HTTPS
server {
listen 80;
listen [::]:80;
server_name backgroundfx.pro www.backgroundfx.pro;
return 301 https://$server_name$request_uri;
}
# Main HTTPS Server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name backgroundfx.pro www.backgroundfx.pro;
# SSL Certificate
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_trusted_certificate /etc/nginx/ssl/chain.pem;
# Security Headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self' https:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://unpkg.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self' https://api.backgroundfx.pro wss://ws.backgroundfx.pro" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Root and Index
root /var/www/html;
index index.html;
# API Endpoints
location /api/ {
limit_req zone=api burst=20 nodelay;
limit_conn addr 10;
proxy_pass http://api_backend;
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 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;
proxy_busy_buffers_size 8k;
# Cache for GET requests
proxy_cache proxy_cache;
proxy_cache_valid 200 1m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_bypass $http_cache_control;
add_header X-Cache-Status $upstream_cache_status;
}
# Upload Endpoint
location /api/upload {
limit_req zone=upload burst=5 nodelay;
client_max_body_size 100M;
client_body_buffer_size 1M;
proxy_pass http://api_backend;
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;
# Longer timeout for uploads
proxy_connect_timeout 120s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
# WebSocket Endpoint
location /ws/ {
proxy_pass http://websocket_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
# WebSocket timeouts
proxy_connect_timeout 7d;
proxy_send_timeout 7d;
proxy_read_timeout 7d;
}
# Static Files
location /static/ {
alias /var/www/static/;
expires 30d;
add_header Cache-Control "public, immutable";
# Enable gzip for static files
gzip_static on;
# CORS for fonts
location ~* \.(eot|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
expires 30d;
}
}
# Processed Images with Caching
location /images/ {
alias /var/www/images/;
expires 7d;
add_header Cache-Control "public, max-age=604800";
# Image processing on the fly
location ~ ^/images/resize/(\d+)x(\d+)/(.+)$ {
alias /var/www/images/$3;
image_filter resize $1 $2;
image_filter_jpeg_quality 85;
image_filter_buffer 20M;
error_page 415 = /empty;
}
# Cache processed images
proxy_cache images;
proxy_cache_valid 200 30d;
proxy_cache_valid 404 1h;
}
# Health Check
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# Metrics Endpoint (internal only)
location /metrics {
allow 10.0.0.0/8;
deny all;
proxy_pass http://api_backend/metrics;
}
# Web Application
location / {
proxy_pass http://web_backend;
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 Connection "";
# Next.js specific
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
# Error Pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /404.html {
root /var/www/error;
internal;
}
location = /50x.html {
root /var/www/error;
internal;
}
location = /empty {
empty_gif;
}
}
# CDN Server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name cdn.backgroundfx.pro;
ssl_certificate /etc/nginx/ssl/cdn-fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/cdn-privkey.pem;
root /var/www/cdn;
location / {
expires 1y;
add_header Cache-Control "public, immutable";
add_header Access-Control-Allow-Origin "*";
# Enable gzip
gzip_static on;
# Serve pre-compressed files if available
gzip_types *;
}
}
}