Apache2+mod_wsgi+Django随机出现500错误:远程端无响应关闭连接
I’ve run into this exact intermittent error a few times in production setups, so let’s break down the most likely causes and actionable fixes:
Common Fixes to Try
Tweak Apache’s KeepAlive Settings
This is the #1 culprit for this error. Open your Apache config file (usually/etc/apache2/apache2.confor/etc/httpd/conf/httpd.conf) and adjust these parameters:KeepAliveTimeout: Bump it from the default 5 seconds to 15-20 seconds. This gives slower client requests enough time to finish without Apache closing the connection prematurely.MaxKeepAliveRequests: Increase this to 1000 or higher. Reduces the overhead of spinning up new connections, which cuts down on unexpected disconnects during traffic spikes.
After making changes, restart Apache withsudo systemctl restart apache2.
Optimize mod_wsgi Daemon Process Configuration
If you’re using mod_wsgi in daemon mode, poor process/thread settings can lead to resource exhaustion and connection drops. In your WSGI config, update theWSGIDaemonProcessline to match your server’s resources:WSGIDaemonProcess mysite processes=4 threads=15 maximum-requests=5000 display-name=%{GROUP}The
maximum-requestsparameter forces processes to restart after a set number of requests, preventing memory leaks or stale connections from building up over time.Fix Database Connection Issues
Stale or exhausted database connections often trigger this error indirectly. Try:- Adjusting Django’s
CONN_MAX_AGEinsettings.py(set it to 300, or 5 minutes, to avoid holding connections open indefinitely). - Adding a connection pooler like
django-db-connection-poolif you’re using PostgreSQL/MySQL—this ensures you always have valid connections available for requests.
- Adjusting Django’s
Enable Detailed Logging to Diagnose Root Cause
To pinpoint exactly what’s happening, crank up logging temporarily:- In your Apache virtual host config, set
LogLevel info(ordebugfor more detail, though be cautious with production logs size) to capture connection lifecycle events. - In Django’s
settings.py, update yourLOGGINGsetup to logdjango.requestanddjango.db.backendsat theDEBUGlevel—this will show if requests are failing mid-processing due to database timeouts or other errors.
- In your Apache virtual host config, set
Check for External Network Timeouts
If your server is behind a load balancer, firewall, or CDN, their timeout settings might be cutting off long-running Django requests. For example, AWS ELB defaults to a 60-second timeout—if your views take longer than that, you’ll get this disconnect error. Either adjust the external service’s timeout or optimize your Django views (use async tasks, add caching, or optimize database queries).
Personal Experience
I dealt with this exact issue on a high-traffic e-commerce Django app a while back. The root cause was a combination of a too-short KeepAliveTimeout (5 seconds) and unoptimized product listing queries that took 7-10 seconds to run during peak hours. Bumping the timeout to 20 seconds and adding database indexes to speed up queries completely resolved the problem—no more random disconnects, even during traffic spikes.
内容的提问来源于stack exchange,提问作者Daniel Sánchez Campillos

