Application Load Balancer https request to EC2 nodejs running on port 3000 - node.js

I have an ALB that is in HTTPS that will request to my EC2 instance.
I configured the ALB listeners to HTTP/HTTPS then target my EC2.
When I try to access my ALB with these:
https://domainSample
Response = Welcome to nginx
https://domainSample/api/getSample
Response = 404 Not Found nginx
https://domainSample:3000
No Response
This is my nginx configuration in EC2 that runs on port 3000
server {
listen 80;
server_name domainSample;
location / {
try_files $uri $uri/ =404;
}
}
Where did I go wrong?

I have search and read about the documentation on AWS and do some tweek and test to the application.
What I understand in the flow of the request from the ALB to EC2.
In configuring the ALB, In Target Groups, we need to set the target of its request which will be the EC2 that your application is running on to.
For instance, we have Node js running on to port 3000 in the EC2.
We will add the target instance which we specify the port on 3000.
This solved my problem. Thanks

Related

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":"adriel#admin.com","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
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.
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

Nginx reverse proxy in docker for express.js server

In docker, I have a client facing server container, an api server container and a nginx container that is exposed outside at port 8000. After running api and client server container, I am running nginx container with has nginx.conf replaced with the following:
events {
}
http {
server {
listen 80;
location /api {
proxy_pass http://server:9002;
}
location / {
proxy_pass http://client:9001;
}
}
}
I can verify client server working properly through nginx by making request to http://localhost:8000 or http://localhost:8000/blah. But if I go to the URL http://localhost:8000/api, it redirects to http://localhost/api and fails to connect.
I verified that the server container is actually running by running it with a separate exposed port and it returns result on request as expected. How would I fix this ?
It might have been an environmental variable issue. I had PORT env var set on docker-compose.yml but not in Docekrfile, adding it solved the issue. Removing PORT env var from docker-compose.yml still works.

Nginx upstream servers all go down when one of them shuts down

I'm trying to set up upstream servers with nginx. All run the same Node.js app on port 8080 with pm2. Here is the nginx default.conf of the main server
upstream backend {
ip_hash;
server localhost:8080;
server sv1_ip_address;
server sv2_ip_address;
}
server {
listen 443 ssl;
location / {
proxy_pass http://backend;
...
}
...
}
And on sv1 and sv2, I have the same default.conf as follows
server {
listen 80 default_server;
location / {
proxy_pass http://localhost:8080;
...
}
}
Now when I tried shutting down either sv1 or sv2 (using pm2 kill for Node or even reboot), all upstream servers went down and I receive a 500 error (?) when accessing the app by the domain name. So I thought there was something wrong with nginx on those secondary servers and I replaced upstream backend with this
upstream backend {
ip_hash;
server localhost:8080;
server sv1_ip_address:8080;
server sv2_ip_address:8080;
}
and now shutting down or rebooting were handled correctly (meaning nginx will route the requests to one of the living servers). Is this an expected behavior, or am I doing something wrong here? I don't think routing requests directly to port 8080 is a good idea though.
I donot know why you had to install nginx service on sv1 and sv2 servers.
When you reboot sv1 , sv2 servers, it should be enabling nginx first. Please check service nginx status is running or not once reboot is done.
And you kill node meaning application is down, so you got 500 error on nginx

Ngnix configuration load balancer node js server

I have developed a Rest API with node js on a machine with ubuntu server 16.04 and on another machine with same SO I have installed Nginx as a reverse proxy. Now I would like to set my Rest API in load balancing deploying my application on two servers.
How can I configure Nginx as a load balancer with to server that exposes the same node js application?
edit your /etc/nginx/sites-available/default according to DigitalOcean such as
upstream backend_hosts {
server host1.example.com;
server host2.example.com;
server host3.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_hosts;
}
}

Azure App Service getting Error 404 when redirected via NGINX

I created a VM, port 80 is open and installed NGINX on it.
I created 2 App Services which can be accessed via x1.azurewebsites.net and x2.azurewebsites.net
I configured the VM to act as an load balancer but when redirecting the traffic I get the following: https://i.gyazo.com/b94bed9c90d3b0f0c400c83f762f0544.png
I am not using my own domain. Does someone know what the issue could be?
I got the following configurations:
upstream backend {
server xx.azurewebsites.net;
server xxx.azurewebsites.net;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name_;
location / {
proxy_pass http://backend;
}
}
Azure App Service uses cookies for ARR (Application Request Routing). You have to make sure that your NGinx reverse proxy configuration pass the correct cookie / header to your web app.
The other possibility (to make sure the behavior comes from ARR) is to disable it: https://azure.microsoft.com/en-us/blog/disabling-arrs-instance-affinity-in-windows-azure-web-sites/

Resources