#!/bin/bash # Minimales Nginx-Setup für statische Frontend Dateien set -e echo "=== Nginx Static Frontend Setup ===" FRONTEND_DIR="/opt/zmb-webui/frontend/out" GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # Verify frontend export exists echo -e "${YELLOW}1. Checking frontend export...${NC}" if [ ! -d "$FRONTEND_DIR" ]; then echo -e "${RED}✗ Frontend export not found: $FRONTEND_DIR${NC}" exit 1 fi FILE_COUNT=$(find "$FRONTEND_DIR" -type f | wc -l) echo -e "${GREEN}✓ Frontend export found ($FILE_COUNT files)${NC}" # Configure nginx echo -e "${YELLOW}2. Configuring nginx...${NC}" mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled sudo tee /etc/nginx/sites-available/zmb-webui > /dev/null <<'NGINX_CONF' server { listen 9090; server_name _; # 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; # Compression gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/json application/javascript; gzip_comp_level 5; # Static assets cache (7 days) location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { root /opt/zmb-webui/frontend/out; expires 7d; add_header Cache-Control "public, max-age=604800, immutable"; access_log off; } # Frontend static pages (SPA routing) location / { root /opt/zmb-webui/frontend/out; try_files $uri $uri/ /index.html; add_header Cache-Control "public, max-age=3600"; } # API routes → Backend location /api/ { proxy_pass http://localhost:8000; 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_connect_timeout 10s; proxy_read_timeout 30s; } # Health check location /health { proxy_pass http://localhost:8000; access_log off; } # Logging access_log /var/log/nginx/zmb-webui-access.log combined; error_log /var/log/nginx/zmb-webui-error.log warn; } NGINX_CONF echo -e "${GREEN}✓ Nginx configured${NC}" # Enable nginx site echo -e "${YELLOW}3. Enabling nginx site...${NC}" sudo rm -f /etc/nginx/sites-enabled/default sudo ln -sf /etc/nginx/sites-available/zmb-webui /etc/nginx/sites-enabled/zmb-webui echo -e "${GREEN}✓ Nginx site enabled${NC}" # Test and reload nginx echo -e "${YELLOW}4. Testing and reloading nginx...${NC}" sudo nginx -t 2>&1 | grep -v "warning\|redundant" sudo systemctl reload nginx echo -e "${GREEN}✓ Nginx reloaded${NC}" # Clean up old services echo -e "${YELLOW}5. Cleaning up Node.js service (if exists)...${NC}" if sudo systemctl is-enabled zmb-webui-frontend 2>/dev/null; then sudo systemctl disable zmb-webui-frontend sudo systemctl stop zmb-webui-frontend 2>/dev/null || true sudo rm -f /etc/systemd/system/zmb-webui-frontend.service sudo systemctl daemon-reload fi echo -e "${GREEN}✓ Cleanup complete${NC}" # Verify echo -e "${YELLOW}6. Testing connectivity...${NC}" sleep 1 if curl -s http://localhost:9090 > /dev/null 2>&1; then echo -e "${GREEN}✓ Frontend accessible on port 9090${NC}" else echo -e "${YELLOW}⚠ Testing with curl (may need time to warm up)${NC}" fi # Summary echo "" echo -e "${GREEN}=== Setup Complete ===${NC}" echo "" echo "Frontend: http://$(hostname -I | awk '{print $1}'):9090" echo "API: http://localhost:8000" echo "" echo "Service: systemctl reload nginx" echo "Logs: tail -f /var/log/nginx/zmb-webui-*.log" echo ""