Strapi listens on 127.0.0.1 rather than 0.0.0.0

System Information
  • 4.14.6:
  • Alpine Linux / Docker Container:
  • MySQL 8:
  • Node v20.10.0:
  • NPM 10.x:

I have a Strapi instance on a docker container behind a reverse proxy.
Locally from the container, Strapi is reachable, from outside the container I get “connection refused” errors.

Interesting fact is that Strapi is telling me at startup that it listens on 127.0.0.1, which would explain why strapi refuses external requests.

┌─────────────────────────────┐
│ http://127.0.0.1:1337/admin │
└─────────────────────────────┘

config/server.js tells me that the default is 0.0.0.0 and HOST is not set.

export default ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
  webhooks: {
    populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
  },
});

I guess that setting host directly in config/server.js would not change anything.

I have a number of containers that run various services, and a reverse proxy that distributes the requests. I also run other strapi instances that work. The environment is the same for all and this specific instance behaves the way i described. The only thing that puzzles me is that it tells me that it listens on 127.0.0.1:1337 which indicates that it only listes on requests to 127.0.0.1. The working Strapi instances listen on 0.0.0.0:1337.

Hey. You can look to this resource : networking - What's the difference between 127.0.0.1 and 0.0.0.0? - Super User

Yes, that’s true, but

when Strapi tells me that it listens to http://0.0.0.0.0:1337 I guess that it listens on all interfaces of its host or docker contaoiner for that matter. Therefore Strapi is reachable from the outside.

When Strapi tells me that it listens to http://127.0.0.1:1337 I guess that it listens on the loopback interface of its host or docker container. Therefore Strapi is NOT reachable from the outside.

This is the case with the installation I mentioned on my post. 127.0.0.1 and not reachable.

The problem is that I can not find any hint as to why Strapi uses 127.0.0.1. No envmironment variables, nothing.

The following lines in config.server.js

  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),

tell me that host is either the value of $HOST or 0.0.0.0.
Now $HOST is not set, so host: should be 0.0.0.0 but apparently it is 127.0.0.1 – hence no external access.

Found a solution:
Set env var HOST to 0.0.0.0, this is picked up by Strapi and then Strapi claims to listen on http://0.0.0.0.0:1337.

This is a bit strange, given the contents of config.server.js which should set 0.0.0.0as a default, but so be it.

Maye this is true for 4.14 only as my instances of 4.15 don’t need this fix, just guessing.