Skip to main content

环境配置和变量

¥Environment configuration and variables

Strapi 提供了特定的环境变量名称。在环境文件(例如 .env)中定义它们将使这些变量及其值在你的代码中可用。

¥Strapi provides specific environment variable names. Defining them in an environment file (e.g., .env) will make these variables and their values available in your code.

此外,可以创建特定的 不同环境的配置

¥Additionally, specific configurations for different environments can be created.

Strapi 的环境变量

¥Strapi's environment variables

Strapi 提供以下环境变量:

¥Strapi provides the following environment variables:

环境描述类型默认值
STRAPI_TELEMETRY_DISABLED不要将遥测使用数据发送到 StrapiBooleanfalse
STRAPI_LICENSE用于激活企业版的许可证密钥Stringundefined
NODE_ENV应用运行的环境类型。

production 启用特定行为(有关详细信息,请参阅 Node.js 文档
String'development'
BROWSER启动后在浏览器中打开管理面板Booleantrue
ENV_PATH包含环境变量的文件的路径String'./.env'
STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE

可选
应用的初始化区域设置(如果已安装 国际化 (i18n) 功能 并在内容类型上启用)(请参阅 生产环境中 i18n 的配置String'en'
STRAPI_ENFORCE_SOURCEMAPS强制打包器发出源映射,这有助于调试管理应用中的错误。booleanfalse
FAST_REFRESH(仅适用于 webpack)
使用 react-refresh 启用 "快速刷新" 以获得近乎即时的反馈,同时开发 Strapi 管理面板。
booleantrue
HOSTStrapi 服务器监听的地址String0.0.0.0
PORTStrapi 服务器使用的端口Number1337
APP_KEYS用于签署 Cookie 和其他密钥信息的逗号分隔键Stringauto-generated
API_TOKEN_SALT创建 API 令牌 时使用的盐值Stringauto-generated
ADMIN_JWT_SECRET管理面板中使用的 JWT 令牌的密钥Stringauto-generated
JWT_SECRET用户和权限 功能生成的 JWT 令牌的密钥Stringauto-generated
TRANSFER_TOKEN_SALT数据管理 功能用于转移令牌的盐值Stringauto-generated
DATABASE_CLIENT要使用的数据库客户端(例如 sqliteStringsqlite
DATABASE_FILENAMESQLite 数据库文件的位置String.tmp/data.db
提示

在环境变量名称中添加 STRAPI_ADMIN_ 前缀会将变量公开给管理前端(例如,可以通过 process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE 访问 STRAPI_ADMIN_MY_PLUGIN_VARIABLE)。

¥Prefixing an environment variable name with STRAPI_ADMIN_ exposes the variable to the admin front end (e.g., STRAPI_ADMIN_MY_PLUGIN_VARIABLE is accessible through process.env.STRAPI_ADMIN_MY_PLUGIN_VARIABLE).

.env 文件示例

¥Example .env file

Strapi CLI 在创建新项目时会生成一个 .env 和一个 .env.example 文件。文件包含自动生成的安全密钥和类似以下内容的数据库设置:

¥The Strapi CLI generates an .env and an .env.example file when creating a new project. The files contain automatically-generated security keys and database settings similar to the following:

.env.example
HOST=0.0.0.0
PORT=1337
APP_KEYS="toBeModified1,toBeModified2"
API_TOKEN_SALT=tobemodified
ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
JWT_SECRET=tobemodified
ENCRYPTION_KEY=tobemodified

环境配置

¥Environment configurations

可以使用以下命名和结构约定创建配置:./config/env/{environment}/{filename}。当你需要针对特定环境的特定静态配置并且使用环境变量不是最佳解决方案时,这非常有用。

¥Configurations can be created with the following naming and structure conventions: ./config/env/{environment}/{filename}. This is useful when you need specific static configurations for specific environments and using environment variables is not the best solution.

这些配置将合并到 ./config 文件夹中定义的基本配置中。该环境基于 NODE_ENV 环境变量,默认为 development

¥These configurations will be merged into the base configurations defined in the ./config folder. The environment is based on the NODE_ENV environment variable, which defaults to development.

当使用 NODE_ENV=production 启动 Strapi 时,它将加载 ./config/*./config/env/production/* 的配置。生产配置中定义的所有内容都将覆盖默认配置。与环境变量相结合,这种模式变得非常强大。

¥When starting Strapi with NODE_ENV=production it will load the configuration from ./config/* and ./config/env/production/*. Everything defined in the production configuration will override the default configuration. In combination with environment variables this pattern becomes really powerful.

例如,使用以下配置文件将为你提供启动服务器的各种选项:

¥For instance, using the following configuration files will give you various options to start the server:

./config/server.js

module.exports = {
host: '127.0.0.1',
};
./config/env/production/server.js

module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
});

使用这些配置文件,服务器将根据传递的环境变量在各个端口上启动:

¥With these configuration files the server will start on various ports depending on the environment variables passed:

yarn start                                   # uses host 127.0.0.1
NODE_ENV=production yarn start # uses host defined in .env. If not defined, uses 0.0.0.0
HOST=10.0.0.1 NODE_ENV=production yarn start # uses host 10.0.0.1

要深入了解如何在代码中使用环境变量,请参考以下指南:

¥To learn deeper about how to use environment variables in your code, please refer to the following guide: