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

如何在Apache HTTPD及Django关联Apache时配置自定义错误页面

Got it, let's break down how to set up custom error pages for both plain Apache HTTPD and your Django + Apache setup—step by step, no fluff.

1. Custom Error Pages in Plain Apache HTTPD

First, create your custom error HTML files (like 404.html, 500.html) and place them somewhere accessible to your Apache server. For example, you could make an errors subdirectory in your web root: /var/www/html/errors/.

Next, open your Apache configuration file—either the main httpd.conf or your site's virtual host config (e.g., /etc/apache2/sites-available/your-site.conf on Debian/Ubuntu). Add the following lines to map error codes to your custom pages:

# Map specific error codes to your custom pages
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
ErrorDocument 400 /errors/400.html

# Optional: Add more for other common errors if needed
ErrorDocument 403 /errors/forbidden.html

⚠️ Important: The path after ErrorDocument is a URL path (relative to your web root), not a server file path. So if your web root is /var/www/html, /errors/404.html points to /var/www/html/errors/404.html.

Then, make sure Apache has permission to access these files:

sudo chown www-data:www-data /var/www/html/errors/*.html
sudo chmod 644 /var/www/html/errors/*.html

Finally, restart Apache to apply changes:

# Debian/Ubuntu
sudo systemctl restart apache2

# RHEL/CentOS
sudo systemctl restart httpd

Test it out by visiting a non-existent URL (should trigger your 404 page) or causing a server error (e.g., a broken CGI script) to see your 500 page.

2. Custom Error Pages for Django + Apache

This requires configuring both Django and Apache, since some errors might be caught by Apache before they reach Django (like static file 404s), while others are handled by Django itself.

Step 1: Configure Django

First, ensure your project is set up for production:

  • In settings.py, set DEBUG = False (Django skips custom error pages when DEBUG is enabled).
  • Set ALLOWED_HOSTS to your domain/IP: ALLOWED_HOSTS = ['your-domain.com', '127.0.0.1'].

Next, create your custom error templates. In your project's root templates directory (make sure it's listed in TEMPLATES in settings.py), add files named exactly 400.html, 404.html, 500.html. For example:

<!-- templates/404.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Page Not Found</title>
</head>
<body>
    <h1>Oops! That page doesn't exist.</h1>
    <p>Check the URL or head back to our <a href="/">homepage</a>.</p>
</body>
</html>

Step 2: Update Django URLs

To let Django handle these error codes (and ensure Apache forwards them correctly), add routes to your project's urls.py:

from django.urls import path
from django.views.defaults import page_not_found, server_error, bad_request

urlpatterns = [
    # Your existing routes go here...
    path('404/', page_not_found, kwargs={'template_name': '404.html'}),
    path('500/', server_error, kwargs={'template_name': '500.html'}),
    path('400/', bad_request, kwargs={'template_name': '400.html'}),
]

Step 3: Configure Apache

Now, tell Apache to forward error requests to Django's error routes. Open your Apache virtual host config and add:

# Forward errors to Django's custom routes
ErrorDocument 404 /404/
ErrorDocument 500 /500/
ErrorDocument 400 /400/

# Make sure static files are served correctly (so your error pages load CSS/JS)
Alias /static/ /path/to/your/django/project/static/
<Directory /path/to/your/django/project/static/>
    Require all granted
</Directory>

# Your existing WSGI configuration for Django
WSGIScriptAlias / /path/to/your/django/project/wsgi.py
<Directory /path/to/your/django/project/>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Step 4: Test and Restart

Restart Apache to apply changes, then test:

  • Visit a non-existent URL to check the 404 page.
  • Trigger a server error (e.g., add raise Exception("Test 500") to a view) to see your 500 page.
  • Send a bad request (e.g., malformed POST data) to test the 400 page.

内容的提问来源于stack exchange,提问作者Siddharth Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:02:59