Multi-Domain Support

Configure multi-domain routing in moclojer to handle different hosts and subdomains. Perfect for multi-tenant applications, staging environments, and API versioning by domain.

moclojer supports routing requests based on the Host header, enabling you to serve different responses for different domains or subdomains on the same server instance. This is essential for multi-tenant applications, environment-specific mocks, and subdomain-based routing.

🎯 How Multi-Domain Routing Works

When a request arrives, moclojer matches endpoints using:

  1. Host (if specified in endpoint configuration)

  2. HTTP Method (GET, POST, etc.)

  3. Path pattern (exact → parameters → wildcards)

flowchart LR
    A[Incoming Request] --> B{Host Match?}
    B -->|Yes| C{Method Match?}
    B -->|No host specified| C
    C -->|Yes| D{Path Match?}
    C -->|No| E[404 Not Found]
    D -->|Yes| F[Return Response]
    D -->|No| E

📝 Basic Configuration

Single Domain Endpoint

Request:

Response:

🌐 Multi-Tenant Applications

Serve different data based on subdomain:

Testing:

🔧 Environment-Specific Mocks

Different responses for staging vs production:

🌍 Subdomain-Based API Versioning

Version your API using subdomains:

🔄 Mixed Configuration

Combine host-specific and host-agnostic endpoints:

🧪 Testing Multi-Domain Locally

Using curl with Host header

Using /etc/hosts for local DNS

Edit /etc/hosts (Linux/Mac) or C:\Windows\System32\drivers\etc\hosts (Windows):

Then access directly:

Using Docker with custom network

📊 Real-World Use Cases

Use Case
Configuration
Benefit

Multi-tenant SaaS

Different host per customer

Isolate customer data

Environment parity

staging/prod hosts

Test environment-specific behavior

API versioning

v1/v2 subdomains

Maintain multiple API versions

Region-specific APIs

us/eu/asia hosts

Simulate regional endpoints

White-label apps

Partner-specific domains

Test branded experiences

✅ Best Practices

Do:

  • ✅ Use host-specific endpoints for multi-tenant isolation

  • ✅ Provide fallback endpoints without host for common resources

  • ✅ Document which domains are expected in production

  • ✅ Test with actual Host headers locally

  • ✅ Use environment variables for dynamic host configuration

Don't:

  • ❌ Mix host-specific and host-agnostic endpoints for the same path without clear intent

  • ❌ Forget to test Host header matching in CI/CD

  • ❌ Hardcode production domains in development mocks

  • ❌ Rely on DNS resolution in tests (use Host headers instead)

🔧 Advanced Patterns

Dynamic Host Matching

Use template variables to respond with the requested host:

Conditional Responses Based on Host

🚨 Important Notes

Host Matching is Exact: The host field requires exact matches. api.example.com will not match www.api.example.com.

Fallback Behavior: Endpoints without a host field act as fallbacks and will match any domain.

Case Sensitivity: Host matching is case-insensitive (follows HTTP spec).

Port Numbers: When specifying hosts, include ports if needed: api.example.com:8080

📚 See Also

Last updated

Was this helpful?