{"title":"部署静态文件","version":"3.0","locale":"zh-hans","docname":"howto/static-files/deployment","url":"/zh-hans/3.0/howto/static-files/deployment/","canonical":"https://djangodocs.dev/zh-hans/3.0/howto/static-files/deployment/","summary":"The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for…","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\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>想要 <a class=\"reference internal\" href=\"/zh-hans/3.0/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/3.0/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>The basic outline of putting static files into production consists of two\nsteps: run the <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> command when static files change, then\narrange for the collected static files directory (<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a>) to be\nmoved to the static file server and served. Depending on\n<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\">STATICFILES_STORAGE</code></a>, files may need to be moved to a new location\nmanually or the <a class=\"reference internal\" href=\"/zh-hans/3.0/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> method of\nthe <code class=\"docutils literal notranslate\">Storage</code> class might take care of that.</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/3.0/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/3.0/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/3.0/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/3.0/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/3.0/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/3.0/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/3.0/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 (content delivery network) 提供静态文件服务。这能让你忽略提供静态文件服务可能出现的问题，提供 Web 页面加载速度（尤其是在用 CDN 的时候）。</p>\n<p>使用这些服务时，基本的工作流程与上面类似，除了要将静态文件传输给存储服务商或 CDN，而不是用 <code class=\"docutils literal notranslate\">rsync</code> 将静态文件传输给服务器。</p>\n<p>There's any number of ways you might do this, but if the provider has an API,\nyou can use a <a class=\"reference internal\" href=\"/zh-hans/3.0/howto/custom-file-storage/\"><span class=\"doc\">custom file storage backend</span></a>\nto integrate the CDN with your Django project. If you've written or are using a\n3rd party custom storage backend, you can tell <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> to use\nit by setting <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\">STATICFILES_STORAGE</code></a> to the storage engine.</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>STATICFILES_STORAGE <span class=\"o\">=</span> <span class=\"s1\">&#39;myproject.storage.S3Storage&#39;</span>\n</code></pre></div>\n<p>Once that's done, all you have to do is run <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\">collectstatic</code></a> and your\nstatic files would be pushed through your storage package up to S3. If you\nlater needed to switch to a different storage provider, you may only have to\nchange your <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\">STATICFILES_STORAGE</code></a> setting.</p>\n<p>关于如何编写这些后端的细节，参考 <a class=\"reference internal\" href=\"/zh-hans/3.0/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/3.0/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/3.0/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/3.0/howto/"},{"docname":"howto/deployment/index","title":"部署 Django","url":"/zh-hans/3.0/howto/deployment/"}],"prev":{"docname":"howto/deployment/asgi/uvicorn","title":"How to use Django with Uvicorn","url":"/zh-hans/3.0/howto/deployment/asgi/uvicorn/"},"next":{"docname":"howto/error-reporting","title":"发送错误","url":"/zh-hans/3.0/howto/error-reporting/"},"formats":{"html":"/zh-hans/3.0/howto/static-files/deployment/","markdown":"/zh-hans/3.0/howto/static-files/deployment.md","json":"/zh-hans/3.0/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/zh-hans/3.0/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"]}