Skip to main content

Strapi Cloud 的中间件配置

🌐 Middleware Configuration for Strapi Cloud

Page summary:

在 Strapi Cloud 上,中间件自定义必须放在 config/env/production/middlewares 中。对全局配置文件的更改在部署时会被覆盖。

Prerequisites
  • 一个本地的 Strapi 项目。
  • 一个 Strapi Cloud 项目(请参阅 入门指南)。

在 Strapi Cloud 上,NODE_ENV 始终设置为 production。平台在部署时会应用其自身的生产级中间件配置。对全局 config/middlewares 文件的任何更改都会被覆盖,并且不会生效。有关可用的中间件选项,请参见 中间件配置

🌐 On Strapi Cloud, NODE_ENV is always set to production. The platform applies its own production-level middleware configuration on deploy. Any changes to the global config/middlewares file are overwritten and will not take effect. For available middleware options, see Middlewares configuration.

要在 Strapi Cloud 上应用自定义中间件配置,请将更改放置在:

🌐 To apply custom middleware configuration on Strapi Cloud, place your changes in:

config/env/production/middlewares.js
Caution

config/env/production/middlewares 文件完全替换全局中间件数组。你的文件必须包含完整的列表:

🌐 The config/env/production/middlewares file fully replaces the global middleware array. Your file must include the complete list:

  • strapi::errors
  • strapi::security
  • strapi::cors
  • strapi::poweredBy
  • strapi::logger
  • strapi::query
  • strapi::body
  • strapi::session
  • strapi::favicon
  • strapi::public

CSP 和 CORS 的自定义可以在同一个文件中结合使用。

🌐 Both CSP and CORS customizations can be combined in the same file.

Note
  • 你可以按原样保留现有的 config/middlewares 文件,因为它不会引起冲突。在 Strapi Cloud 上,特定于生产的文件优先。
  • Strapi Cloud 上的上传大小限制是在基础设施层面强制执行的,不能通过 strapi::body 配置覆盖。
  • 有关每个计划的值以及基于内存的图片上传推荐,请参阅 Strapi Cloud 上传大小限制。有关外部存储选项,请参阅 上传提供程序配置

自定义内容安全策略 (CSP)

🌐 Custom Content Security Policy (CSP)

如果你使用外部上传提供商,请在 CSP 指令中允许其域名。否则,Strapi 管理面板将阻止来自这些来源的图片和媒体。

🌐 If you use an external upload provider, allow its domain in the CSP directives. Without this, the Strapi Admin panel will block images and media from those sources.

创建或更新 config/env/production/middlewares

🌐 Create or update config/env/production/middlewares:

config/env/production/middlewares.js
module.exports = [
'strapi::errors',
{
name: 'strapi::security',
config: {
contentSecurityPolicy: {
useDefaults: true,
directives: {
'connect-src': ["'self'", 'https:'],
'img-src': [
"'self'",
'data:',
'blob:',
'market-assets.strapi.io',
'your-custom-domain.com', // replace with your provider domain
],
'media-src': [
"'self'",
'data:',
'blob:',
'market-assets.strapi.io',
'your-custom-domain.com', // replace with your provider domain
],
upgradeInsecureRequests: null,
},
},
},
},
'strapi::cors',
'strapi::poweredBy',
'strapi::logger',
'strapi::query',
'strapi::body',
'strapi::session',
'strapi::favicon',
'strapi::public',
];
Tip

有关上传提供商及其所需域的完整列表,请参见 Strapi Market

自定义 CORS 头

🌐 Custom CORS headers

如果你的前端发送自定义请求头(例如用于授权流程),你需要在 CORS 配置中显式允许它们。在全局 config/middlewares 文件中设置是无法在 Strapi Cloud 上生效的。应将其放在 config/env/production/middlewares 中。

🌐 If your frontend sends custom request headers (e.g. for authorization flows), you need to explicitly allow them in the CORS configuration. Placing this in the global config/middlewares file will not work on Strapi Cloud. Place it in config/env/production/middlewares instead.

config/env/production/middlewares.js
module.exports = ({ env }) => [
'strapi::errors',
'strapi::security',
{
name: 'strapi::cors',
config: {
enabled: true,
origin: [env('CLIENT_URL')],
headers: [
'Content-Type',
'Authorization',
'Origin',
'Accept',
'X-Requested-With',
'your-custom-header', // add any custom headers your frontend sends
],
},
},
'strapi::poweredBy',
'strapi::logger',
'strapi::query',
'strapi::body',
'strapi::session',
'strapi::favicon',
'strapi::public',
];