This commit is contained in:
Iliyan Angelov
2025-11-25 02:06:38 +02:00
parent 2f6dca736a
commit 82024016cd
37 changed files with 1800 additions and 1478 deletions

View File

@@ -1,23 +1,31 @@
# Production Nginx Configuration for GNX Soft
# Place this in /etc/nginx/sites-available/gnxsoft
# Symlink to /etc/nginx/sites-enabled/gnxsoft
#
# DEPLOYMENT NOTES:
# 1. Frontend: Next.js production build runs on port 3000
# - Build: npm run build
# - Start: npm start (or use PM2: pm2 start npm --name "gnxsoft-frontend" -- start)
# 2. Backend: Django runs on port 8000 (internal only)
# - Use Gunicorn: gunicorn gnx.wsgi:application --bind 127.0.0.1:8000
# - Or PM2: pm2 start gunicorn --name "gnxsoft-backend" -- gnx.wsgi:application --bind 127.0.0.1:8000
# DEPLOYMENT NOTES (Host Deployment):
# 1. Frontend: Next.js production build runs on port 1087
# - Build: cd frontEnd && npm run build
# - Start: Use start-services.sh script or PM2: PORT=1087 pm2 start npm --name "gnxsoft-frontend" -- start
# 2. Backend: Django runs on port 1086 (internal only)
# - Use start-services.sh script or PM2: gunicorn gnx.wsgi:application --bind 127.0.0.1:1086 --workers 3
# 3. Database: PostgreSQL on host (port 5433 to avoid conflict with Docker instance on 5432)
# 4. Use install-postgresql.sh to install and configure PostgreSQL
# 5. Use start-services.sh to start both backend and frontend services
#
# NOTE: Rate limiting zones must be defined in the main nginx.conf http context
# Add these lines to /etc/nginx/nginx.conf inside the http {} block:
# limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
# limit_req_zone $binary_remote_addr zone=general_limit:10m rate=100r/s;
# Frontend - Public facing (Next.js Production Server)
# Frontend - Public facing (Next.js Production Server on port 1087)
upstream frontend {
server 127.0.0.1:3000;
server 127.0.0.1:1087;
keepalive 64;
}
# Backend - Internal only (Django)
# Backend - Internal only (Django on port 1086)
upstream backend_internal {
server 127.0.0.1:8000;
server 127.0.0.1:1086;
keepalive 64;
}
@@ -61,9 +69,7 @@ server {
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), interest-cohort=()" always;
# Rate Limiting Zones
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=general_limit:10m rate=100r/s;
# Rate Limiting (zones must be defined in main nginx.conf http context)
limit_req_status 429;
# Client settings
@@ -75,7 +81,134 @@ server {
access_log /var/log/nginx/gnxsoft_access.log;
error_log /var/log/nginx/gnxsoft_error.log warn;
# Root location - Frontend (Next.js)
# IMPORTANT: More specific location blocks MUST come before location /
# Order matters in nginx - longest match wins
# API Proxy - Frontend talks to backend ONLY through this internal proxy
# Backend port 1086 is BLOCKED from internet by firewall
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
# Internal proxy to backend (127.0.0.1:1086)
# Backend is NOT accessible from public internet
proxy_pass http://backend_internal/api/;
proxy_http_version 1.1;
# Add internal API key (must match INTERNAL_API_KEY in Django .env)
set $api_key "9hZtPwyScigoBAl59Uvcz_9VztSRC6Zt_6L1B2xTM2M";
proxy_set_header X-Internal-API-Key $api_key;
# Backend sees request as coming from localhost
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-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Hide backend server info
proxy_hide_header X-Powered-By;
proxy_hide_header Server;
# Timeouts
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# CORS headers (if needed)
add_header Access-Control-Allow-Origin "https://gnxsoft.com" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
add_header Access-Control-Allow-Headers "Authorization, Content-Type, X-Internal-API-Key" always;
add_header Access-Control-Allow-Credentials "true" always;
# Handle preflight requests
if ($request_method = 'OPTIONS') {
return 204;
}
}
# Media files (served by nginx directly for better performance)
location /media/ {
alias /var/www/GNX-WEB/backEnd/media/;
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
# Security
location ~ \.(php|py|pl|sh)$ {
deny all;
}
}
# Static files (served by nginx directly)
location /static/ {
alias /var/www/GNX-WEB/backEnd/staticfiles/;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# Next.js image optimization API - must be proxied to Next.js server
# Use regex to match /_next/image with query strings
location ~ ^/_next/image {
proxy_pass http://frontend;
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-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# Preserve query string
proxy_pass_request_headers on;
# Timeouts for image processing
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# Buffer settings for image processing
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
# Cache optimized images
proxy_cache_valid 200 1d;
add_header Cache-Control "public, max-age=86400";
}
# Next.js static files - serve directly from filesystem for better performance
location /_next/static/ {
alias /var/www/GNX-WEB/frontEnd/.next/static/;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
# Correct MIME types
types {
text/css css;
application/javascript js;
application/json json;
font/woff2 woff2;
font/woff woff;
font/ttf ttf;
image/png png;
image/jpeg jpg jpeg;
image/webp webp;
image/svg+xml svg;
}
}
# Frontend public images
location /images/ {
alias /var/www/GNX-WEB/frontEnd/public/images/;
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
# Root location - Frontend (Next.js) - MUST be last
location / {
limit_req zone=general_limit burst=50 nodelay;
@@ -95,72 +228,6 @@ server {
proxy_read_timeout 60s;
}
# API Proxy - Frontend talks to backend ONLY through this internal proxy
# Backend port 8000 is BLOCKED from internet by firewall
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
# Internal proxy to backend (127.0.0.1:8000)
# Backend is NOT accessible from public internet
proxy_pass http://backend_internal/api/;
proxy_http_version 1.1;
# Backend sees request as coming from localhost
proxy_set_header Host $host;
proxy_set_header X-Real-IP 127.0.0.1;
proxy_set_header X-Forwarded-For 127.0.0.1;
proxy_set_header X-Forwarded-Proto $scheme;
# Hide backend server info
proxy_hide_header X-Powered-By;
proxy_hide_header Server;
# Timeouts
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# CORS headers (if needed)
add_header Access-Control-Allow-Origin "https://gnxsoft.com" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
add_header Access-Control-Allow-Headers "Authorization, Content-Type" always;
add_header Access-Control-Allow-Credentials "true" always;
# Handle preflight requests
if ($request_method = 'OPTIONS') {
return 204;
}
}
# Media files (served by nginx directly for better performance)
location /media/ {
alias /var/www/gnxsoft/media/;
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
# Security
location ~ \.(php|py|pl|sh)$ {
deny all;
}
}
# Static files (served by nginx directly)
location /static/ {
alias /var/www/gnxsoft/static/;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# Next.js static files
location /_next/static/ {
proxy_pass http://frontend;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# Deny access to hidden files
location ~ /\. {
deny all;
@@ -168,10 +235,26 @@ server {
log_not_found off;
}
# Deny access to backend admin (extra security)
location /admin {
deny all;
return 404;
# Admin panel - Proxy to backend (with IP restriction)
location /admin/ {
# IP restriction is handled by Django middleware
# Add internal API key (must match INTERNAL_API_KEY in Django .env)
set $api_key "9hZtPwyScigoBAl59Uvcz_9VztSRC6Zt_6L1B2xTM2M";
proxy_set_header X-Internal-API-Key $api_key;
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_pass http://backend_internal;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection "";
# Timeouts
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# Health check endpoint
@@ -185,11 +268,13 @@ server {
# ==============================================================================
# IMPORTANT SECURITY NOTES:
# ==============================================================================
# 1. Backend runs on 127.0.0.1:8000 (internal only)
# 2. Firewall BLOCKS external access to port 8000
# 3. Only nginx can reach backend (internal network)
# 4. Public internet can ONLY access nginx (ports 80, 443)
# 5. All API calls go through nginx proxy (/api/* → 127.0.0.1:8000/api/*)
# 6. Backend IP whitelist middleware ensures only localhost requests
# 1. Backend runs on 127.0.0.1:1086 (internal only)
# 2. Frontend runs on 127.0.0.1:1087 (internal only)
# 3. Firewall BLOCKS external access to ports 1086 and 1087
# 4. Only nginx can reach backend/frontend (internal network)
# 5. Public internet can ONLY access nginx (ports 80, 443)
# 6. All API calls go through nginx proxy (/api/* → 127.0.0.1:1086/api/*)
# 7. Backend IP whitelist middleware ensures only localhost requests
# 8. Rate limiting zones must be added to /etc/nginx/nginx.conf http {} block
# 9. PostgreSQL runs on port 5433 (to avoid conflict with Docker on 5432)
# ==============================================================================