使用 Django 在 Apache 上提供静态文件(404 错误)
Serving static files on Apache with Django (404 error)
我一直在尝试在 CentOS 服务器 (6.7) 上使用 gunicorn 和 django (1.6.11) 将 Apache (2.2.15) 设置为代理,但我遇到了有关静态的问题文件。 (我是 django 的新手)
我已经看过 Django 提供的很棒的文档,以及其他一些 Whosebug 的帖子,但无济于事。
我可以检查的内容:
- 问题似乎不是来自 Django 或模板,因为 运行 具有 'DEBUG = False' 和选项“--insecure”的开发服务器工作正常。
- 这可能是我在虚拟主机中添加的别名和目录块的问题,但我不明白为什么它不起作用。
- Gunicorn 不应该在里面说任何话,因为它不提供静态文件。
我的配置是这样的:
1) 由于 'collectstatic' 命令,我将项目的静态文件放入了“/var/www/myproject/static”。
2) 然后,我创建了虚拟主机(我认为问题所在):
(重定向到 HTTPS 的第一个)
<virtualhost *:80>
ServerAdmin user@domain.com
ServerName localhost
ServerSignature Off
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/ [NC,R,L]
</virtualhost>
(实际工作的第二个)
<virtualhost *:443>
ServerAdmin user@domain.com
ServerName localhost
ServerSignature Off
DocumentRoot /var/www/myproject
Alias /static/ "/var/www/myproject/static/"
<Directory "/var/www/myproject/static">
Order Allow,Deny
Allow from All
</Directory>
ProxyPass / http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
ProxyPassReverse / http://127.0.0.1:8000/
ProxyPreserveHost On
ProxySet http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
... here goes the SSL and Log configuration ...
</virtualhost>
服务重启后,我的静态文件没有加载,我在获取它们时遇到 404 错误。 'httpd -S' 没有抛出任何错误,网络界面的其余功能运行良好。
我也尝试过不以“/”结尾的“/static/”别名,因为这对其他一些人来说似乎是个问题,我尝试将文件直接移动到 /var/www/myproject 下并访问它们没有 DocumentRoot 的别名...
如果你想看看 django settings.py(不知道它是否相关,但一些 django 大师也可能会发现那里有问题):
STATIC_ROOT = '/var/www/myproject/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static',),)
以及模板:
{% load staticfiles %}
....
<link href="{% static 'bower_components/bootstrap/dist/css/boostrap.min.css %}" rel="stylesheet">
Urls.py 项目:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^$', lambda x: redirect('/Collecte/')),
url(r'^Collecte/', include('Collecte.urls', namespace="Collecte")),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
以及应用程序的 urls.py(名为 'Collecte'):
from django.conf.urls import patterns, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from Collecte import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^Collectes/Execution/ù', views.CollExecution, name='CollExecution'),
... quite a lot of them ...
)
# Commented this line from a suggestion, was present at start
#urlpatterns += staticfiles_urlpatterns
如果您觉得缺少与问题相关的任何文件,请提出要求,我会尽力:)。
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
是一个辅助函数,仅在
时起作用
debug = True
例如,只需再次设置 debug = True
,重新启动服务器并检查
好的,
我在 another Whosebug thread 中找到了问题的答案,它具有类似的配置(但使用的是 flask 而不是 django)。
正如 Ajay Gupta 所说,我认为我在项目的 urls.py 文件中遇到了问题,因为我必须添加 'static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)' (从使用 django 提供的静态文件应用程序切换到其他方式).
另一个问题出在 apache 虚拟主机配置中,因为我希望我的静态文件由 apache 而不是 gunicorn 或 django 提供服务。我将所有内容重定向到 gunicorn 服务器,甚至是对静态文件的请求。所以我必须添加 'ProxyPass /static/ !' 以便 apache 提供文件。
我不知道这样做是否正确,但它对我有用。
我一直在尝试在 CentOS 服务器 (6.7) 上使用 gunicorn 和 django (1.6.11) 将 Apache (2.2.15) 设置为代理,但我遇到了有关静态的问题文件。 (我是 django 的新手)
我已经看过 Django 提供的很棒的文档,以及其他一些 Whosebug 的帖子,但无济于事。
我可以检查的内容:
- 问题似乎不是来自 Django 或模板,因为 运行 具有 'DEBUG = False' 和选项“--insecure”的开发服务器工作正常。
- 这可能是我在虚拟主机中添加的别名和目录块的问题,但我不明白为什么它不起作用。
- Gunicorn 不应该在里面说任何话,因为它不提供静态文件。
我的配置是这样的:
1) 由于 'collectstatic' 命令,我将项目的静态文件放入了“/var/www/myproject/static”。
2) 然后,我创建了虚拟主机(我认为问题所在):
(重定向到 HTTPS 的第一个)
<virtualhost *:80>
ServerAdmin user@domain.com
ServerName localhost
ServerSignature Off
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/ [NC,R,L]
</virtualhost>
(实际工作的第二个)
<virtualhost *:443>
ServerAdmin user@domain.com
ServerName localhost
ServerSignature Off
DocumentRoot /var/www/myproject
Alias /static/ "/var/www/myproject/static/"
<Directory "/var/www/myproject/static">
Order Allow,Deny
Allow from All
</Directory>
ProxyPass / http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
ProxyPassReverse / http://127.0.0.1:8000/
ProxyPreserveHost On
ProxySet http://127.0.0.1:8000/ connectiontimeout=150 timeout=300
... here goes the SSL and Log configuration ...
</virtualhost>
服务重启后,我的静态文件没有加载,我在获取它们时遇到 404 错误。 'httpd -S' 没有抛出任何错误,网络界面的其余功能运行良好。
我也尝试过不以“/”结尾的“/static/”别名,因为这对其他一些人来说似乎是个问题,我尝试将文件直接移动到 /var/www/myproject 下并访问它们没有 DocumentRoot 的别名...
如果你想看看 django settings.py(不知道它是否相关,但一些 django 大师也可能会发现那里有问题):
STATIC_ROOT = '/var/www/myproject/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static',),)
以及模板:
{% load staticfiles %}
....
<link href="{% static 'bower_components/bootstrap/dist/css/boostrap.min.css %}" rel="stylesheet">
Urls.py 项目:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^$', lambda x: redirect('/Collecte/')),
url(r'^Collecte/', include('Collecte.urls', namespace="Collecte")),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
以及应用程序的 urls.py(名为 'Collecte'):
from django.conf.urls import patterns, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from Collecte import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^Collectes/Execution/ù', views.CollExecution, name='CollExecution'),
... quite a lot of them ...
)
# Commented this line from a suggestion, was present at start
#urlpatterns += staticfiles_urlpatterns
如果您觉得缺少与问题相关的任何文件,请提出要求,我会尽力:)。
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
是一个辅助函数,仅在
debug = True
例如,只需再次设置 debug = True
,重新启动服务器并检查
好的,
我在 another Whosebug thread 中找到了问题的答案,它具有类似的配置(但使用的是 flask 而不是 django)。
正如 Ajay Gupta 所说,我认为我在项目的 urls.py 文件中遇到了问题,因为我必须添加 'static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)' (从使用 django 提供的静态文件应用程序切换到其他方式).
另一个问题出在 apache 虚拟主机配置中,因为我希望我的静态文件由 apache 而不是 gunicorn 或 django 提供服务。我将所有内容重定向到 gunicorn 服务器,甚至是对静态文件的请求。所以我必须添加 'ProxyPass /static/ !' 以便 apache 提供文件。
我不知道这样做是否正确,但它对我有用。