Fix Varnish backend to use server IP instead of 127.0.0.1

Apache VirtualHosts listen on the public IP, not localhost. Script now detects primary server IP and configures Varnish backend accordingly.
This commit is contained in:
cschantz
2026-01-21 22:00:16 -05:00
parent 27567c62ac
commit 212af57746
+13 -1
View File
@@ -477,13 +477,22 @@ install_varnish() {
configure_varnish_vcl() {
print_banner "Configuring Varnish VCL"
# Get server's primary IP address for Apache backend
local server_ip=$(hostname -I | awk '{print $1}')
if [ -z "$server_ip" ]; then
server_ip="127.0.0.1"
print_warning "Could not detect server IP, using 127.0.0.1"
else
print_info "Detected server IP: $server_ip"
fi
# Simple VCL with comprehensive admin page bypasses
cat > "$VARNISH_VCL" <<'EOFVCL'
vcl 4.1;
# Backend definition - Apache on port 81 (ea-nginx stock port)
backend default {
.host = "127.0.0.1";
.host = "SERVER_IP_PLACEHOLDER";
.port = "81";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
@@ -606,6 +615,9 @@ sub vcl_deliver {
}
EOFVCL
# Replace placeholder with actual server IP
sed -i "s/SERVER_IP_PLACEHOLDER/$server_ip/g" "$VARNISH_VCL"
# Test VCL configuration
# Note: /tmp is usually mounted with noexec on cPanel servers, so use -n parameter
# to specify a working directory outside /tmp