{"title":"如何部署静态文件","version":"5.1","locale":"zh-hans","docname":"howto/static-files/deployment","url":"/zh-hans/5.1/howto/static-files/deployment/","canonical":"https://djangodocs.dev/zh-hans/5.1/howto/static-files/deployment/","summary":"See also 想要 django.contrib.staticfiles 的使用指南，请参考 如何管理静态文件（如图片、JavaScript、CSS） 。 在生产环境提供静态文件服务 Link to this heading # 将静态文件放入生产环境的基本步骤包括两个步骤：静态文件更改时运行 collectstatic…","html":"<h1>如何部署静态文件<a class=\"heading-anchor\" href=\"#how-to-deploy-static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>想要 <a class=\"reference internal\" href=\"/zh-hans/5.1/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\">django.contrib.staticfiles</code></a> 的使用指南，请参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/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/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> 命令，然后安排已收集的静态文件目录 (<a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a>) 移动到静态文件服务器并提供服务。根据 <code class=\"docutils literal notranslate\">staticfiles</code> <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\">STORAGES</code></a> 别名，可能需要手动移动文件或 <code class=\"docutils literal notranslate\">Storage</code> 类的 <a class=\"reference internal\" href=\"/zh-hans/5.1/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\">post_process</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/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a>，将所有的静态文件拷贝至 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a>。</p></li>\n<li><p>配置 Web 服务器，使其在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> 下为 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a> 目录下的文件提供静态文件服务。例如， <a class=\"reference internal\" href=\"/zh-hans/5.1/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 的服务器——来提供静态文件。这台服务器通常运行不同类型的网络服务器——速度更快，但功能不全。一些常见的选择是：</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/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a>。</p></li>\n<li><p>将本地 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</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（内容交付网络）中提供静态文件。这可以让你忽略提供静态文件的问题，而且通常可以使网页的加载速度更快（特别是在使用 CDN 时）。</p>\n<p>使用这些服务时，基本的工作流程与上面类似，除了要将静态文件传输给存储服务商或 CDN，而不是用 <code class=\"docutils literal notranslate\">rsync</code> 将静态文件传输给服务器。</p>\n<p>有许多方法可以做到这一点，但如果提供商有一个 API，你可以使用一个 <a class=\"reference internal\" href=\"/zh-hans/5.1/howto/custom-file-storage/\"><span class=\"doc\">自定义文件存储后端</span></a> 来将 CDN 集成到你的 Django 项目中。如果你编写了或正在使用第三方的自定义存储后端，可以通过在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\">STORAGES</code></a> 中设置 <code class=\"docutils literal notranslate\">staticfiles</code> 来告诉 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> 使用它。</p>\n<p>例如，若你已在 <code class=\"docutils literal notranslate\">myproject.storage.S3Storage</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>STORAGES <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;staticfiles&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;myproject.storage.S3Storage&quot;</span><span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>一旦完成了这些步骤，你只需运行 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a>，你的静态文件将通过存储包上传到 S3。如果以后需要切换到不同的存储提供商，你可能只需更改 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\">STORAGES</code></a> 设置中的 <code class=\"docutils literal notranslate\">staticfiles</code>。</p>\n<p>关于如何编写这些后端的细节，参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/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/5.1/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\">django.contrib.staticfiles</code></a> 包含的其它零碎的完整细节，参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/\"><span class=\"doc\">staticfiles 参考</span></a>。</p>\n</section>","rootId":"how-to-deploy-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/5.1/howto/"}],"prev":{"docname":"howto/deployment/checklist","title":"部署清单","url":"/zh-hans/5.1/howto/deployment/checklist/"},"next":{"docname":"howto/auth-remote-user","title":"使用 REMOTE_USER 进行身份验证","url":"/zh-hans/5.1/howto/auth-remote-user/"},"formats":{"html":"/zh-hans/5.1/howto/static-files/deployment/","markdown":"/zh-hans/5.1/howto/static-files/deployment.md","json":"/zh-hans/5.1/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/zh-hans/5.1/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","it","pt-br","ko","es","el","pl"]}