Unexpected IP when call from frontend to backend in the same EC2

0

I have builded an EC2 that have frontend with VueJs and backend with NodeJs (backend and frontend in one instance)

I have set the inbound security group to accept traffic from 127.0.0.1, public and private IP of the instance itself.

But the frontend site always can not call API to the backend. I have change the backend IP to call from frontend with:

but still didn't work.

I have re-set the inbound security group to 0.0.0.0/0 then it works.

From the backend, I print out the request IP and it returned an IP that totally not related to any IP of the instance.

I re-set the inbound security group to that strange IP and it still work.

But when the instance turn off and on again, that strange IP has been changed althought I set Elastic IP for my instance.

How can I call API from my frontend to backend with localhost or it own public or private IP?

Thanks for reading!

Bach
asked 2 months ago161 views
1 Answer
0

Hello.

Since the front-end app runs on the browser, I think access to the back-end will be through the client's public IP address.
https://medium.com/@pabloregen/about-frontend-and-backend-and-where-my-javascript-runs-60e321d07790

profile picture
EXPERT
answered 2 months ago
  • Thanks for your answer. But that IP changed everytime I stop and start the instance again. If that is my PC public IP, I think it was stable.

  • Is it possible that the IP address of your ISP has changed?

  • How can I call API from my frontend to backend with localhost or it own public or private IP?

    If you configure the web server to use a proxy when accessing the API endpoint, the backend access will be localhost. For example, when I had an API like "/api/test", I think that if I set a proxy when accessing this URL, it would be accessed using localhost or a private IP of EC2.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions