I've dealt with this error in the nginx error log for the past few hours.
*2 connect() failed (111: Connection refused) while connecting to upstream, client: my ip, server: my domain, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:3000/", host: "my domain"
I'm currently trying to deploy a next.js app with nginx using engintron for CPanel as well as pm2.
default.conf
server {
listen [::]:80 default_server ipv6only=off;
server_name my domain domain-ip;
# deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy
# Set the port for HTTP proxying
set $PROXY_TO_PORT 8080;
include common_http.conf;
common_http.conf
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
There aren't any errors on pm2's front, and sudo nginx -t works just fine, so I'm confused on what exactly the issue is.
Any sort of help is appreciated, have a good rest of your day :)
Fixed the issue, for some reason my pm2 wasn't working properly, after a clean reinstall I got this issue fixed, but now I've got a 404 not found for my index file, the fun life of being a developer lol
Related
I'm deploying my project, I had no problem until i decided to buy a domain for my digital ocean droplet i added the records from my namecheap domain to my droplet i was working on it then suddenly when i want access to myproject/api it's now showing 502 errors i'll give you all the informations so maybe you can help me with it
1 ) mongoDB atlas status : All good ; i even tried " npm start " to see if DB can't connect but it connects perfectly fine. I refreshed in .env the connecting string, it stills connect with no problems.
2 ) Nginx error.log shows this information i don't really understand, i guess it's the key point :
86 connect() failed (111: Connection refused) while connecting to upstream, client: 164.92.206.160, server: _, request: "GET /api/category/cinema HTTP/1.1", upstream: "http://[::1]:8000/api
3 ) The front is perfectly showing himself on my project domain url it starts showing 502 error when i click something using my DB/server.
4 ) i rebuilded and restarted front & back + nginx multiple times
5 ) i updated endpoints and .env variables to match with my fresh domain and it works, only the API side is breaking
6 ) nginx conf file :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location /api {
proxy_pass http://localhost:8000;
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;
}
location / {
proxy_pass http://localhost:3000;
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;
}
I tried barely everything and it's still not working as it was used to work.
I am currently trying to run Nginx as a reverse proxy for a small Node application and serve up files for the core of a site.
E.g.
/ Statically served files for root of website
/app/ Node app running on port 3000 with Nginx reverse proxy
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
# Set path for access_logs
access_log /var/log/nginx/access.example.com.log combined;
# Set path for error logs
error_log /var/log/nginx/error.example.com.log notice;
# If set to on, Nginx will issue log messages for every operation
# performed by the rewrite engine at the notice error level
# Default value off
rewrite_log on;
# Settings for main website
location / {
try_files $uri $uri/ =404;
}
# Settings for Node app service
location /app/ {
# Header settings for application behind proxy
proxy_set_header Host $host;
# proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Proxy pass settings
proxy_pass http://127.0.0.1:3000/;
# Proxy redirect settings
proxy_redirect off;
# HTTP version settings
proxy_http_version 1.1;
# Response buffering from proxied server default 1024m
proxy_max_temp_file_size 0;
# Proxy cache bypass define conditions under the response will not be taken from cache
proxy_cache_bypass $http_upgrade;
}
}
This appeared to work at first glance, but what I have found over time is that I am being served 502 errors constantly on the Node app route. This applies to both the app itself, as well as static assets included in the app.
I've tried using various different variations of the above config, but nothing I can find seems to fix the issue. I had read of issues with SELinux, but this is currently not on the server in question.
Few additional bits of information;
Server: Ubuntu 18.04.3
Nginx: nginx/1.17.5
2020/02/09 18:18:07 [error] 8611#8611: *44 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: x, server: example.com, request: "GET /app/assets/images/image.png HTTP/1.1", upstream: "http://127.0.0.1:3000/assets/images/image.png", host: "example.com", referrer: "http://example.com/overlay/"
2020/02/09 18:18:08 [error] 8611#8611: *46 connect() failed (111: Connection refused) while connecting to upstream, client: x, server: example.com, request: "GET /app/ HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "example.com"
Has anyone encountered similar issues, or knows what it is that I've done wrong?
Thanks in advance!
It's may be because of your node router.It's better to share nodes code too.
Anyway try put your main router and static route like app.use('/app', mainRouter); and see it make any sense?
Hard to write a proper title.
I have a pure websocket application which is based on an express server. In development mode the express server does some simple routing to access the html, js and png files. Basically it is a one page app that only handle websocket protocol. But in production mode, I delegate all that to Nginx so I don't do any routing in express production. I was expecting Nginx will find all these files on its "root" directory. I get a "Cannot Get /" error however.
TL;DR version:
When I use Nginx to serve static contents for a one page app, do I need to do anything in express routing? If so, how?
UPD (my Nginx settings, can find another more detail question here):
upstream upstream_project {
server 127.0.0.1:8888;
keepalive 64;
}
server {
listen 0.0.0.0:80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/local/share/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name ws_server;
location / {
try_files $uri $uri/ index.html;
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_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://upstream_project;
proxy_read_timeout 240s;
}
}
UPDATED:
I have found in Nginx log that the client is trying to access the server via ipv6. See this:
kevent() reported that connect() failed (61: Connection refused) while
connecting to upstream, client: ::1, server: ws_server, request: "GET /
HTTP/1.1", upstream: "http://127.0.0.1:8888/", host: "localhost"
I remove the ipv6 server listen line (listen [::]:80 default_server ipv6only=on;) and have to change the try_files line to use file name explicitly.
try_files $uri $uri/app.js ;
Then I can get my app working. But I don't understand. Why do I have to do this?
I still can't access the static png file from the subdirectory of "root" folder. Any help would be appreciated.
NGINX supports WebSocket by allowing a tunnel to be set up between a client and a backend server. For NGINX to send the Upgrade request from the client to the backend server, the Upgrade and Connection headers must be set explicitly, as in this example:
location /wsapp/ {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Take a look on this NGINX guide for more details and examples.
I try to use Iframely. I install the self hosted version on my server ubuntu + nginx:
https://iframely.com/docs/host
When i start node like this:
# node server
Iframely works well
Otherwise, i get a 502 bad gateway error.
ERROR
In the log error:
2016/01/25 06:06:58 [error] 13265#0: *4476 connect() failed (111: Connection refused) while connecting to upstream, client: xx:xx:xx:xx:xx:xx, server: iframely.custom.com, request: "GET /iframely?url=http://coub.com/view/2pc24rpb HTTP/1.1", upstream: "http://127.0.0.1:8061/iframely?url=http://coub.com/view/2pc24rpb", host: "iframely.custom.com"
When i try:
# curl -i 127.0.0.1:8061/iframely?url=http://coub.com/view/2pc24rpb
It confirm the error:
curl: (7) Failed to connect to 127.0.0.1 port 8061: Connection refused
I begin with node and i understand that maybe node.js is not listening on port 8061.
When i try:
netstat -pantu
I don't see the port in question but others like this one used by another node.js app which works perfectly:
tcp6 0 0 127.0.0.1:4567 127.0.0.1:60724 ESTABLISHED 12329/node
CONFIGURATION
My host configuration:
upstream iframely.custom.com {
ip_hash;
server localhost:8061;
keepalive 8;
}
server {
listen 80;
listen [::]:80;
server_name iframely.custom.com;
root /usr/share/nginx/html/iframely.custom.com;
# Logs
access_log /var/log/iframely.access_log;
error_log /var/log/iframely.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 Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://iframely.custom.com/;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Exclude from the logs to avoid bloating when it's not available
include drop.conf;
}
I have tried to change in the configuration localhost for 127.0.0.1 but it doesn't change anything.
How to keeps a node.js app alive: do i have to restart it forever?
Could it be a problem with ipv4 or ipv6?
I post this question on serverfault because i was thinking it's a problem with nginx configuration. But someone suggest i am wrong.
Thank you in advance for any help.
jb
Firstly, you should make node application to listen port 8061 and it should be shown in "netstat -tpln" e.g.:
tcp 0 0 127.0.0.1:8061 0.0.0.0:* LISTEN 21151/node
Secondly, you should test it with curl. If the response is taken, then node server works perfectly.
Finally, shift focus to nginx.
With only one backend, there's no benefit to using the upstream module. You can remove your upstream section and update your proxy_pass line like this:
proxy_pass http://127.0.0.1:8061/;
It's also possible the backend is listening on the IP, but is not responding to the name "localhost". It's unlikely, but possible. But it must be listening on some IP address, so using the IP address is safer.
The advice above by Vladislav is good, too.
I solve the issue using forever: https://github.com/foreverjs/forever
I'm using Express.js to create a server to which I can connect using web sockets.
Even though it eventually seems to work (that, is connects and passes an event to the client), I initially get an error in Chrome's console:
Unexpected response code: 502
On the backend, the socket.io only logs warn - websocket connection invalid.
However, nginx logs this:
2012/02/12 23:30:03 [error] 25061#0: *81 upstream prematurely closed
connection while reading response header from upstream, client:
71.122.117.15, server: www.example.com, request: "GET /socket.io/1/websocket/1378920683898138448 HTTP/1.1", upstream:
"http://127.0.0.1:8090/socket.io/1/websocket/1378920683898138448",
host: "www.example.com"
Note: I have nginx dev running: nginx version: nginx/1.1.14 so it should support HTTP/1.1.
Also note that if I just use the node.js server without the nginx it works without any warnings.
Finally, here is my nginx config file:
server {
listen 0.0.0.0:80;
server_name www.example.com;
access_log /var/log/nginx/example.com.log;
location / {
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://node;
proxy_redirect off;
}
}
upstream node {
server 127.0.0.1:8090;
}
Any help would be greatly appreciated. I tried the fix suggested in this question but that didn't work either.
nginx has some kind of Web Socket support in unstable 1.1 branch only. See Socket.IO wiki.
Afaik there are currently only few stable Node.js based http proxies that support Web Sockets properly.
Check out node-http-proxy (we use this):
https://github.com/nodejitsu/node-http-proxy
and bouncy:
https://github.com/substack/bouncy
Or you can use pure TCP proxy such as HAproxy
Update!
nginx (1.3.13>=) supports websockets out of the box!
http://nginx.org/en/docs/http/websocket.html