Add default backend page for unmatched domains
All checks were successful
HAProxy Manager Build and Push / Build-and-Push (push) Successful in 37s

- Add default backend template (hap_default_backend.tpl)
- Add customizable default page HTML template (default_page.html)
- Modify generate_config() to include default backend for unmatched domains
- Add environment variables for customizing default page content:
  - HAPROXY_DEFAULT_PAGE_TITLE
  - HAPROXY_DEFAULT_MAIN_MESSAGE
  - HAPROXY_DEFAULT_SECONDARY_MESSAGE
- Update README with documentation and examples
- Ensure backward compatibility with existing configurations
- Remove email contact link as requested
This commit is contained in:
jknapp 2025-07-11 19:10:05 -07:00
parent ef488a253d
commit 27f3f8959b
4 changed files with 163 additions and 1 deletions

View File

@ -28,6 +28,7 @@ docker run -d -p 80:80 -p 443:443 -p 8000:8000 -v lets-encrypt:/etc/letsencrypt
- **NEW**: Certificate download endpoints for other services
- **NEW**: Comprehensive error logging and alerting system
- **NEW**: Certificate status monitoring with expiration dates
- **NEW**: Default backend page for unmatched domains
## Security
@ -326,6 +327,34 @@ tail -f /var/log/haproxy-manager-errors.log
| Variable | Description | Default |
|----------|-------------|---------|
| `HAPROXY_API_KEY` | API key for authentication (optional) | None (no auth) |
| `HAPROXY_DEFAULT_PAGE_TITLE` | Title for the default page | Site Not Configured |
| `HAPROXY_DEFAULT_MAIN_MESSAGE` | Main message on the default page | This domain has not been configured yet. Please contact your system administrator to set up this website. |
| `HAPROXY_DEFAULT_SECONDARY_MESSAGE` | Secondary message on the default page | If you believe this is an error, please check the domain name and try again. |
## Default Backend Configuration
When a domain is accessed that hasn't been configured in HAProxy, the system will serve a default page instead of showing an error. This default page:
- Informs visitors that the site is not configured
- Displays the domain name and current timestamp
- Is fully customizable through environment variables
### Customizing the Default Page
You can customize the default page by setting environment variables:
```bash
docker run -d \
-p 80:80 -p 443:443 -p 8000:8000 \
-v lets-encrypt:/etc/letsencrypt \
-v haproxy:/etc/haproxy \
-e HAPROXY_API_KEY=your-secure-api-key-here \
-e HAPROXY_DEFAULT_PAGE_TITLE="Website Coming Soon" \
-e HAPROXY_DEFAULT_MAIN_MESSAGE="This website is currently under construction and will be available soon." \
-e HAPROXY_DEFAULT_SECONDARY_MESSAGE="Please check back later or contact us for more information." \
--name haproxy-manager \
repo.anhonesthost.net/cloud-hosting-platform/haproxy-manager-base:latest
```
## Example Usage

View File

@ -728,7 +728,12 @@ def generate_config():
config_parts.append(letsencrypt_acl)
config_acls = []
config_backends = []
# Add domain configurations
# Add default backend rule (will be used when no domain matches)
default_rule = " # Default backend for unmatched domains\n default_backend default-backend\n"
config_parts.append(default_rule)
# Add domain configurations
for domain in domains:
if not domain['backend_name']:
logger.warning(f"Skipping domain {domain['domain']} - no backend name")
@ -782,6 +787,31 @@ def generate_config():
# Add LetsEncrypt Backend
letsencrypt_backend = template_env.get_template('hap_letsencrypt_backend.tpl').render()
config_parts.append(letsencrypt_backend)
# Add Default Backend
try:
# Render the default page template with customizable content
default_page_template = template_env.get_template('default_page.html')
default_page_content = default_page_template.render(
page_title=os.environ.get('HAPROXY_DEFAULT_PAGE_TITLE', 'Site Not Configured'),
main_message=os.environ.get('HAPROXY_DEFAULT_MAIN_MESSAGE', 'This domain has not been configured yet. Please contact your system administrator to set up this website.'),
secondary_message=os.environ.get('HAPROXY_DEFAULT_SECONDARY_MESSAGE', 'If you believe this is an error, please check the domain name and try again.')
)
default_page_content = default_page_content.replace('"', '\\"').replace('\n', '\\n')
default_backend = template_env.get_template('hap_default_backend.tpl').render(
default_page_content=default_page_content
)
config_parts.append(default_backend)
except Exception as e:
logger.error(f"Error generating default backend: {e}")
# Fallback to a simple default backend
fallback_backend = '''# Default backend for unmatched domains
backend default-backend
mode http
option http-server-close
http-response set-header Content-Type text/html
http-response set-body "<!DOCTYPE html><html><head><title>Site Not Configured</title></head><body><h1>Site Not Configured</h1><p>This domain has not been configured yet.</p></body></html>"'''
config_parts.append(fallback_backend)
# Add Backends
config_parts.append('\n' .join(config_backends) + '\n')
# Write complete configuration to tmp

View File

@ -0,0 +1,91 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ page_title }}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
text-align: center;
padding: 50px 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
margin: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
max-width: 600px;
width: 100%;
}
.icon {
font-size: 64px;
margin-bottom: 20px;
display: block;
}
h1 {
color: #e74c3c;
margin-bottom: 20px;
font-size: 2.2em;
font-weight: 600;
}
p {
color: #555;
line-height: 1.7;
margin-bottom: 15px;
font-size: 1.1em;
}
.contact {
background: linear-gradient(135deg, #3498db, #2980b9);
color: white;
padding: 12px 24px;
border-radius: 6px;
text-decoration: none;
display: inline-block;
margin-top: 25px;
font-weight: 500;
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.contact:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
}
.domain-info {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 6px;
padding: 15px;
margin: 20px 0;
font-family: 'Courier New', monospace;
color: #495057;
}
</style>
</head>
<body>
<div class="container">
<span class="icon">⚠️</span>
<h1>{{ page_title }}</h1>
<p>{{ main_message }}</p>
<p>{{ secondary_message }}</p>
<div class="domain-info">
<strong>Domain:</strong> <span id="domain"></span><br>
<strong>Time:</strong> <span id="timestamp"></span>
</div>
</div>
<script>
// Display the current domain and timestamp
document.getElementById('domain').textContent = window.location.hostname;
document.getElementById('timestamp').textContent = new Date().toLocaleString();
</script>
</body>
</html>

View File

@ -0,0 +1,12 @@
# Default backend for unmatched domains
backend default-backend
mode http
option http-server-close
http-request set-header X-Forwarded-Proto https if { ssl_fc }
http-request set-header X-Forwarded-Port %[dst_port]
http-request set-header X-Forwarded-For %[src]
http-request set-header X-Real-IP %[src]
# Serve the default page HTML response
http-response set-header Content-Type text/html
http-response set-body "{{ default_page_content }}"