Strapi v4 deployed to AWS EBS trying to load resource from localhost

I have deployed Strapi v4 to AWS Elastic Beanstalk. The landing page loads fine but navigating to /admin shows the loading spinner followed by a toast saying “Warning: An error occurred while requesting the API”. Looking in the developer console, there are several content-policy error logs, of which one is
Content Security Policy: The page’s settings blocked the loading of a resource at http://localhost:1337/admin/project-type (“connect-src”).. This seems odd given that the site is hosted on a remote server. Any thoughts on this?

Is your default port is 1337 ? if not i’m pretty sure that the admin build has webpack errors. So, check the eb logs if it says webpack built with errors all you need to do is to fix those errors.

Hello @isfaaq Here is my decision on your case:

Go and edit: project root/config/middlewares.js and update your code:

module.exports = [
  "strapi::errors",
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": [
            "'self'",
            "data:",
            "blob:",
            "https://!!!BUKETNAME-EDIT!!!.amazonaws.com/",
          ],
          "media-src": ["'self'", "data:", "blob:"],
          upgradeInsecureRequests: null,
        },
      },
     
    },
  },
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::favicon",
  "strapi::public",
  "strapi::session",
];

This solution works and has been tested with the latest version: 4.0.7

The default port is 1337
Eb log does not show any error, however nginx/error.log shows the following;

an upstream response is buffered to a temporary file /var/lib/nginx/tmp/proxy/1/00/0000000001 while reading upstream

I tried resolving this issue, and I’m not sure if it’s related to the one under discussion

My root/config/middlewares.js is like the one provided, except strapi::session is missing. Adding that line prevents strapi from building with the following error

Middleware strapi::session not found.

@isfaaq
Which version are you using exactly?
There are changes after the version 4.0.6

Try to remove “strapi::session” and rebuild

I am on version 4.0.7
Even after adding the session middle ware + associated keys, the issue persists

removing strapi session would return me to square one

@isfaaq
Can you share your configuration for:
/config/plugins.js
/config/middlewares.js
/config/server.js

Can you also share the path of a file that has been uploaded.

config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'aws-s3',
      providerOptions: {
        accessKeyId: env('AWS_ACCESS_KEY_ID'),
        secretAccessKey: env('AWS_ACCESS_SECRET'),
        region: env('AWS_REGION'),
        params: {
          Bucket: env('AWS_BUCKET'),
        },
      },
    },
  },
  // ...
});

config/middlewares.js

module.exports = ({ env }) => [
  'strapi::errors',
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', `${env('AWS_BUCKET')}.s3.${env('AWS_REGION')}.amazonaws.com`],
          'media-src': ["'self'", 'data:', 'blob:', `${env('AWS_BUCKET')}.s3.${env('AWS_REGION')}.amazonaws.com`],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::favicon',
  'strapi::public'
];

config/server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: env('APP_URL')
});

I haven’t uploaded anything yet

I use the same configuration and it works for me.
Check what settings you have in Amazon.
For a test you can do them as I showed in the screenshot.
It is very difficult for me to debug the problem in this way without being able to simulate it for myself.

Try a new build and clear the cache.

I figured out what’s wrong. I was using the command strapi develop to run the application on the production server. This explains why the server attempts to load contents from localhost. The solution is there to change the run script to strapi start.

Hey, thanks! This did the trick! :cowboy_hat_face: