apache 似乎在向静态文件添加尾部斜线所以 404 但 python manage.py runserver 工作正常

问题描述 投票:0回答:0

我是 django 和 apache 的新手,我正在尝试在 apache 2.4 服务器上使用 django 1.9 在 Debian 9 上提供一个项目。出于某种原因,当我尝试使用 python manage.py runserver 0.0.0.0:8000 运行 django 项目并使用服务器 ip:8000 从本地 Web 浏览器访问它时它工作正常但是当我出于某种原因尝试通过 apache 2.4 提供它时似乎 apache 正在向静态文件路径添加尾部斜线,我收到 404 错误

django 项目路径:/var/gnats django 设置路径:/var/gnats/gnats/settings.py

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'compressor',
    'taggit',
    'modelcluster',
    'wagtail.wagtailcore',
    'wagtail.wagtailadmin',
    'wagtail.wagtaildocs',
    'wagtail.wagtailsnippets',
    'wagtail.wagtailusers',
    'wagtail.wagtailimages',
    'wagtail.wagtailembeds',
    'wagtail.wagtailsearch',
    'wagtail.wagtailsites',
    'wagtail.wagtailredirects',
    'wagtail.wagtailforms',
    'djrill',
    'mail_templated',
    'password_reset',
    'pages',
    'account',
    'siteconfig',
    'api'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'wagtail.wagtailcore.middleware.SiteMiddleware',
    'wagtail.wagtailredirects.middleware.RedirectMiddleware',
)

STATIC_ROOT = '/var/gnats/pages/static'
STATIC_URL = '/static/'

模板格式

    <link href="/static/css/bootstrap.css" rel="stylesheet">
    <link href="/static/font/stylesheet.css" rel="stylesheet" >
    <link href="/static/css/font-awesome.css" rel="stylesheet" >
    <link href="/static/css/screen.css" rel="stylesheet" >

使用 python manage.py runserver 0.0.0.0:8000 时的日志(没有问题)

[30/Mar/2023 04:56:45] "GET / HTTP/1.1" 200 12779
[30/Mar/2023 04:56:45] "GET /static/css/bootstrap.css HTTP/1.1" 304 0
[30/Mar/2023 04:56:45] "GET /static/font/stylesheet.css HTTP/1.1" 304 0
....

access.log 当我尝试通过 apache 服务项目时记录。静态文件未加载,路径现在有尾部斜杠(/static/css/bootstrap.css -> /static/css/bootstrap.css/)

*.*.*.* - - [30/Mar/2023:04:16:42 +0000] "GET / HTTP/1.1" 200 3975 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
*.*.*.* - - [30/Mar/2023:04:16:44 +0000] "GET /static/css/bootstrap.css/ HTTP/1.1" 404 491 "http://*.*.*.*/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
*.*.*.* - - [30/Mar/2023:04:16:44 +0000] "GET /static/font/stylesheet.css/ HTTP/1.1" ....

apache 配置文件 /etc/apache2/sites-available/000-default.conf

<VirtualHost *:443>
.... 
    WSGIDaemonProcess gnats python-home=/var/gnats/venv python-path=/var/gnats
    WSGIProcessGroup gnats
    WSGIScriptAlias / /var/gnats/apache/django.wsgi

    <Directory /var/gnats/apache>
        Require all granted
    </Directory>

    Alias /static/ /var/gnats/pages/static/
    <Directory /var/gnats/pages/static>
        Require all granted
    </Directory>
...
</VirtualHost>

你能告诉我可能是什么问题吗?

到目前为止我已经尝试过:

  • 在 apache 配置中添加 DirectorySlash Off,
  • 在 settings.py 中更改 STATIC_URL 和 STATIC_ROOT 带/不带斜线,
  • 在 urls.py 的 urlpatterns 中添加静态(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
django apache django-staticfiles
© www.soinside.com 2019 - 2024. All rights reserved.