-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add middleware customization guide for Strapi Cloud production environment #3148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raulbalestra
wants to merge
5
commits into
strapi:main
Choose a base branch
from
raulbalestra:cloud/add-middleware-customization-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
729b8f4
Add middleware customization guide for Strapi Cloud production enviro…
raulbalestra bb53c66
Address review feedback on middleware guide
raulbalestra 646c7fb
Apply inline review feedback from derrickmehaffy
raulbalestra 806aa5d
Fix code block titles in upload.md Security Middleware section
raulbalestra 5344a28
Apply structural simplification per reviewer feedback
raulbalestra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| --- | ||
| title: Middleware Configuration for Strapi Cloud | ||
| displayed_sidebar: cloudSidebar | ||
| description: Configure custom middlewares for your Strapi Cloud production environment. | ||
| canonicalUrl: https://docs.strapi.io/cloud/advanced/middlewares.html | ||
| tags: | ||
| - configuration | ||
| - middlewares | ||
| - CORS | ||
| - Content Security Policy | ||
| - CSP | ||
| - production | ||
| - Strapi Cloud | ||
| - Strapi Cloud configuration | ||
| - Strapi Cloud project | ||
| --- | ||
|
|
||
| # Middleware Configuration for Strapi Cloud | ||
|
|
||
| <Tldr> | ||
| On Strapi Cloud, middleware customizations must go in `config/env/production/middlewares`. Changes to the global config file are overwritten on deploy. | ||
| </Tldr> | ||
|
|
||
| :::prerequisites | ||
|
|
||
| - A local Strapi project. | ||
| - A Strapi Cloud project (see [Getting Started](/cloud/getting-started/deployment)). | ||
|
|
||
| ::: | ||
|
|
||
| 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](/cms/configurations/middlewares). | ||
|
|
||
| To apply custom middleware configuration on Strapi Cloud, place your changes in: | ||
|
|
||
| <Tabs groupId="js-ts"> | ||
| <TabItem value="js" label="JavaScript" default> | ||
|
|
||
| ``` | ||
| config/env/production/middlewares.js | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="ts" label="TypeScript"> | ||
|
|
||
| ``` | ||
| config/env/production/middlewares.ts | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| :::caution | ||
| 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` | ||
|
|
||
| Both CSP and CORS customizations can be combined in the same file. | ||
| ::: | ||
|
|
||
| :::note | ||
| - You can keep your existing `config/middlewares` file as-is as it will not cause conflicts. The production-specific file takes precedence on Strapi Cloud. | ||
| - Upload size limits on Strapi Cloud are enforced at the infrastructure level (Cloudflare gateway) and cannot be overridden via the `strapi::body` config. For external storage options, see [Upload Provider Configuration](/cloud/advanced/upload). | ||
| ::: | ||
|
|
||
| ## Custom Content Security Policy (CSP) | ||
|
|
||
| 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. | ||
|
|
||
| Create or update `config/env/production/middlewares`: | ||
|
|
||
| <Tabs groupId="js-ts"> | ||
| <TabItem value="js" label="JavaScript" default> | ||
|
|
||
| ```js title="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, | ||
| }, | ||
|
pwizla marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| }, | ||
| 'strapi::cors', | ||
| 'strapi::poweredBy', | ||
| 'strapi::logger', | ||
| 'strapi::query', | ||
| 'strapi::body', | ||
| 'strapi::session', | ||
| 'strapi::favicon', | ||
| 'strapi::public', | ||
| ]; | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="ts" label="TypeScript"> | ||
|
|
||
| ```ts title="config/env/production/middlewares.ts" | ||
| export default [ | ||
| '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, | ||
| }, | ||
|
pwizla marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| }, | ||
| 'strapi::cors', | ||
| 'strapi::poweredBy', | ||
| 'strapi::logger', | ||
| 'strapi::query', | ||
| 'strapi::body', | ||
| 'strapi::session', | ||
| 'strapi::favicon', | ||
| 'strapi::public', | ||
| ]; | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| :::tip | ||
| For a full list of upload providers and their required domains, see the <ExternalLink to="https://market.strapi.io/providers" text="Strapi Market"/>. | ||
| ::: | ||
|
|
||
| ## Custom CORS headers | ||
|
|
||
| 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. | ||
|
|
||
| <Tabs groupId="js-ts"> | ||
| <TabItem value="js" label="JavaScript" default> | ||
|
|
||
| ```js title="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', | ||
| ]; | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="ts" label="TypeScript"> | ||
|
|
||
| ```ts title="config/env/production/middlewares.ts" | ||
| export default ({ 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', | ||
| ]; | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.