{"title":"部署静态文件","version":"2.2","locale":"zh-hans","docname":"howto/static-files/deployment","url":"/zh-hans/2.2/howto/static-files/deployment/","canonical":"https://djangodocs.dev/zh-hans/2.2/howto/static-files/deployment/","summary":"See also 想要 django.contrib.staticfiles 的使用指南，请参考 管理静态文件（比如图片、JavaScript、CSS） 。 在生产环境提供静态文件服务 Link to this heading # 在生产环境部署静态文件的要点很简单：在静态文件变化时，运行 collectstatic…","html":"<h1>部署静态文件<a class=\"heading-anchor\" href=\"#deploying-static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>想要 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 的使用指南，请参考 <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/static-files/\"><span class=\"doc\">管理静态文件（比如图片、JavaScript、CSS）</span></a>。</p>\n</aside>\n<section id=\"serving-static-files-in-production\">\n<span id=\"staticfiles-production\"></span><h2>在生产环境提供静态文件服务<a class=\"heading-anchor\" href=\"#serving-static-files-in-production\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在生产环境部署静态文件的要点很简单：在静态文件变化时，运行 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a>，然后将已收集的静态文件目录(<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>)移至静态文件服务器并提供服务。 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a> 可能会要求手动将文件移动到新位置，不过 <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage</span></code> 类的 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.StaticFilesStorage.post_process\" title=\"django.contrib.staticfiles.storage.StaticFilesStorage.post_process\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">post_process</span></code></a> 方法可能会处理它。</p>\n<p>当然，像所有的部署任务一样，细节决定成败。每个生成环境的配置可能都有点不同个，所以，需要调整配置以满足你的需求。以下是常见模式，可能对你有所帮助。</p>\n<section id=\"serving-the-site-and-your-static-files-from-the-same-server\">\n<h3>在同一服务器提供站点和静态文件服务<a class=\"heading-anchor\" href=\"#serving-the-site-and-your-static-files-from-the-same-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>如果你想在早已提供站点服务器服务器上同时提供静态文件服务，操作步骤类似这样：</p>\n<ul class=\"simple\">\n<li><p>将代码推送至部署服务器。</p></li>\n<li><p>在服务器上运行 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a>，将所有的静态文件拷贝至 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>。</p></li>\n<li><p>配置 Web 服务器，使其在 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> 下为 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 目录下的文件提供静态文件服务。例如， <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/deployment/wsgi/modwsgi/#serving-files\"><span class=\"std std-ref\">这里是如何以 Apache 配合 mod_wsgi 开始</span></a>。</p></li>\n</ul>\n<p>你可能期望将该流程自动化，特别是在你有好几个 web 服务器的时候。</p>\n</section>\n<section id=\"serving-static-files-from-a-dedicated-server\">\n<h3>专用服务器提供静态文件服务<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-dedicated-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>大多数大型 Django 站点使用一个独立的 Web 服务器——即，该服务器并未运行 Django，值提供静态文件服务。这种服务器一般运行一种不同的 Web 服务器——更快，更简单。常见选项如下：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://nginx.org/en/\">Nginx</a></p></li>\n<li><p>一个 <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> 的朴素版本</p></li>\n</ul>\n<p>如何配置这些服务器超出了本文范围；查阅这些服务器各自的文档获取介绍。</p>\n<p>由于静态文件服务器并不运行 Django，你需要将部署策略改成这样：</p>\n<ul class=\"simple\">\n<li><p>当静态文件改变时，本地运行 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a>。</p></li>\n<li><p>将本地 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 推送到静态文件服务器提供服务的目录。 <a class=\"reference external\" href=\"https://rsync.samba.org/\">rsync</a> 是一个常见选项，因为这种配置只会传输文件修改部分的数据流。</p></li>\n</ul>\n</section>\n<section id=\"serving-static-files-from-a-cloud-service-or-cdn\">\n<span id=\"staticfiles-from-cdn\"></span><h3>从云服务或 CDN 提供静态文件服务<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-cloud-service-or-cdn\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>另一种常见的策略是从类似亚马逊 S3 的云存储服务商或 CDN (content delivery network) 提供静态文件服务。这能让你忽略提供静态文件服务可能出现的问题，提供 Web 页面加载速度（尤其是在用 CDN 的时候）。</p>\n<p>使用这些服务时，基本的工作流程与上面类似，除了要将静态文件传输给存储服务商或 CDN，而不是用 <code class=\"docutils literal notranslate\"><span class=\"pre\">rsync</span></code> 将静态文件传输给服务器。</p>\n<p>有很多中方法可以实现目的，但是若服务商提供一个 API <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-file-storage/\"><span class=\"doc\">自定义文件存储后端</span></a>，这将会使流程难以置信的简单。若你已编写或正在使用第三方的自定义存储后端，你可以通过将 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a> 指向该存储引擎告诉 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> 启用它。</p>\n<p>例如，若你已在 <code class=\"docutils literal notranslate\"><span class=\"pre\">myproject.storage.S3Storage</span></code> 中写了一个 S3 存储后端，可以这么用:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">STATICFILES_STORAGE</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;myproject.storage.S3Storage&#39;</span>\n</code></pre></div>\n<p>只要完成了上述流程，你就只需运行 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a>，静态文件将通过存储包推送给 S3。如果稍后需要切换至另一个存储服务商，只需简单地修改 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a> 配置。</p>\n<p>关于如何编写这些后端的细节，参考 <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-file-storage/\"><span class=\"doc\">编写一个自定义存储系统</span></a>。有很多可用的第三方应用提供了针对常见文件存储 API 的存储后端。 <a class=\"reference external\" href=\"https://djangopackages.org/grids/g/storage-backends/\">djangopackages.org 入门</a> 是个不错的起点。</p>\n</section>\n</section>\n<section id=\"learn-more\">\n<h2>了解更多<a class=\"heading-anchor\" href=\"#learn-more\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>想要了解所有配置项，命令，模板标签和 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 包含的其它零碎的完整细节，参考 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/staticfiles/\"><span class=\"doc\">staticfiles 参考</span></a>。</p>\n</section>","rootId":"deploying-static-files","toc":[{"title":"在生产环境提供静态文件服务","anchor":"serving-static-files-in-production","children":[{"title":"在同一服务器提供站点和静态文件服务","anchor":"serving-the-site-and-your-static-files-from-the-same-server","children":[]},{"title":"专用服务器提供静态文件服务","anchor":"serving-static-files-from-a-dedicated-server","children":[]},{"title":"从云服务或 CDN 提供静态文件服务","anchor":"serving-static-files-from-a-cloud-service-or-cdn","children":[]}]},{"title":"了解更多","anchor":"learn-more","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/2.2/howto/"},{"docname":"howto/deployment/index","title":"部署 Django","url":"/zh-hans/2.2/howto/deployment/"}],"prev":{"docname":"howto/deployment/wsgi/apache-auth","title":"Apache 利用 Django 的用户数据库进行验证","url":"/zh-hans/2.2/howto/deployment/wsgi/apache-auth/"},"next":{"docname":"howto/error-reporting","title":"发送错误","url":"/zh-hans/2.2/howto/error-reporting/"},"formats":{"html":"/zh-hans/2.2/howto/static-files/deployment/","markdown":"/zh-hans/2.2/howto/static-files/deployment.md","json":"/zh-hans/2.2/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/2.2.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/zh-hans/2.2/howto/static-files/deployment/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}