Ngnix configuration load balancer node js server - node.js

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;
}
}

Related

How to reverse proxy to a Node.js app in a domain subfolder with Nginx on CentOS?

I have a domain https://ytdownvideo.com running a WordPress website.
I want to run a Node.js app in a subfolder on the same domain, as follows: https://ytdownvideo.com/youtube/
I am using Nginx on CentOS 7 x64.
How can I configure Nginx to reverse proxy to the Node.js app when navigating to this subfolder?
First, you will need to run your Node.js app on a different and unused port. For example, 3000.
Then, add the following to your nginx configuration:
location ~ ^\/youtube.*$ {
proxy_pass http://localhost:3000;
port_in_redirect off;
}
Restart nginx with:
sudo systemctl restart nginx

How to connect nginx to local mongodb

I've got nginx to run my (node.js/react) application on the server. But I can't seem to connect to the database.
In the nginx.conf file I've added the following inside http.
http {
...
server {
listen 80;
server_name localhost;
...}
...}
And above the http section I have the following,
stream {
server {
listen 4000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass stream_mongo_backend;
}
upstream stream_mongo_backend {
server 127.0.0.1:27017;
}
}
I start the nginx server, the application runs on localhost, opens up the login page but I can't login because it's still not connected to the database (mongodb).
I'm not sure if I've got my port numbers wrong or if I'm missing some configuration line inside nginx.conf.
EDIT: Ok, I had it wrong before. I wasn't supposed to connect to mongodb at this point. I was supposed to connect to the backend server of my application which would run at 4000. So now I've added a new location for /api/ inside http and proxied all requests to 4000. I still have one question though. I have to run my backend server separately for this to work. For the frontend I've created a folder and put all my build files in there so nginx starts up the webserver from there. Doing the same for the backend did not start up the server. Is there a way to get nginx to start the backend server as well?
Also can I get the frontend to run directly without the build files ? Like node would with npm start?
the port number is right. try to open up a mongo shell and see if you are able to access a mongo instance. if not, you will need to run sudo service mongodb start to start it up.
Guess it's knida late but you don't need to setup nginx for your backend to connect local mongodb.
And you need to run the frontend and backend server first by yarn start, node run or something like that if you want to run it without build files.
And then bypass the calls from 80 port to the local host servers.
For example, your FE run at 3000 port, BE run at 5000 port.
Then your nginx should be:
http {
...
server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass localhost:5000;
}
location / {
proxy_pass localhost:3000;
}
...}
...}

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

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