Heath degrading of Elastic Beanstalk - node.js

I have set up my node.js server using elastic beanstalk. Recently I have seen that my server health is degrading to sever from ok state and I keep getting emails for that.
While checking the logs in the error. logs of Nginx I found out that most of the errors are occurring because of
2022/04/23 18:34:14 [error] 3620#0: *14 upstream prematurely closed connection while reading response header from upstream, client: 172.31.54.200, server: , request: "POST /parse/functions/processCloverRequest HTTP/1.1", upstream: "http://155.0.0.1:8081/parse/functions/processCloverRequest", host: "server-i9cck-env.us-east-1.elasticbeanstalk.com"
I have also handled each error in the application and increased the read and connect timeout of Nginx.
location / {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
}
But still I am getting the emails and the health of my instances degrades. Is there a way to resolve this issue?

Related

twilio application showing 404 error after deployment to Linux server

We have developed a testing application in Twilio Sandbox and it is running fine in Heroku server. But when we are trying to put it in Linux (centos 8.1.1911) server, getting 404 error.
conf file in Linux server:
location /myapp/ {
proxy_pass http://13.71.23.139:3020/;
proxy_ssl_server_name on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
error:
*212 connect() failed (111: Connection refused) while connecting to upstream
Any suggestions?

nodejs + nginx getting 502 error when using https module instead of http

I have make my nodejs app, hosted it on digital ocean server connect it to domain name, and all works fine, but when i'm trying to put ssl certificate (using https module instead of http), it doesn't works. Here is a code:
var sslopt = {
key : fs.readFileSync('./ssl/server.key'),
cert : fs.readFileSync('./ssl/server.crt'),
ca : [fs.readFileSync('./ssl/ca1.crt'), fs.readFileSync('./ssl/ca2.crt')]
};
var server = https.createServer(sslopt,function(req,res){
...
});
server.listen(8001,function(err){
...
});
My nodejs app running fine but if i'm trying to access it, I just see the 502 Bad Gateway error, and no requests was sent to my nodejs app. When I have opened my nginx error log I see the errors
-date- -time- [error] 18116#18116: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: -ip-, server: -server-, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: -host-
But the most strange thing that if I'm trying to get access with https protocol and port 8001 (https://{domainname}.com:8001) I can see my app working fine, but connection is not secured.
I just can't understand what I'm doing wrong...
P.S.
my nginx config file
server {
listen *:443;
listen *:80;
server_name {myhostname};
access_log /var/log/nginx/qt.access.log;
error_log /var/log/nginx/qt.error.log;
root /srv/qt;
index index.html index.htm index.php;
# Headers to pass to proxy server.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_redirect off;
# Go to next upstream after if server down.
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 5s;
# Gateway timeout.
proxy_read_timeout 20s;
proxy_send_timeout 20s;
# Buffer settings.
proxy_buffers 8 32k;
proxy_buffer_size 64k;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Recognizing that this is an old post, hopefully this will still help someone out. I just figured out what seems to be the same thing and it was proxy server related.
My NodeJS server worked fine without SSL but gave the 502 without ever reaching the server when I used https.
// =============================================================================
// START THE SERVER
var port = process.env.PORT || 8080; // set our port
https.createServer({
key: fs.readFileSync('./certs/private.key'),
cert: fs.readFileSync('./certs/cert.crt')
}, app).listen(port);
//app.listen(port);
console.log('API Active on port ' + port);
My client and server are behind a pfSense firewall using a Squid3 proxy server.
pfSense controls the DNS for api.mydomain.com and routes the traffic to my dev server when I make a call to api.mydomain.com:8080/myroute. I have a valid SSL/TLS cert for api.mydomain.com
With the app.listen(port) line uncommented everything worked great.
With the https.createServer(...) uncommented I got the 502 error and traffic never reached the server.
To fix:
In pfSense click Services -> Squid Proxy Server
Click on the ACLs
configuration page
At the bottom of the page find the section
called Squid Allowed Ports.
In the field For ACL SSL Ports, enter the port your
application is using (in my case 8080).
For me, rainbows appeared and bluebirds did sing.

Socket.io behind load balancer

I am using socket.io behind nginx which is behind azure load balancer. How ever On the client side I am constantly getting errors like
WebSocket connection to 'ws://**********/socket.io/? session_id=i9bk_iqkVrUveKwvIBz4fMNDbkoYuaITQ_APO73sgQd6-tQBaRkjp8RR8N9LTA5LnqMeKXzZg5AXXgjEevFKqSKRJJI8iaK3&id=dc978ae038af4746baf68ead35d182f4&EIO=3&transport=websocket&sid=61LGLpdw53xaMYqBAAJR' failed: Error during WebSocket handshake: Unexpected response code: 502
Also nginx gives the error below
[error] 15348#0: *84812 upstream prematurely closed connection while reading response header from upstream, client: 10.100.50.14, server: _, request: "GET /prod/socket.io/?session_id=nR0P30IDeUutoavDyjcqAQ8hUw_3l7dtAHQ3tqzW4zVT8eBOxwbHZq_7mWd9K7qRNO2Aq45QXm8w2KSvzyFlq3O4w7P2tl2q&id=955bb63a4f804b42b9d85ac8cf9172a7&EIO=3&transport=websocket&sid=xXGRnAsjKX6Gj-SAAAls HTTP/1.1", upstream: "http://127.0.0.1:3000/socket.io/?session_id=nR0P30IDeUutoavDyjcqAQ8hUw_3l7dtAHQ3tqzW4zVT8eBOxwbHZq_7mWd9K7qRNO2Aq45QXm8w2KSvzyFlq3O4w7P2tl2q&id=955bb63a4f804b42b9d85ac8cf9172a7&EIO=3&transport=websocket&sid=xXGRnAsjKX6Gj-SAAAls"
Anyone have an idea about the reason?
Nginx conf:
proxy_pass http://lb-prod/; # Load balance the URL location "/" to the upstream lb1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
The problem here was about session stickyness. Configuring my load balancer to use sticky session solved the problem!
There is a blog from Nginx offical website which is the best practice for using NGINX and NGINX Plus with Node.js and Socket.IO.
I think it's very helpful for you now.

socket io with node js is not connecting through nginx proxy_pass

The issue is when i use nginx the socket is not establishing.Any one help me in steps to follow to integrate Socket oi with nginx.
i tried this
location /field {
# the following is required for WebSockets
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# supposedly prevents 502 bad gateway error;
# ultimately not necessary in my case
proxy_buffers 8 32k;
proxy_buffer_size 64k;
# the following is required
proxy_pass
proxy_redirect off;
# the following is required as well for WebSockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
tcp_nodelay on; # not necessary
}
First of all check your nginx version. According to that page websockets supported after v1.3.13;
http://nginx.com/blog/nginx-nodejs-websockets-socketio/
Then compare your nginx conf with the below configuration as stated in the nginx blog;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 192.168.100.10:8010;
}
server {
listen 8020;
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
http://nginx.com/blog/websocket-nginx/
Also check your firewall configuration for the port you chose for socket.io server. (I've read that some ISPs are blocking websocket connection for ports other than 80 & 443, please also check if server receive packets using tcpdump etc.)
If everyhing is okay up until now, check your nginx error logs (/var/log/nginx/error.log) to see if there are any socket.io related error messages. You can paste it here for further analysis.
Then if there is no socket error in nginx logs start your node app with DEBUG mode on as below;
DEBUG=* node yourfile.js
And check if any socket connection message is printed to console. You can also paste it for further analysis.

Nginx - Node.js error with upstream

Have a Frontend server which accesses a backend api all written in node.js.
Currently have Nginx with Phusion Passenger configured to launch the node app
Everything has been running fine when all environments (dev.qa.prod) experienced this similar issue which crashed our Frontend servers. Restarting Nginx allowed the application to work.
The Errors look like this
[error] 25833#0: *14050 upstream prematurely closed connection while reading response header from upstream, client: 10.0.0.183, server: 54.148.10.11, request: "POST /api/course/54e113e8d98e579c1a790bbd/step/54e113e8d98e579c1a790bbe HTTP/1.1", upstream: "https://54.201.58.163:443/api/course/54e113e8d98e579c1a790bbd/step/54e113e8d98e579c1a790bbe", host: "dev.****.net", referrer: "https://dev.***.net/08f35b9a752554df591279a88babad96fd7e88021084d0396ef7bda16798eaa5743bfc4880015294dd3199482dd9fc564bbf6a681b6881eb5d91b369059a1643"
Nginx Configuration under sites-available/default:
server {
server_name 10.0.0.134;
listen 80;
root /home/ubuntu/web/portal;
passenger_enabled on;
passenger_set_cgi_param _PASSENGER_NODE_CONTROL_SERVER 1;
location /api {
access_log off;
proxy_pass https://dev-api.***.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300;
}}

Resources