服务器配置
🌐 Server configuration
Page summary:
/config/server管理主机、端口、URL、代理、定时任务等;更改需要重新构建管理面板。
/config/server.js 文件用于定义 Strapi 应用的服务器配置。
🌐 The /config/server.js file is used to define the server configuration for a Strapi application.
对 server.js 文件的更改需要重建管理面板。保存修改后的文件后,在终端中运行 yarn build 或 npm run build 以实现更改。
🌐 Changes to the server.js file require rebuilding the admin panel. After saving the modified file run either yarn build or npm run build in the terminal to implement the changes.
可用选项
🌐 Available options
/config/server.js 文件可以包含以下参数:
🌐 The /config/server.js file can include the following parameters:
| 参数 | 描述 | 类型 | 默认 |
|---|---|---|---|
host❗️ 必填 | 主机名 | 字符串 | localhost |
port❗️ 必填 | 服务器应运行的端口。 | 整数 | 1337 |
app.keys❗️ 必填 | 声明会话密钥(基于 Koa session),用于 session 中间件的用户与权限插件以及文档插件。 | 字符串数组 | undefined |
| “socket” | 用插座听。当提供这个选项时,主机和端口只是装饰性,使用时也请使用“url”来生成正确的URL。该选项适用于运行服务器时不暴露端口,同时在同一台机器上使用代理服务器(例如 Heroku nginx buildpack | 字符串 | 整数 | '/tmp/nginx.socket' |
emitErrors | (在 Strapi 5 中已移除) 之前可以将错误发送到 koa,这样你就可以附加自定义逻辑或使用错误报告服务。该选项现在不再读取,也没有任何效果,所以你可以放心地从 /config/server 中删除它。有关支持的方法,请参见错误处理。 | boolean | |
url | 服务器的公共 URL。对于许多不同功能都是必需的(例如:重置密码、第三方登录提供商等),当 Strapi 运行在像 Apache 或 Nginx 这样的代理后面时,绝对 URL 也是从这个地址构建的,例如:https://mywebsite.com/api。信任代理添加的头信息是另一个设置,proxy.koa。URL 可以是相对的,如果是相对的,会以 http://${host}:${port} 作为基础 URL 来使用。不过推荐使用绝对 URL。 | string | '' |
proxy | 代理配置 | 对象 | |
proxy.global | 定义所有外部请求的代理代理。如果 Strapi 项目位于前置代理之后,则使用此选项。 | 字符串 | |
| “proxy.fetch” | 所有在“strapi.fetch”内请求的代理(用于许可检查、遥测和webhook) | 字符串 | ProxyAgent.Options | |
proxy.http | 所有(非 fetch)HTTP 请求的代理 | 字符串 | |
proxy.https | 所有(非 fetch)https 请求的代理 | 字符串 | |
proxy.koa | 设置 koa 变量 app.proxy。当 true 时,代理头字段将被信任。 | 布尔值 | false |
proxy.ipHeader | 5.52.0+ 在识别客户端真实 IP 地址时要信任的代理 IP 头名称。当位于反向代理(例如 Nginx、Apache、负载均衡器)后时设置。出于安全考虑,总是与 proxy.maxIpsCount 配合使用以防止头伪造。示例:'X-Forwarded-For'、'CF-Connecting-IP'。 | 字符串 | 'X-Forwarded-For' |
proxy.maxIpsCount | 5.52.0+ 从代理 IP 头部读取的最大 IP 地址数量。在使用反向代理时必须设置,以防止 IP 欺骗攻击。单个代理设置为 1,代理链设置为 2 等。设置为 0(Koa 默认)表示无限,这在代理后面是不安全的。 | 整数 | 0 |
cron | Cron 配置(由 `node-schedule` 提供支持) | 对象 | |
cron.enabled | 启用或禁用 CRON 作业 以在特定日期安排作业。 | 布尔值 | false |
cron.tasks | 声明将在特定日期运行的 CRON 作业。 | 对象 | |
dirs | Strapi 使用的不同目录的路径配置。 | 对象 | |
dirs.public | 自定义公共文件夹的路径。 | 字符串 | ./public |
http | Strapi 使用的 HTTP 服务器配置 | 对象 | |
http.serverOptions | 传递给 http createServer 的选项 | http.serverOptions | |
transfer.remote.enabled | 切换使用 传输功能 的能力 | 布尔值 | true |
transfer.remote.assetIdleTimeoutMs | 在使用 strapi transfer --from 从远程实例拉取资源流时,如果超过此毫秒数没有收到数据,则该资源流会被认为已停止。传输大文件或在网络慢的情况下可以增加这个值。 | 整数 | 300000(5 分钟) |
logger.startup.enabled | 切换终端启动消息 | 布尔值 | true |
logger.updates.enabled | 切换终端中关于更新 strapi 的通知消息 | 布尔值 | true |
openapi | OpenAPI 端点配置。两个端点默认都使用 access: 'disabled',且未注册。 | 对象 | |
openapi['content-api'].access | 访问模式:disabled(未注册)或 public(无认证)。 | 字符串 | disabled |
openapi['content-api'].route.path | 内容 API 端点的子路径,在 REST API 前缀 下解析。 | string | /openapi.json |
openapi['content-api'].cache.enabled | 启用基于文件的生成规范缓存。 | 布尔值 | true |
openapi['content-api'].cache.maxAgeMs | 缓存有效时间(毫秒)。 | 整数 | 60000 |
openapi['content-api'].cache.filePath | 缓存规范的文件路径。相对路径从应用根目录解析。 | 字符串 | .strapi/openapi/content-api.json |
openapi.admin.access | 访问模式:disabled(未注册)或 authenticated(需要管理员会话)。 | 字符串 | disabled |
openapi.admin.route.path | 管理端点的子路径,在 admin path 下解析。 | string | /openapi.json |
openapi.admin.cache.enabled | 启用对生成的规范进行基于文件的缓存。 | 布尔值 | true |
openapi.admin.cache.maxAgeMs | 缓存有效时间(毫秒)。 | 整数 | 60000 |
openapi.admin.cache.filePath | 缓存规范的文件路径。相对路径从应用根目录解析。 | 字符串 | .strapi/openapi/admin.json |
webhooks.defaultHeaders | 每个 webhook 请求都会发送的默认头。单个 webhook 设置的头优先。参见 Webhooks。 | 对象 | {} |
webhooks.populateRelations | (在 Strapi 5 中已移除) 这个选项不再被读取,也没有任何作用。如果你的 /config/server 文件中仍然存在这个键,你可以放心删除它。详见 已移除的 webhooks.populateRelations 服务器配置。 | 布尔值 |
没有特定于 Strapi 的保持活动配置选项,因为 Strapi 使用 Node 的默认选项来处理传入的 HTTP 请求,默认情况下保持连接活动。
🌐 There is no Strapi-specific keep alive configuration option, because Strapi uses Node's default one for incoming HTTP requests, keeping connections alive by default.
对于外发的 HTTP 调用,你可以将一个保持连接的代理传递给你的 HTTP 客户端。使用 agentkeepalive 和 Axios 的示例:
🌐 For outgoing HTTP calls, you can pass a keep-alive agent to your HTTP client.
Example with agentkeepalive and Axios:
const { HttpsAgent } = require('agentkeepalive');
const axios = require('axios');
const agent = new HttpsAgent();
axios.get('https://example.com', { httpsAgent: agent });
Strapi 提供了一个专用的健康检查路由用于运行时间探测。对 /_health 的任何请求都会返回一个空响应,并带有 204 状态和 strapi: You are so French! 响应头,这适用于只需要简单存活指示器的负载均衡器或监控工具。
🌐 Strapi exposes a dedicated health check route for uptime probes. Any request to /_health returns an empty response with a 204 status and a strapi: You are so French! response header, which is suitable for load balancers or monitoring tools that only need a simple liveness indicator.
配置
🌐 Configurations
/config/server.js最小配置在开发时需要host和port参数。完整配置可以包括更多参数。
🌐 The /config/server.js minimal configuration requires the host and port parameters for development. Additional parameters can be included for a full configuration.
环境配置(即使用 env() 助手)不需要包含所有值,只要它们存在于默认的 ./config/server.js 中即可。
使用任何新项目创建的默认配置至少应包括以下内容:
- Minimal configuration
- Full configuration
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
});
import type { Core } from '@strapi/strapi';
export default ({ env }: Core.Config.Shared.ConfigParams): Core.Config.Server => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
});
以下是一个完整配置文件的示例。并非所有这些键都是必需的(请参见 可用选项)。
🌐 The following is an example of a full configuration file. Not all of these keys are required (see available options).
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
socket: '/tmp/nginx.socket', // only use if absolutely required
url: env('PUBLIC_URL', 'https://api.example.com'),
proxy: {
koa: env.bool('IS_PROXIED', true),
ipHeader: env('PROXY_IP_HEADER', 'X-Forwarded-For'),
maxIpsCount: env.int('MAX_IPS_COUNT', 1),
},
cron: {
enabled: env.bool('CRON_ENABLED', false),
},
transfer: {
remote: {
enabled: false,
},
},
logger: {
updates: {
enabled: false,
},
startup: {
enabled: false,
},
},
openapi: {
'content-api': {
access: 'public',
},
},
});
import type { Core } from '@strapi/strapi';
export default ({ env }: Core.Config.Shared.ConfigParams): Core.Config.Server => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
socket: '/tmp/nginx.socket', // only use if absolutely required
url: env('PUBLIC_URL', 'https://api.example.com'),
proxy: {
koa: env.bool('IS_PROXIED', true),
ipHeader: env('PROXY_IP_HEADER', 'X-Forwarded-For'),
maxIpsCount: env.int('MAX_IPS_COUNT', 1),
},
cron: {
enabled: env.bool('CRON_ENABLED', false),
},
transfer: {
remote: {
enabled: false,
},
},
logger: {
updates: {
enabled: false,
},
startup: {
enabled: false,
},
},
openapi: {
'content-api': {
access: 'public',
},
},
});