Skip to main content

服务器配置

🌐 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.

Caution

server.js 文件的更改需要重建管理面板。保存修改后的文件后,在终端中运行 yarn buildnpm 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当发生错误时,启用将错误发送到 koa,以便附加自定义逻辑或使用错误报告服务。布尔值false
url服务器的公共网址。许多不同功能需要此项(例如:重置密码、第三方登录提供商等)。同时支持代理,如 Apache 或 Nginx,示例:https://mywebsite.com/api。网址可以是相对的,如果是相对的,将与 http://${host}:${port} 一起作为基础网址使用。不过,推荐使用绝对网址。字符串''
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
cronCron 配置(由 `node-schedule` 提供支持)对象
cron.enabled启用或禁用 CRON 作业 以在特定日期安排作业。布尔值false
cron.tasks声明将在特定日期运行的 CRON 作业对象
dirsStrapi 使用的不同目录的路径配置。对象
dirs.public自定义公共文件夹的路径。字符串./public
httpStrapi 使用的 HTTP 服务器配置对象
http.serverOptions传递给 http createServer 的选项http.serverOptions
transfer.remote.enabled切换使用 传输功能 的能力布尔值true
logger.startup.enabled切换终端启动消息布尔值true
logger.updates.enabled切换终端中关于更新 strapi 的通知消息布尔值true
Note

没有特定于 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 });
Tip

Strapi 提供了一个专用的健康检查路由,使正常运行时间探测变得简单。对 /_health 的任何请求都会返回一个空响应,并带有 204 状态和 strapi: You are so French! 响应头,这适用于只需要简单存活指示器的负载均衡器或监控工具。

🌐 Strapi exposes a dedicated health check route to make uptime probes straightforward. 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最小配置在开发时需要hostport参数。完整配置可以包括更多参数。

🌐 The ./config/server.js minimal configuration requires the host and port parameters for development. Additional parameters can be included for a full configuration.

Note

环境配置(即使用 env() 助手)不需要包含所有值,只要它们存在于默认的 ./config/server.js 中即可。

使用任何新项目创建的默认配置至少应包括以下内容:

./config/server.js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
});