I found that no matter what port I chose to run Gatsby on a default AWS Cloud9 Ubuntu instance, external HTTP connections to those ports were rejected with "the site refused to connect."
However, I could successfully reach Gatsby locally by telneting to those same ports. Also, other socket servers, such as Strapi, would happily accept external connections on these ports too. Therefore I suspected the cause wasn't an AWS networking issue by itself, but perhaps a poor interaction unique to Gatsby.
While I'm still not sure of the ultimate networking level cause, I noticed when launching Gatsby to listen to "localhost" (not specifying a host via -H on the command line), that Gatsby was binding to a IPv4 socket, and not an IPv6 socket.
While it suspect this is likely either a conflict with the AWS VPC or the C9 instance's default networking configuration, I was able to address the problem for now by removing the "program.host" argument altogether from the server.listen() call within the Gatsby package (see below). I did this because I wasn't able to figure out what hostname I should pass on the command line to get it to open a ip6 socket, "ip6-localhost" did not work, for example (see /etc/hosts file below).
While my workaround functions, it's made in the core Gatsby module, so perhaps this needs to be addressed in Gatsby somehow? Perhaps at least a warning.
Initially, when Gatsby was running on default port 8000, "netstat -tnlp" reported that Gatsby was using the "tcp" rather than "tcp6" protocol:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 989/sshd
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 4457/node
tcp6 0 0 :::22 :::* LISTEN 989/sshd
tcp6 0 0 :::1337 :::* LISTEN 1361/node
Note port 1337 here is Strapi, 8000 is Gatsby.
After modifying the Gatsby package to not pass the program.host at all, netstat would instead report that Gatsby was binding to a tcp6 friendly socket:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 989/sshd
tcp6 0 0 :::22 :::* LISTEN 989/sshd
tcp6 0 0 :::1337 :::* LISTEN 1361/node
tcp6 0 0 :::8000 :::* LISTEN 4270/node
Default Cloud9 Ubuntu 18.04.3 instance:
System:
OS: Linux 4.15 Ubuntu 18.04.3 LTS (Bionic Beaver)
CPU: (1) x64 Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz
Shell: 4.4.20 - /bin/bash
Binaries:
Node: 10.16.2 - ~/.nvm/versions/node/v10.16.2/bin/node
Yarn: 1.17.3 - /usr/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v10.16.2/bin/npm
Languages:
Python: 2.7.15+ - /usr/bin/python
npmPackages:
gatsby: ^2.15.20 => 2.15.20
gatsby-image: ^2.2.20 => 2.2.20
gatsby-plugin-manifest: ^2.2.18 => 2.2.18
gatsby-plugin-offline: ^3.0.8 => 3.0.8
gatsby-plugin-react-helmet: ^3.1.8 => 3.1.8
gatsby-plugin-sharp: ^2.2.25 => 2.2.25
gatsby-source-filesystem: ^2.1.26 => 2.1.26
gatsby-source-strapi: 0.0.9 => 0.0.9
gatsby-transformer-sharp: ^2.2.16 => 2.2.16
npmGlobalPackages:
gatsby-cli: 2.8.5
/etc/hosts file:
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
To get Gatsby to accept connections, I made a very small modification to node_modules/gatsby/dist/commands/develop.js lines 324-5:
# Commented out the program.host variable:
const listener = server.listen(program.port, /* program.host, */ err => {
Does setting the host have any change to the issue gatsby develop -H 0.0.0.0?
Indeed, that works. Nice.
Interestingly, netstat still returns that Gatsby is listening to the "tcp" protocol, rather than "tcp6", which is what I thought had been the issue. Still not sure what's going on, ultimately, but glad I can connect on Cloud9 without modifying the core code now.
Glad that helped. I had a similar issue when trying to access the dev server on my network. Setting the host did the trick.
Setting the host fixed it for me to when running on an ec2 instance ubunutu server
Most helpful comment
Does setting the host have any change to the issue
gatsby develop -H 0.0.0.0?