You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache2部署Django应用出现请求超时与内部服务器错误问题

部署Django应用到Apache2时的请求超时/响应异常问题

在Ubuntu 22.04环境下使用Apache2与libapache2-mod-wsgi-py3部署Django应用时遇到异常:网站仅能正常响应1-2次请求,之后开始无限加载,等待一段时间后出现内部服务器错误。执行sudo systemctl reload apache2重启服务器后,仅能正常响应1次请求,问题再次出现。但使用runserver 0.0.0.0:8000运行应用时一切正常。

环境信息

  • libapache2-mod-wsgi-py3版本:libapache2-mod-wsgi-py3/jammy-updates,jammy-security,now 4.9.0-1ubuntu0.1 amd64 [installed]
  • Python版本:3.10.12

Apache全局配置文件(/etc/apache2/apache.conf)

仅添加了ServerName与LimitRequestFieldSize,配置内容如下:

ServerName caspian-bavarians.com

DefaultRuntimeDir ${APACHE_RUN_DIR}

PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 5


User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf


<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^.ht">
        Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent


IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

LimitRequestFieldSize 32768

网站虚拟主机配置文件

<VirtualHost *:80>
    ServerName caspian-bavarians.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  Alias /static /home/ftpuser/caspi/static
  <Directory /home/ftpuser/caspi/static>
    Require all granted
  </Directory>

  Alias /media /home/ftpuser/caspi/media
  <Directory /home/ftpuser/caspi/media>
    Require all granted
  </Directory>
  <Directory /home/ftpuser/caspi/main>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  WSGIScriptAlias /  /home/ftpuser/caspi/main/wsgi.py
  WSGIDaemonProcess django_app python-path=/home/ftpuser/caspi/ python-home=/home/ftpuser/venv
  WSGIProcessGroup django_app
</VirtualHost>

Apache错误日志

[Fri Sep 22 05:38:30.466675 2023] [wsgi:error] [pid 6140:tid 140592497751616] (70007)The timeout specified has expired: [client 82.194.20.184:60468] mod_wsgi (pid=6140): Failed to proxy response from daemon., referer: http://www.caspian-bavarians.com/az/cars/ 
[Fri Sep 22 05:38:34.918090 2023] [wsgi:error] [pid 6139:tid 140592497751616] [client 82.194.20.184:57353] Truncated or oversized response headers received from daemon process 'django_app': /home/ftpuser/caspi/main/wsgi.py, referer: http://www.caspian-bavarians.com/az/car/mercedes-benz-e220-7/ 
[Fri Sep 22 05:38:34.918108 2023] [wsgi:error] [pid 6139:tid 140592480966208] [client 82.194.20.184:57024] Truncated or oversized response headers received from daemon process 'django_app': /home/ftpuser/caspi/main/wsgi.py, referer: http://www.caspian-bavarians.com/az/cars/ 
[Fri Sep 22 05:50:33.804640 2023] [wsgi:error] [pid 6139:tid 140592298276416] (70007)The timeout specified has expired: [client 82.194.20.184:63960] mod_wsgi (pid=6139): Failed to proxy response from daemon., referer: http://www.caspian-bavarians.com/az/cars/ 
[Fri Sep 22 05:50:37.371732 2023] [wsgi:error] [pid 6139:tid 140592506144320] [client 82.194.20.184:58025] Timeout when reading response headers from daemon process 'django_app': /home/ftpuser/caspi/main/wsgi.py, referer: http://www.caspian-bavarians.com/az/car/mercedes-benz-e220-7/ 
[Fri Sep 22 05:50:38.194056 2023] [wsgi:error] [pid 6140:tid 140592323454528] [client 82.194.20.184:63962] Timeout when reading response headers from daemon process 'django_app': /home/ftpuser/caspi/main/wsgi.py, referer: http://www.caspian-bavarians.com/az/car/mercedes-benz-e220-7/ 
[Fri Sep 22 05:50:38.742344 2023] [wsgi:error] [pid 6139:tid 140592357025344] [client 159.203.17.7:44390] Truncated or oversized response headers received from daemon process 'django_app': /home/ftpuser/caspi/main/wsgi.py

wsgi.py文件内容

"""
WSGI config for main project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

application = get_wsgi_application()

已尝试的解决操作

  • 执行apt-get update与apt-get upgrade
  • 检查libapache2-mod-wsgi-py3版本
  • 重启系统
  • 禁用并重新启用网站

内容的提问来源于stack exchange,提问作者Али Зейналов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:19:57