# Nginx vs Apache for Drupal Appliances: Making the Memory-Conscious Choice
## The Appliance Context
When building a Drupal-based appliance with integrated LLM capabilities, every megabyte of RAM matters. The choice between Apache and Nginx isn't just about web server preferences—it's about maximizing resources for the AI components that customers are actually paying for.
## The Memory Economics
In our appliance architecture, we're looking at:
- **Target Hardware**: 16-32GB RAM mini PC
- **LLM Requirements**: 8-12GB for Ollama models
- **Drupal Stack**: 4-6GB for database and PHP
- **OS Overhead**: ~200MB (Debian minimal)
This leaves us with a tight memory budget where web server efficiency directly impacts LLM performance.
### Memory Comparison
- **Apache**: 50-100MB baseline + 20-50MB per worker process
- **Nginx**: 10-20MB baseline + minimal per-connection overhead
For a typical appliance load (10-50 concurrent users), this translates to 100-200MB memory savings with Nginx—enough to run larger language models or handle more concurrent AI queries.
## Why Nginx Makes Sense for Appliances
### Resource Efficiency
Nginx's event-driven architecture uses significantly less memory per connection. In an appliance where customers might leave multiple browser tabs open or run automated processes, this efficiency compounds.
### Reverse Proxy Excellence
Our appliance needs to route traffic to multiple services:
- Drupal (port 8080 via PHP-FPM)
- Management dashboard (port 9000)
- Ollama API endpoints (port 11434)
- Solr admin interface (port 8983)
Nginx handles this routing cleanly with simple location blocks, while Apache would require more complex virtual host configurations.
### Static Asset Performance
Drupal sites generate significant static asset traffic (CSS, JS, images). Nginx serves these with lower CPU overhead, keeping more processing power available for LLM operations.
### Configuration Predictability
For an appliance that needs to be remotely supportable, Nginx's more explicit configuration style reduces troubleshooting complexity. No hunting through .htaccess files or dealing with module interaction surprises.
## The Drupal Integration Challenge
### What We're Giving Up
Moving from Apache means losing some Drupal ecosystem advantages:
- Native .htaccess support
- mod_php's tight integration
- Familiar debugging workflows
- Some contrib modules assume Apache
### What We're Gaining
- Better resource utilization for our primary use case
- Cleaner separation between web server and PHP processes
- More predictable performance characteristics
- Simplified SSL/TLS termination
## Critical Nginx + Drupal Gotchas
### 1. Clean URLs Configuration
**Problem**: Drupal's clean URLs rely on URL rewriting that works differently in Nginx.
**Solution**: Proper location block configuration:
```nginx
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
```
**Gotcha**: Test thoroughly with Drupal's URL alias system and ensure all contrib modules that generate custom URLs work correctly.
### 2. PHP-FPM Integration
**Problem**: Unlike Apache's mod_php, Nginx requires separate PHP-FPM process management.
**Solution**: Proper PHP-FPM pool configuration with appropriate memory limits and process counts.
**Gotcha**: PHP-FPM process limits must account for LLM memory usage. Too many PHP processes can starve the Ollama service.
### 3. File Upload Handling
**Problem**: Large file uploads (media, backups) can timeout or fail with default Nginx settings.
**Solution**: Configure appropriate timeouts and body size limits:
```nginx
client_max_body_size 100M;
client_body_timeout 120s;
```
**Gotcha**: These limits affect both Drupal file uploads and management dashboard backup uploads.
### 4. Security Header Management
**Problem**: Drupal modules may expect Apache-style header manipulation.
**Solution**: Explicitly configure security headers in Nginx:
```nginx
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
```
**Gotcha**: Some Drupal security modules may not recognize Nginx header configurations, potentially causing false positives in security scans.
### 5. .htaccess Translation
**Problem**: Drupal contrib modules that rely on .htaccess rules will break.
**Solution**: Manually translate .htaccess rules to Nginx location blocks, or choose alternatives.
**Gotcha**: This is an ongoing maintenance issue—module updates may introduce new .htaccess requirements that need manual translation.
### 6. Performance Module Compatibility
**Problem**: Drupal performance modules like AdvAgg or Boost may have Apache-specific optimizations.
**Solution**: Test caching and aggregation modules thoroughly, potentially switching to Nginx-compatible alternatives.
**Gotcha**: Some performance gains may be lost if switching away from well-optimized Apache-specific modules.
## Implementation Strategy
### Phase 1: Basic Configuration
- Get Drupal running with Nginx + PHP-FPM
- Implement clean URL rewriting
- Configure file upload limits
- Test core Drupal functionality
### Phase 2: Appliance Integration
- Add reverse proxy rules for management dashboard
- Configure SSL termination for all services
- Implement security headers
- Test backup and restore workflows
### Phase 3: Optimization
- Fine-tune PHP-FPM pool settings
- Optimize static asset caching
- Configure rate limiting for API endpoints
- Performance testing under appliance load conditions
## Risk Mitigation
### Testing Requirements
- Full Drupal module compatibility testing
- Load testing with concurrent LLM operations
- Backup/restore procedure validation
- Remote management interface testing
### Fallback Plan
Keep Apache configuration as backup during development. If critical incompatibilities emerge that would delay MVP, we can revert to Apache and optimize memory usage through other means (PHP opcache tuning, database optimization).
### Documentation Needs
- Nginx-specific Drupal troubleshooting guide
- Module compatibility matrix
- Performance tuning checklist
- Remote support procedures
## Decision Rationale
The 100-200MB memory savings from Nginx directly translates to better LLM performance—the primary differentiator of our appliance. While the Apache ecosystem integration is more mature, the resource efficiency gains justify the additional configuration complexity.
For an appliance product where we control the entire stack, the Nginx learning curve is a one-time cost that pays dividends in every deployed unit's performance.
The key is thorough testing and documentation to ensure we can support this configuration remotely and reliably.