'Can't connect to node.js API, EC2 AWS
I am trying to deploy my nodeJS API at Ubuntu Amazon Web Services. The app.js is runing at port 3002
As you can see my app.js
is running at port3002 and firewall is allowing the connection with this port
Next test is send a request to my API using curl
curl -d '{"email":"[email protected]","password":"Zxycok159!"}' -H 'Content-Type: application/json' http://localhost:3002/user/login
I got the expected response with curl
.
res:
token: f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b
To finish the process I will check my security groups, to allow 3002 port, and my private ip at AWS console.
public IPv4
18.221.231.202
private IPv4
172.31.17.178
It seems to be all right, but when I try to enter in a regular browser http://172.31.17.178:3002
I got a timeout response, can't connect properly and have an API response.
Res:
Unable to access this site 172.31.17.178 took too long to respond.
Try:
Check the connection
Check proxy and firewall
Run Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT
I tried the most common solutions (AWS EC2 security groups, Ubuntu Firewall, Curl tests) to this issue, but can't connect to API in the browser, any suggestions?
I don't know how can I have the properly response like when I do the same process at my local machine, at my local browser receive the expect response
Solution 1:[1]
The right away to configure nginx for both (front end and backend)
server {
listen 80 default_server;
server_name _;
# react app & front-end files
location / {
root /opt/front-end/dist;
try_files $uri /index.html;
}
# node api reverse proxy
location /api/ {
proxy_pass http://localhost:3002/;
}
}
This is the correct away to configure a website at 80
Solution 2:[2]
You're trying to use the private IP of the instance. Use the public Ip of the instance. Private IP is only accessible within your AWS VPC (This depends). Public IP is what clients use to access your application.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Adriel Kirch |
Solution 2 | Supun Sandeeptha |