Nodejs Nginx 502 Bad Gateway - node.js

I have set up my server on Digital Ocean and running it on port: 3000 using pm2. when I curl to my server in the do it responds correctly.
curl http://localhost:3000/
This is my nginx configuration.
server {
listen 80;
#root /var/www/html;
# Add index.php to the list if you are using PHP
#index index.html index.htm index.nginx-debian.html;
server_name api.skreem.co;
access_log /var/log/nginx/api.skreem.access.log;
error_log /var/log/nginx/api.skreem.error.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
I am getting logs on my Nginx access and error logs as follows
#access_log
106.51.111.247 - - [20/Mar/2020:09:27:34 +0000] "GET /favicon.ico HTTP/1.1" 502 584 "http://api.skreem.co/api/v2/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/
537.36"
#error_log
2020/03/20 09:27:34 [error] 1651#1651: *121 connect() failed (111: Connection refused) while connecting to upstream, client: 106.51.111.247, server: api.skreem.co, request: "GET /favicon.ico HTTP/1.1", upstream: "ht
tp://127.0.0.1:3000/favicon.ico", host: "api.skreem.co", referrer: "http://api.skreem.co/api/v2/"
On my Pm2 monitor I am not getting any Logs. I have rechecked the proxy_pass but still not able to get nginx to pass it on to the nodejs server.
Am getting
502 Bad Gateway error

Related

nodejs nginx 504 gateway timeout reverese proxy

I am trying to run a NodeJS application on port 2000 of an ubuntu server.
the application runs using pm2.
As far as I can tell the application is running fine as i can use the command:
curl http://localhost:2000
And I get the web page back almost instantly.
The problem I have is that I am trying to use nginx as the reverse proxy and for SSL. From an SSL perspective this works great as trying to browse to the website 'http://dreamingoftech.uk' I get redirected to the HTTPS version and I get the green padlock. Happy days.
However unfortunately I don't get the webpage but a 504 error.
The strange thing is, if I run:
time -p GET -H 'Host: dreamingoftech.uk' http://127.0.0.1:2000
I do eventually get the webpage returned (although i have to admit that I don't really know what this command is doing)
I am using the basic nginx.conf file except with these values added:
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
and my sites-available/default file is as follows:
#HTTP
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# HTTPS — proxy all requests to the Node app
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name dreamingoftech.uk;
# Use the Let’s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pe$
ssl_certificate_key /etc/letsencrypt/live/dreamingoftech.uk/privkey.$
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:2000;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
proxy_http_version 1.1;
}
In the access logs I get this error, although I am not sure if this is a red herring:
"GET / HTTP/2.0" 504 665 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
My question really is could anyone advise me on the correct setup to get my website to load in a timely manner. I can't understand why a higher timeout is needed if the server is capable of delivering the page almost instantly (although again this could be my naivity).
Thanks in advance
Steve

Can't request POST method in NginX

I deploy node application on cloud and cloud use nginx for reverse proxy I can request GET method but I can't request POST method. Please help me for allow POST method.
My config
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
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;
proxy_pass http://localhost:3000;
proxy_redirect off;
}
}
And error log in NginX show
2017/03/14 09:21:00 [warn] 10102#10102: invalid value "POST" in /etc/nginx/sites-enabled/default:58
2017/03/14 09:21:00 [warn] 10105#10105: invalid value "POST" in /etc/nginx/sites-enabled/default:58
2017/03/14 10:17:10 [error] 13483#13483: *6 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xxx.xx.xx.xxx, server: localhost, request: "POST /api/v1/users/$
2017/03/14 10:21:42 [error] 13483#13483: *11 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xxx.xx.xx.xxx, server: localhost, request: "POST /api/v1/users$

Howto debug when nginx gives 502 bad gateway?

On landing.example.com:10000 have I a webserver that works fine, which is a Docker container that exposes port 10000. Its IP is 172.17.0.2.
What I would like is having a nginx reverse proxy on port 80, and send the visitor to different Docker containers depending on the URL they visit.
server {
listen 80;
server_name landing.example.com;
location / {
proxy_pass http://172.17.0.2:10000/;
}
access_log /landing-access.log;
error_log /landing-error.log info;
}
When I do this, I get 502 Bad Gateway and the log says
2016/04/14 16:58:16 [error] 413#413: *84 connect()
failed (111: Connection refused) while connecting to upstream, client:
xxx.xxx.xxx.xxx, server: landing.example.com, request: "GET / HTTP/1.1",
upstream: "http://172.17.0.2:10000/", host: "landing.example.com"
try this:
upstream my_server {
server 172.17.0.2:10000;
}
server {
listen 80;
server_name landing.example.com;
location / {
proxy_pass http://my_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_redirect http:// $scheme://;
}
}
Here you define the upstream server (your server by IP or hostname)
and make sure to forward the headers too so the server answering knowns who to answer to.

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

Web Sockets not working for SSL, NGINX and Node JS

Quick breakdown -
Dev environment works properly. SSL Cert on local server, nginx configured for proxy pass on 443 traffic.
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
for some reason, version 1.1 never works (always tosses an error)
Again, Dev environment works properly.
Production environment is giving me the following error:
WebSocket connection to 'wss://website.com/sockjs/421/fin4cg38/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
I use a load balancer which has the SSL cert on it and we enforce SSL through our application.
Now, I checked the configuration of NGINX and I have it as follows:
server {
listen 80;
server_name www.website.com;
error_log /var/log/nginx/sites-error.log;
return 301 https://website.com$request_uri;
}
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://localhost:3000;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
The way its set up is anything on 80 goes to 443, etc etc.
If I uncomment the proxy_set_headers, our sign up breaks and the chrome console doesn't show any errors.
error log has this:
2014/07/13 18:15:16 [error] 16175#0: *93 connect() failed (111: Connection refused) while connecting to upstream, client: 10.189.245.5, server: _, request: "POST /sockjs/421/wq2dqb2w/xhr_send HTTP/1.1", upstream: "http://[::1]:3000/sockjs/421/wq2dqb2w/xhr_send", host: "website.com", referrer: "https://website.com/signIn"
2014/07/13 18:15:42 [error] 16487#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 10.189.245.5, server: _, request: "GET /sockjs/120/95m6dozn/websocket HTTP/1.1", upstream: "http://[::1]:3000/sockjs/120/95m6dozn/websocket", host: "website.com"
2014/07/13 18:16:12 [error] 16487#0: *12 connect() failed (111: Connection refused) while connecting to upstream, client: 10.189.245.5, server: _, request: "GET /f1be3f98162c975fdc9524a0a222f9a02cc6dcde.js HTTP/1.1", upstream: "http://[::1]:3000/f1be3f98162c975fdc9524a0a222f9a02cc6dcde.js", host: "website.com", referrer: "https://website.com/signIn"
I'm not really sure what's going on. Any help would be awesome.
Thanks!

Resources