Refused to connect to http://localhost:1337/admin/init

System Information
  • Strapi Version: 4.1.3
  • Operating System: Linux
  • Database: Postgres 10
  • Node Version: 14.18.1
  • NPM Version: 6.14.16
  • Yarn Version: 1.22.17

Hello everyone, I’ve deployed Strapi on AWS ECS using Fargate arc but I got an error Refused to connect to http://localhost:1337/admin/init because it does not appear in the connect-src directive of the Content Security Policy. when entering http://.com/admin. I’m confused why it calls localhost:1337 even port 80 has been defined in the configuration. Note that when I enter http://.com, it shows the Strapi first page without any errors, and also on my localhost, it’s working perfectly.

Here is my env on AWS ECS

NODE_ENV=PRODUCTION
DATABASE_HOST=xxx.ap-southeast-1.rds.amazonaws.com
DATABASE_PORT=5432
DATABASE_NAME=backend
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=xxx
DATABASE_TIMEZONE=Asia/Bangkok
DATABASE_SCHEMA=public
AWS_ACCESS_KEY_ID=xxx
AWS_ACCESS_SECRET=xxx
AWS_REGION=ap-southeast-1
AWS_BUCKET_NAME=xxx-bucket
API_TOKEN_SALT=xxx
JWT_SECRET=xxx
PORT=80

Thank you in advance.

I solved this problem regard to this issue Strapi is ignoring the server config PUBLIC_URL and calling back to localhost:port from the browser · Issue #12129 · strapi/strapi · GitHub
Just run a simple command npm run build in the Dockerfile and everything works.

1 Like

For anyone else having this issue, I had the same exact thing running the quickstart guide. The issue for me was Strapi wasn’t respecting the environment variable I setup in the .env file, or the one that came with the quickstart guide either (it was using 8080 for the admin UI, but 1337 for the services, which was failing).

Changing the port specifically to 1337 in /config/server.js (it was using “env.int(‘PORT’, 1337” beforehand) fixed it!