Why not serving static files with Django in a production environment?

左心房为你撑大大i 提交于 2020-01-24 09:46:05

问题


I came across the following example for settings.py:

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and was told:

The static() helper function is suitable for development but not for production use. Never serve your static files with Django in a production environment.

Can anyone explain why and how to use it the better way?

EDIT:

Can I use static() with Apache?


回答1:


Django is not very fast or efficient for serving static files. To quote the Django docs, "This method is grossly inefficient and probably insecure, so it is unsuitable for production." It is better to use tools that are specifically designed for serving static content. There are extensive instructions for how to setup a static server in the Django documentation on deploying static files.

The basic idea is to not unnecessarily involve Django in the serving of static files. Let your production server, which from your comment it sounds like is apache, serve the static files directly. Here are instructions for editing your httpd.conf file to get apache to serve the static files https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files. The static() function in django should not be involved at all. Make sure to use the collectstatic management command in django to copy all your static files to the STATIC_ROOT so apache can find them.



来源:https://stackoverflow.com/questions/38860601/why-not-serving-static-files-with-django-in-a-production-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!