Python 如何让 Django 使用 Gunicorn 提供静态文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12800862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:54:22  来源:igfitidea点击:

How to make Django serve static files with Gunicorn?

pythondjangogunicorn

提问by alix

I want to run my django project under gunicorn on localhost. I installed and integrated gunicorn. When I run:

我想在本地主机上的 gunicorn 下运行我的 django 项目。我安装并集成了 gunicorn。当我运行时:

python manage.py run_gunicorn

It works but there are no any static files (css and js)

它可以工作,但没有任何静态文件(css 和 js)

I disabled debug and template_debug in settings.py (made them false), but it is still same. Am I missing something?

我在 settings.py 中禁用了 debug 和 template_debug(使它们为 false),但它仍然相同。我错过了什么吗?

I call statics like:

我将静态称为:

{{ STATIC_URL }}css/etc....

采纳答案by rantanplan

When in developmentmode and when you are using some other server for local developmentadd this to your url.py

开发模式下以及使用其他服务器进行本地开发时,将此添加到您的 url.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()

More info here

更多信息在这里

When in productionyou never, ever put gunicorn in front. Instead you use a server like nginx which dispatches requests to a pool of gunicorn workers and also serves the static files.

生产中,您永远不要将 gunicorn 放在前面。相反,您使用像 nginx 这样的服务器,它将请求分派到一组 gunicorn 工作人员并提供静态文件。

See here

这里

回答by Ngure Nyaga

The gunicorn should be used to serve the python "application" itself, while the static files are served by a static file server ( such as Nginx ).

gunicorn 应该用于为python“应用程序”本身提供服务,而静态文件由静态文件服务器(例如 Nginx )提供服务。

There is a good guide here: http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn

这里有一个很好的指南:http: //honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn

This is an excerpt from one of my configurations:

这是我的配置之一的摘录:

upstream app_server_djangoapp {
    server localhost:8000 fail_timeout=0;
}

server {
    listen < server port goes here >;
    server_name < server name goes here >;

    access_log  /var/log/nginx/guni-access.log;
    error_log  /var/log/nginx/guni-error.log info;

    keepalive_timeout 5;

    root < application root directory goes here >;

    location /static {    
        autoindex on;    
        alias < static folder directory goes here >;    
    }

    location /media {
       autoindex on;
       alias < user uploaded media file directory goes here >;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
}

Some notes:

一些注意事项:

  • The static root, media root, static files path prefix and media file path prefix are set up in your settings.py
  • Once you have nginx set up to serve from the static content directory, you need to run "python manage.py collectstatic" in your project root so that the static files in the various apps can be copied to the static folder
  • 静态根、媒体根、静态文件路径前缀和媒体文件路径前缀在您的 settings.py 中设置
  • 将 nginx 设置为从静态内容目录提供服务后,您需要在项目根目录中运行“python manage.py collectstatic”,以便将各种应用程序中的静态文件复制到静态文件夹中

In closing: while it is possible to serve static files from gunicorn ( by enabling a debug-only static file serving view ), that is considered bad practice in production.

最后:虽然可以从 gunicorn 提供静态文件(通过启用仅用于调试的静态文件服务视图),但这在生产中被认为是不好的做法。

回答by WhyNotHugo

I've used this for my development environment (which uses gunicorn):

我已经将它用于我的开发环境(使用 gunicorn):

from django.conf import settings
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.wsgi import get_wsgi_application


if settings.DEBUG:
    application = StaticFilesHandler(get_wsgi_application())
else:
    application = get_wsgi_application()

And then run gunicorn myapp.wsgi. This works similarto @rantanplan's answer, however, it does not run any middleware when running static files.

然后运行gunicorn myapp.wsgi。此作品相似@ rantanplan的答案,但是,在运行静态文件,当它不运行任何中间件。

回答by frost-nzcr4

Since Django 1.3 there is django/conf/urls/static.py that handle static files in the DEBUG mode:

从 Django 1.3 开始,有 django/conf/urls/static.py 在调试模式下处理静态文件:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Read more https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development

阅读更多https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development