mnemo_cards/tools/deploy/generate-nginx-configs.sh
2025-11-27 22:16:53 +03:00

250 lines
7.2 KiB
Bash
Executable file

#!/bin/bash
# Script to generate all nginx configurations for the project
# This ensures consistent configuration across all deployments
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
OUTPUT_DIR="$SCRIPT_DIR/generated_configs"
echo "🔧 Generating nginx configurations..."
echo "Output directory: $OUTPUT_DIR"
echo ""
# Create output directory
mkdir -p "$OUTPUT_DIR"
# Function to generate nginx configuration for a service
generate_nginx_config() {
local domain=$1
local upstream_port=$2
local service_name=$3
local output_file="$OUTPUT_DIR/$domain.conf"
echo "📝 Generating config for $domain (port $upstream_port)..."
cat > "$output_file" << EOF
# Nginx configuration for $domain
# Generated by generate-nginx-configs.sh on $(date)
# Service: $service_name
server {
listen 80;
server_name $domain;
return 301 https://\$server_name\$request_uri;
}
server {
listen 443 ssl http2;
server_name $domain;
# SSL configuration
ssl_certificate /etc/letsencrypt/live/mnemo-cards.online/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mnemo-cards.online/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
EOF
# Add service-specific settings
case $service_name in
vscode)
cat >> "$output_file" << EOF
# VSCode Server specific settings
client_max_body_size 100M;
# Static files - no rate limiting
location ~ ^/(static|out|node_modules)/ {
proxy_pass http://127.0.0.1:$upstream_port;
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_valid 200 1h;
add_header Cache-Control "public, max-age=3600";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# Static file extensions - no rate limiting
location ~ \.(js|css|woff|woff2|ttf|eot|png|jpg|jpeg|gif|svg|ico|webp|map|json)$ {
proxy_pass http://127.0.0.1:$upstream_port;
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_valid 200 1h;
add_header Cache-Control "public, max-age=3600";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# Main proxy location
location / {
proxy_pass http://127.0.0.1:$upstream_port;
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_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffering off;
proxy_request_buffering off;
}
EOF
;;
forgejo)
cat >> "$output_file" << EOF
# Forgejo (Git) specific settings
client_max_body_size 100M;
# Main proxy location
location / {
proxy_pass http://127.0.0.1:$upstream_port;
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_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffering off;
proxy_request_buffering off;
}
EOF
;;
webapp)
cat >> "$output_file" << EOF
# Web app specific settings
client_max_body_size 1000M;
root /var/www/mnemo_cards;
index index.html;
location / {
try_files \$uri \$uri/ /index.html;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# ACME challenge for certificate renewal
location /.well-known/acme-challenge/ {
root /var/www/html;
try_files \$uri =404;
}
EOF
;;
api)
cat >> "$output_file" << EOF
# API Backend specific settings
client_max_body_size 100M;
# CORS headers for API
add_header Access-Control-Allow-Origin "*" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS" always;
add_header Access-Control-Allow-Headers "Origin, Content-Type, Accept, Authorization, user_token, request_token, app_version" always;
add_header Access-Control-Expose-Headers "Authorization" always;
add_header Access-Control-Max-Age "86400" always;
# Handle preflight OPTIONS requests
if (\$request_method = OPTIONS) {
return 204;
}
# Proxy to backend server (HTTPS for API, HTTP for others)
location / {
proxy_pass https://127.0.0.1:$upstream_port;
proxy_ssl_verify off; # Skip SSL verification for local connection
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_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
EOF
;;
esac
# Close server block
cat >> "$output_file" << EOF
# Security - deny access to hidden files
location ~ /\. {
deny all;
}
}
EOF
echo "✅ Generated $output_file"
}
# Generate configurations for all services
# Main web application
generate_nginx_config "mnemo-cards.online" "" "webapp"
# API Backend Server
generate_nginx_config "api.mnemo-cards.online" "8081" "api"
# VSCode Server
generate_nginx_config "vscode.mnemo-cards.online" "8443" "vscode"
# Forgejo (Git server)
generate_nginx_config "code.mnemo-cards.online" "3000" "forgejo"
echo ""
echo "📊 Validating generated configurations..."
# Test configurations syntax (basic validation only)
echo "Testing generated configurations..."
for config in "$OUTPUT_DIR"/*.conf; do
config_name=$(basename "$config")
echo "Testing $config_name..."
# Basic syntax check - look for obvious errors
if grep -q "server {" "$config" && grep -q "}" "$config"; then
echo "✅ Basic structure OK"
else
echo "❌ Missing server block or closing brace"
fi
done
echo ""
echo "✅ All configurations generated successfully!"
echo ""
echo "📁 Generated files in: $OUTPUT_DIR"
echo ""
echo "🚀 Use deploy-nginx-configs.sh to deploy these configurations to the server"