This commit is contained in:
Iliyan Angelov
2025-11-24 16:47:37 +02:00
parent d7ff5c71e6
commit 0b1cabcfaf
45 changed files with 2021 additions and 28 deletions

218
nginx.conf Normal file
View File

@@ -0,0 +1,218 @@
# Production Nginx Configuration for GNX Soft (Docker)
# This configuration is for nginx running on the host machine
# It proxies to Docker containers: backend (1086) and frontend (1087)
#
# IMPORTANT PORT CONFIGURATION:
# - Backend (Django): Only accessible on port 1086 (internal)
# - Frontend (Next.js): Only accessible on port 1087 (internal)
# - Nginx: Public access on ports 80 (HTTP) and 443 (HTTPS)
# - Ports 1086 and 1087 should be blocked from external access by firewall
# Frontend - Next.js running in Docker on port 1087
# All frontend requests (/) are proxied here
upstream frontend {
server 127.0.0.1:1087;
keepalive 64;
}
# Backend - Django running in Docker on port 1086
# All API requests (/api/) and admin (/admin/) are proxied here
upstream backend_internal {
server 127.0.0.1:1086;
keepalive 64;
}
# Redirect HTTP to HTTPS
server {
listen 80;
listen [::]:80;
server_name gnxsoft.com www.gnxsoft.com;
# Let's Encrypt validation
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Redirect all other traffic to HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}
# HTTPS Server
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gnxsoft.com www.gnxsoft.com;
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/gnxsoft.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gnxsoft.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# Security Headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
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 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;
limit_req_status 429;
# Client settings
client_max_body_size 10M;
client_body_timeout 30s;
client_header_timeout 30s;
# Logging
access_log /var/log/nginx/gnxsoft_access.log;
error_log /var/log/nginx/gnxsoft_error.log warn;
# Root location - Frontend (Next.js on port 1087)
location / {
limit_req zone=general_limit burst=50 nodelay;
proxy_pass http://frontend;
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;
proxy_cache_bypass $http_upgrade;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# API Proxy - Frontend talks to backend through this proxy
# Backend runs in Docker on port 1086 (internal only)
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
# Internal proxy to backend Docker container (127.0.0.1:1086)
proxy_pass http://backend_internal/api/;
proxy_http_version 1.1;
# Backend sees request as coming from nginx
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;
# Add internal API key (will be replaced by docker-start.sh)
set $api_key "PLACEHOLDER_INTERNAL_API_KEY";
proxy_set_header X-Internal-API-Key $api_key;
# 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 from Docker volume
location /media/ {
alias /home/gnx/Desktop/GNX-WEB/backEnd/media/;
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
# Security - deny execution of scripts
location ~ \.(php|py|pl|sh)$ {
deny all;
}
}
# Static files - Served from Docker volume
location /static/ {
alias /home/gnx/Desktop/GNX-WEB/backEnd/staticfiles/;
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;
}
# Admin panel - Proxy to backend (with IP restriction)
location /admin/ {
# IP restriction is handled by Django middleware
# Add internal API key (will be replaced by docker-start.sh)
set $api_key "PLACEHOLDER_INTERNAL_API_KEY";
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;
}
# Deny access to hidden files
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Health check endpoint
location /health {
access_log off;
return 200 "OK\n";
add_header Content-Type text/plain;
}
}
# ==============================================================================
# IMPORTANT NOTES:
# ==============================================================================
# 1. Backend runs in Docker on port 1086 (internal only)
# 2. Frontend runs in Docker on port 1087
# 3. Nginx runs on host and proxies to Docker containers
# 4. Firewall should BLOCK external access to ports 1086 and 1087
# 5. Only nginx (ports 80, 443) should be accessible from internet
# 6. Set INTERNAL_API_KEY environment variable in nginx config or systemd service
# 7. Update media/static paths to match your actual deployment location
# ==============================================================================