2

I have an application running on a server at port 6001(frontend, built by react.js) and port 6002(backend, built by node.js) in EC2 instance.

When I send a request through ubuntu terminal in the instance by curl -X GET http://127.0.0.1:6002/api/works, It works fine, I get a proper data.

Now I go to a browser with the domain (http://example.com). However, I only get the front end getting called. When I send a request on a browser to the backend server, It gives me an error GET http://127.0.0.1:6002/api/works net::ERR_CONNECTION_REFUSED (the domain goes through ELB)

Here's my nginx config.

server {
        listen 80;
        listen [::]:80;

        server_name example.com;

        root /home/www/my-project/;
        index index.html;

        location / {
                proxy_pass http://127.0.0.1:6001/;
        }

        location /api/ {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_redirect off;
                proxy_pass http://127.0.0.1:6002/;
                proxy_set_header X-Real-IP $remote_addr;
        }
}

my case is similar to this, he/she says

My entry point via the aws dns takes me to the React app 127.0.0.1:4100 that is the port 80 nginx is listening to. The react app is the one that makes the backend calls and it is coded to make the backend api calls on 127.0.0.1:1323/api. The client error you saw was because it is displaying what the react app is trying to call in the server, but nginx is not redirecting that call to the app that is running at that port. Does that make sense?

the selected answer didn't work on me.

Also, According to the comment, the problem is solved by sending a request by http://AWS_IP/ on the react app. but I'm not sure If it's a good solution for me since there's no point to use ELB then? If I understand the concept of ELB right? I think the requests need to be done via ELB?

Please help, this is driving me crazy.

Mijeong Won
  • 291
  • 5
  • 16
  • This is because of your backend URL, See my explanation below in answers, Also I've couple of suggestions – Sam Jun 02 '20 at 18:18

1 Answers1

2

From your question, I understood the following things,

  1. Your Domain is pointing to Amazon ELB
  2. And there is a VM behind this ELB, and it has Nginx and 2 applications in it.
  3. Nginx is listening on port 80 and Backend application is listening on port 6002 and frontend is listening on port 6001

  4. YOUR FRONTEND APPLICATION IS CALLING THE BACKEND FROM YOUR LOCAL BROWSER USING http://127.0.0.1:6002/api/works

Here is the problem,

You can curl 127.0.0.1 from the same instance where the application is running(listening to port 6001) because you are hitting the localhost of that instance, And it is different when your web application running on your local browser because your react application(all javascript application) executes in your local machine and for backend call, it is hitting the localhost(in your case) and returning CONNECTION REFUSED.

So the solution is you've to change the backend URL so that it should look something like http://yourdomain/api/works

In Addition to this, I've a couple of suggestions on your configuration.

  1. You don't need a separate web server for your frontend since you can use the same Nginx.
  2. Make sure that your ELB target port is 80 or the same port that NGINX is listening to.
  3. And close the ports 6001 and 6002 (if it is publically accessible)
Sam
  • 4,046
  • 8
  • 31
  • 47
  • THANK YOU SO MUCH! It's embarrassing to let everyone know I have lack of knowledge in this industry. but I'm really happy to learn!! thanks for the explanation, now I understand how it works thanks to you! – Mijeong Won Jun 03 '20 at 01:11
  • btw Could you explain more about the "1. You don't need a separate web server for your frontend since you can use the same Nginx" ? or give me a keyword to search? – Mijeong Won Jun 03 '20 at 01:12
  • @MijeongWon The Nginx itself is a web server and it is capable of serving static files, like HTML CSS and js. Since your application is react.js you can use NGINX as a web server as well. – Sam Jun 03 '20 at 09:17
  • @MijeongWon please check this link https://dev.to/xarala221/the-easiest-way-to-deploy-a-react-web-application-2l8a – Sam Jun 03 '20 at 09:18