Can't connect to node.js API, EC2 AWS - node.js

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

Related

How do I set up my Linux Azure VM so that I can connect via a browser?

I'm working on a Linux VM on Azure which was set up by someone else (so I don't know all the details). I'm trying to connect it to a domain name.
The server has a "Hello World" program, so when I go to "example.com" I should be seeing "Hello World". Currently I'm just getting
Safari can't open the page "http://example.com" because Safari can't find the server "my domain.com"
I thought I'd start with making sure that the IP address connects to the server (which it did at one point. So I enter the IP address of the server (let's say it's "12.345.678.901") in the browser, and it can't connect... I get the error
Can't open the page "12.345.678.901" because the server where this page is located isn't responding
There's an Inbound port rule to allow connections for port 8080, so I tried "12.345.678.901:8080" but this time got
Can't open the page "12.345.678.901:8080" because Safari can't connect to the server
I don't know what to try next. Presumably something needs to be enabled on the server to allow the browser to connect?
The other inbound port rules are ssh on port 22 (TCP) and then what I assume are the standard Azure ones (I can't edit or delete them anyway).
To view your Linux VM inside the browser, you need to install a web server. Easiest to install and get working straight away is nginx.
First thing you need to do is SSH(port 22) into your VM using the username and IP address of the machine:
ssh username#ipaddress
Which will prompt you to enter a passphrase to gain access to the VM.
This also assumes your SSH public key exists inside ~/.ssh/authorized_keys on the VM. If you don't have this setup then you need to get the owner of the VM to copy your public key into this file. Otherwise you won't be able to connect and get a Permission denied (publickey) error.
Assuming the above works, you can install the nginx webserver with the following two commands:
sudo apt-get -y update
sudo apt-get -y install nginx
Then once this web server is installed, add an HTTP inbound port 80 rule inside the network settings. For security reasons, having your web server listen on this port is probably unsecure long term. Its just easier to get working when you choose this port to begin with, because its the default.
You can see what the default listening port by viewing the server configuration host file with cat /etc/nginx/sites-available/default:
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Which shows the default port of 80. You can change this default port to 8080, then run sudo service nginx restart to restart the server and apply the changes. Additionally, you can have a look at this How to make Nginx Server Listen on Multiple Ports tutorial, which goes into more depth on how to configure listening ports for nginx webservers.
You should then be able to view your VM from a browser window(blurred out my IP address for security reasons):
You can also have a look at this Quickstart: Create a Linux virtual machine in the Azure portal tutorial for a step by step on how to get this setup in Azure.
You should first check to see if you have an entry for http://example.com. The reason could be that you do not have a DNS Entry and when you are trying to connect to it via the browser. Since you tried connecting to it via IP and it still did not work, I would suggest you check your Webserver configurations to make sure it is correctly listening for port 8080. Also, ensure that your webserver is also turned on as well. You can tail the webserver log and try to hit it via the IP like you did earlier and see if you see any errors in the logs. It would at least tell you if your request you are making on your browser is actually getting to the webserver.

Accessing cups from node within a docker container

So I have a node server within a docker container. Right now I would like to have it communicate with the parent system's CUP server. However when I do an ajax call to the server, with port 631 exposed I get a 400 bad request error.
When looking at the CUPS logs it gives this reason for the rejection:
Request from "localhost" using invalid Host: field "host.docker.internal:631"
Now to even access the parent machine I have to use host.docker.internal to gain access, but I have not figured out a way to get cups to ignore the host or think its localhost.
Cups is watching for any serverAlias, and anything on port 631 so it "should" accept the call. Any ideas?
I had the same problem with CUPS (2.3.4) on osx. I spent several hours to fix the invalid Host: field error.
It seems that there's a bug, even when using SeverAlias * on cups conf.
For those who are looking for a workaround:
We have to change the Host header sent from the docker container to localhost in order to do so, I managed to set up an Nginx container listening on port 8888 and rewriting the Host field while proxy_pass to the host’s CUPS server.
This is the nginx conf.d:
server {
listen 8888;
location / {
proxy_pass http://host.docker.internal:631;
proxy_set_header Host localhost;
}}
Now instead of connecting to host.docker.internal:631 we connect the cups client to localhost:8888. (I have set up the nginx sever on the same docker container, you might want to set up a separate container depending on your needs)

Can't access my nodeJS web app hosted on AWS EC2 through browser

I have hosted a nodeJS application on AWS ec2 instance which I can access through ssh but not through my browser i.e, http.
There is no error logged on the console it only shows the following message in the browser:
"unable to connect"
I am aware about security groups and checked them twice I've included http as well as https properly. Please help
Did you configure a web server to direct the requests from the AWS URL to the nodejs application you are running?
Try checking out nginx (https://www.nginx.com/), its really easy and fast to cofigure.
Here is a minimal configuration template:
server {
listen 80;
server_name <aws-url>;
location / {
proxy_pass 127.0.0.1:<node-port>;
}
}

Application Load Balancer https request to EC2 nodejs running on port 3000

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

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