简体   繁体   中英

Django - Serving MEDIA/uploaded files in production

I currently have this in my project urls.py, the last line is what's important.

urlpatterns = patterns('',
    url(r'^', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I've been told and I've read that this is not suitable for a production environment. Why is this the case?

Django is built to be an "application server", not a "web server".

In other words, serving static files from Django will have worse performance than using Apache or Nginx. These static content servers are (1) written in C and (2) optimized for performance.

In contrast, Django is (1) written in pure Python and (2) optimized for developing an application.

See the documentation .


That may be totally fine. I have used Django to serve static content in production, when I knew the load would not be high and I wasn't serving large files. It depends on what kind of environment "production" actually is.


FYI, A common production setup would be to use Nignx, Django, Gunicorn, and Supervisor. Nginx servers the static content from disk and reverse proxies the rest of it to Gunicorn, which runs multiple Django instances. Supervisor monitors Gunicorn and makes sure it stays running. It all depends on what level of web application you need.

It is not recommended to serve static files from the django server itself. The recommended way is to serve them in a separate server. check static files deployment , there you will find all you need.

Extending @Paul Draper's answer:

When using Nginx, make sure to list the following configuration:

location /media/ {
        root path/to/your/media;
}

我使用了django-storages 包google 部分的教程,它解决了从 Google Storage 提供文件(静态或媒体或两者)的问题,从而避免了在 Nginx 等上进行额外配置的麻烦

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM