---
title: "部署静态文件"
version: 3.0
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/3.0/howto/static-files/deployment/
canonical: https://djangodocs.dev/zh-hans/3.0/howto/static-files/deployment/
---
# 部署静态文件

> **See also**
>
> 想要 [`django.contrib.staticfiles`](/zh-hans/3.0/ref/contrib/staticfiles/#module-django.contrib.staticfiles) 的使用指南，请参考 [管理静态文件（比如图片、JavaScript、CSS）](/zh-hans/3.0/howto/static-files/)。

## 在生产环境提供静态文件服务

The basic outline of putting static files into production consists of two
steps: run the [`collectstatic`](/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic) command when static files change, then
arrange for the collected static files directory ([`STATIC_ROOT`](/zh-hans/3.0/ref/settings/#std-setting-STATIC_ROOT)) to be
moved to the static file server and served. Depending on
[`STATICFILES_STORAGE`](/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE), files may need to be moved to a new location
manually or the [`post_process`](/zh-hans/3.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.StaticFilesStorage.post_process) method of
the `Storage` class might take care of that.

当然，像所有的部署任务一样，细节决定成败。每个生成环境的配置可能都有点不同个，所以，需要调整配置以满足你的需求。以下是常见模式，可能对你有所帮助。

### 在同一服务器提供站点和静态文件服务

如果你想在早已提供站点服务器服务器上同时提供静态文件服务，操作步骤类似这样：

- 将代码推送至部署服务器。
- 在服务器上运行 [`collectstatic`](/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic)，将所有的静态文件拷贝至 [`STATIC_ROOT`](/zh-hans/3.0/ref/settings/#std-setting-STATIC_ROOT)。
- 配置 Web 服务器，使其在 [`STATIC_URL`](/zh-hans/3.0/ref/settings/#std-setting-STATIC_URL) 下为 [`STATIC_ROOT`](/zh-hans/3.0/ref/settings/#std-setting-STATIC_ROOT) 目录下的文件提供静态文件服务。例如， [这里是如何以 Apache 配合 mod\_wsgi 开始](/zh-hans/3.0/howto/deployment/wsgi/modwsgi/#serving-files)。

你可能期望将该流程自动化，特别是在你有好几个 web 服务器的时候。

### 专用服务器提供静态文件服务

大多数大型 Django 站点使用一个独立的 Web 服务器——即，该服务器并未运行 Django，值提供静态文件服务。这种服务器一般运行一种不同的 Web 服务器——更快，更简单。常见选项如下：

- [Nginx](https://nginx.org/en/)
- 一个 [Apache](https://httpd.apache.org/) 的朴素版本

如何配置这些服务器超出了本文范围；查阅这些服务器各自的文档获取介绍。

由于静态文件服务器并不运行 Django，你需要将部署策略改成这样：

- 当静态文件改变时，本地运行 [`collectstatic`](/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic)。
- 将本地 [`STATIC_ROOT`](/zh-hans/3.0/ref/settings/#std-setting-STATIC_ROOT) 推送到静态文件服务器提供服务的目录。 [rsync](https://rsync.samba.org/) 是一个常见选项，因为这种配置只会传输文件修改部分的数据流。

### 从云服务或 CDN 提供静态文件服务

另一种常见的策略是从类似亚马逊 S3 的云存储服务商或 CDN (content delivery network) 提供静态文件服务。这能让你忽略提供静态文件服务可能出现的问题，提供 Web 页面加载速度（尤其是在用 CDN 的时候）。

使用这些服务时，基本的工作流程与上面类似，除了要将静态文件传输给存储服务商或 CDN，而不是用 `rsync` 将静态文件传输给服务器。

There's any number of ways you might do this, but if the provider has an API,
you can use a [custom file storage backend](/zh-hans/3.0/howto/custom-file-storage/)
to integrate the CDN with your Django project. If you've written or are using a
3rd party custom storage backend, you can tell [`collectstatic`](/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic) to use
it by setting [`STATICFILES_STORAGE`](/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE) to the storage engine.

例如，若你已在 `myproject.storage.S3Storage` 中写了一个 S3 存储后端，可以这么用:

```
STATICFILES_STORAGE = 'myproject.storage.S3Storage'
```

Once that's done, all you have to do is run [`collectstatic`](/zh-hans/3.0/ref/contrib/staticfiles/#django-admin-collectstatic) and your
static files would be pushed through your storage package up to S3. If you
later needed to switch to a different storage provider, you may only have to
change your [`STATICFILES_STORAGE`](/zh-hans/3.0/ref/settings/#std-setting-STATICFILES_STORAGE) setting.

关于如何编写这些后端的细节，参考 [编写一个自定义存储系统](/zh-hans/3.0/howto/custom-file-storage/)。有很多可用的第三方应用提供了针对常见文件存储 API 的存储后端。 [djangopackages.org 入门](https://djangopackages.org/grids/g/storage-backends/) 是个不错的起点。

## 了解更多

想要了解所有配置项，命令，模板标签和 [`django.contrib.staticfiles`](/zh-hans/3.0/ref/contrib/staticfiles/#module-django.contrib.staticfiles) 包含的其它零碎的完整细节，参考 [staticfiles 参考](/zh-hans/3.0/ref/contrib/staticfiles/)。
