diff --git a/modules/performance/nginx-varnish-manager.sh b/modules/performance/nginx-varnish-manager.sh index 476da59..a620288 100755 --- a/modules/performance/nginx-varnish-manager.sh +++ b/modules/performance/nginx-varnish-manager.sh @@ -721,9 +721,10 @@ test_domain() { # Quick proof that caching is actually working proof_of_caching() { - print_banner "Proof of Caching Test" + print_banner "Proof of Caching Test - HTTP & HTTPS" - echo "This test proves caching is working by showing MISS → HIT pattern" + echo "This test proves BOTH HTTP and HTTPS are cached by Varnish" + echo "Testing pattern: MISS → HIT (proves caching is working)" echo "" # Get first domain @@ -740,79 +741,152 @@ proof_of_caching() { local server_ip=$(hostname -I | awk '{print $1}') echo "Testing domain: $domain" - echo "Server IP: $server_ip (testing direct to bypass CDN)" + echo "Server IP: $server_ip (bypassing CDN for accurate results)" echo "" - - # Test common static files - local test_files=( - "/robots.txt" - "/favicon.ico" - "/" - ) - - local cache_working=0 - - for test_path in "${test_files[@]}"; do - local url="http://${domain}${test_path}" - - echo "Testing: $test_path" - - # First request - should be MISS - # Use --resolve to bypass CDN and test directly to server - local first_request=$(curl -s -I --max-time 5 --resolve "${domain}:80:${server_ip}" "$url" 2>/dev/null) - local first_cache=$(echo "$first_request" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') - - # Second request - should be HIT - sleep 0.5 - local second_request=$(curl -s -I --max-time 5 --resolve "${domain}:80:${server_ip}" "$url" 2>/dev/null) - local second_cache=$(echo "$second_request" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') - - # Check if we got valid responses - if [ -z "$first_cache" ] || [ -z "$second_cache" ]; then - echo " Status: Skipped (no cache headers)" - echo "" - continue - fi - - # Display results - echo " Request 1: X-Cache: $first_cache" - echo " Request 2: X-Cache: $second_cache" - - # Check if caching worked - if [ "$first_cache" = "MISS" ] && [ "$second_cache" = "HIT" ]; then - print_success " ✓ CACHING WORKS! (MISS → HIT)" - cache_working=1 - elif [ "$second_cache" = "HIT" ]; then - print_success " ✓ Cache HIT detected" - cache_working=1 - elif [ "$first_cache" = "MISS" ] && [ "$second_cache" = "MISS" ]; then - print_warning " ⚠ No caching (both MISS) - may be dynamic content" - else - print_info " ℹ Status: $first_cache → $second_cache" - fi - - echo "" - done - echo "════════════════════════════════════════════════════════════" - if [ "$cache_working" -eq 1 ]; then + local http_cache_working=0 + local https_cache_working=0 + + # Test file that's most likely to be cacheable + local test_file="/robots.txt" + + # ============================================================================ + # TEST 1: HTTP (Port 80) + # ============================================================================ + echo "" + print_info "TEST 1: HTTP Protocol (Port 80)" + echo "────────────────────────────────────────────────────────────" + + # Clear this specific URL from cache first + varnishadm "ban req.url ~ ${test_file}" >/dev/null 2>&1 + + sleep 1 + + echo "Testing: http://${domain}${test_file}" + echo "" + + # First HTTP request - should be MISS + local http_req1=$(curl -s -I --max-time 5 --resolve "${domain}:80:${server_ip}" "http://${domain}${test_file}" 2>/dev/null) + local http_cache1=$(echo "$http_req1" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') + local http_served1=$(echo "$http_req1" | grep -i "X-Served-By:" | awk '{print $2}' | tr -d '\r') + + echo " Request 1:" + echo " X-Cache: ${http_cache1:-NOT FOUND}" + echo " X-Served-By: ${http_served1:-NOT FOUND}" + + # Second HTTP request - should be HIT + sleep 1 + local http_req2=$(curl -s -I --max-time 5 --resolve "${domain}:80:${server_ip}" "http://${domain}${test_file}" 2>/dev/null) + local http_cache2=$(echo "$http_req2" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') + local http_served2=$(echo "$http_req2" | grep -i "X-Served-By:" | awk '{print $2}' | tr -d '\r') + local http_hits=$(echo "$http_req2" | grep -i "X-Cache-Hits:" | awk '{print $2}' | tr -d '\r') + + echo " Request 2:" + echo " X-Cache: ${http_cache2:-NOT FOUND}" + echo " X-Served-By: ${http_served2:-NOT FOUND}" + if [ -n "$http_hits" ]; then + echo " X-Cache-Hits: $http_hits" + fi + echo "" + + # Evaluate HTTP caching + if [ "$http_cache1" = "MISS" ] && [ "$http_cache2" = "HIT" ] && [ "$http_served2" = "Varnish" ]; then + print_success "✓ HTTP CACHING CONFIRMED: MISS → HIT via Varnish" + http_cache_working=1 + elif [ "$http_cache2" = "HIT" ] && [ "$http_served2" = "Varnish" ]; then + print_success "✓ HTTP via Varnish (cache already populated)" + http_cache_working=1 + else + print_error "✗ HTTP caching not detected" + fi + + # ============================================================================ + # TEST 2: HTTPS (Port 443) + # ============================================================================ + echo "" + print_info "TEST 2: HTTPS Protocol (Port 443)" + echo "────────────────────────────────────────────────────────────" + + # Clear cache again + varnishadm "ban req.url ~ ${test_file}" >/dev/null 2>&1 + + sleep 1 + + echo "Testing: https://${domain}${test_file}" + echo "" + + # First HTTPS request - should be MISS + local https_req1=$(curl -sk -I --max-time 5 --resolve "${domain}:443:${server_ip}" "https://${domain}${test_file}" 2>/dev/null) + local https_cache1=$(echo "$https_req1" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') + local https_served1=$(echo "$https_req1" | grep -i "X-Served-By:" | awk '{print $2}' | tr -d '\r') + + echo " Request 1:" + echo " X-Cache: ${https_cache1:-NOT FOUND}" + echo " X-Served-By: ${https_served1:-NOT FOUND}" + + # Second HTTPS request - should be HIT + sleep 1 + local https_req2=$(curl -sk -I --max-time 5 --resolve "${domain}:443:${server_ip}" "https://${domain}${test_file}" 2>/dev/null) + local https_cache2=$(echo "$https_req2" | grep -i "X-Cache:" | awk '{print $2}' | tr -d '\r') + local https_served2=$(echo "$https_req2" | grep -i "X-Served-By:" | awk '{print $2}' | tr -d '\r') + local https_hits=$(echo "$https_req2" | grep -i "X-Cache-Hits:" | awk '{print $2}' | tr -d '\r') + + echo " Request 2:" + echo " X-Cache: ${https_cache2:-NOT FOUND}" + echo " X-Served-By: ${https_served2:-NOT FOUND}" + if [ -n "$https_hits" ]; then + echo " X-Cache-Hits: $https_hits" + fi + echo "" + + # Evaluate HTTPS caching + if [ "$https_cache1" = "MISS" ] && [ "$https_cache2" = "HIT" ] && [ "$https_served2" = "Varnish" ]; then + print_success "✓ HTTPS CACHING CONFIRMED: MISS → HIT via Varnish" + https_cache_working=1 + elif [ "$https_cache2" = "HIT" ] && [ "$https_served2" = "Varnish" ]; then + print_success "✓ HTTPS via Varnish (cache already populated)" + https_cache_working=1 + else + print_error "✗ HTTPS caching not detected" + fi + + # ============================================================================ + # FINAL VERDICT + # ============================================================================ + echo "" + echo "════════════════════════════════════════════════════════════" + echo "" + + if [ "$http_cache_working" -eq 1 ] && [ "$https_cache_working" -eq 1 ]; then + print_success "🎉 PROOF COMPLETE: Both HTTP and HTTPS are cached by Varnish!" echo "" - print_success "✓ PROOF CONFIRMED: Varnish is caching your sites!" + echo "Traffic Flow Confirmed:" + echo " HTTP: Client → Nginx:80 → Varnish:6081 → Apache:81" + echo " HTTPS: Client → Nginx:443 (SSL) → Varnish:6081 (HTTP) → Apache:81" echo "" - echo "What this means:" - echo " • Static files are being cached by Varnish" - echo " • Repeat visitors get instant responses from cache" - echo " • Server load is reduced for cached content" + echo "Benefits:" + echo " ✓ Static content cached for both protocols" + echo " ✓ Reduced server load and faster response times" + echo " ✓ SSL termination at Nginx, HTTP backends to Varnish" + echo "" + elif [ "$http_cache_working" -eq 1 ]; then + print_warning "⚠ Partial Success: HTTP caching works, but HTTPS may have issues" + echo "" + echo "Try running 'Auto-Fix Issues' (option 4) to resolve HTTPS caching" + echo "" + elif [ "$https_cache_working" -eq 1 ]; then + print_warning "⚠ Partial Success: HTTPS caching works, but HTTP may have issues" + echo "" + echo "Try running 'Auto-Fix Issues' (option 4) to resolve HTTP caching" echo "" else + print_error "✗ Could not confirm caching for either protocol" echo "" - print_warning "⚠ Could not confirm caching" - echo "" - echo "Possible reasons:" - echo " • Tested URLs are dynamic (admin pages, etc.)" - echo " • Cache was already populated (no MISS seen)" - echo " • Try option 7 to clear cache, then test again" + echo "Troubleshooting:" + echo " 1. Run 'Health Check' (option 3) to diagnose issues" + echo " 2. Run 'Auto-Fix Issues' (option 4) to repair configuration" + echo " 3. Check that Varnish service is running: systemctl status varnish" echo "" fi