Can't proxy request with nginx to node.js (socket.io) - node.js

Tutorials and other answers doesn't help. I know that thing is in nginx config, but where?
I have CentOS 6.5, nginx 1.4.4, node 0.10.24, socket.io.js 0.9.16 and
in my_server_nginx.conf, node section:
location ~ /node {
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_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
in app.js:
var io = require('/usr/lib/node_modules/socket.io/lib/socket.io').listen(3000);
in html:
<script type="text/javascript" src="/node/socket.io/socket.io.js"></script>
in browser console:
Uncaught ReferenceError: io is not defined
Direct open /node/socket.io/socket.io.js returns
Welcome to socket.io.

In this case, '/node/socket.io/socket.io.js' is your socket root directory. and the socket.io js file will be served from within there. try linking to '/node/socket.io/socket.io.js/socket.io.js'

Related

404 - Trying to acess socket.io over nginx

I am very lost at the moment, so much so that I actually signed up because of my problem. So here we go: I am running a System with Plex and I am trying to get a node.js Backend with socket.io to work that is running behind Nginx.
The proxy pass for my HTML (I am using ejs to be precise, not sure if relevant) hosted via express works like a charm. But I get a 404 error when my website tries to connect to my backend via socket.io.
My Nginx directives as configured via Plex:
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:50090;
}
location /socket.io/ {
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:50090/socket.io/;
}
My client connecting to the backend:
var socket = io.connect('https://subdomain.mydomain.com', {secure: true});
My Backend listening:
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io').listen(http);
http.listen(50090, "127.0.0.1");
There is a lot about this on the internet, but it seems like I tried everything by now and nothing worked. Likely because I am very new to this.

Serve express dynamic URL using NGINX with Nodejs reverse proxy

I am using Express for serving static files and dynamic URLs on my nodejs app. I am generating a URL based on the :id in Mongodb and then on NGINX I am using Nodejs reverse proxy to serve this path to the user but it doesn't work whatever I try. It always says "CANNOT GET.."
I am running NGINX 1.14.2, Node 8.14.0. I have tried NGINX rewrite directive and various wild cards ( ~, ~^, ., etc.) but none of them have worked. I have included the current express code and NGINX config below. Any help is hugely appreciated
Express
router.get('/dynamic-uri/:fooId', function (req, res) {
res.render('index');
});
The resultant URL with :fooId is
https://example.com/myNodeApp/dynamic-uri/5c35e51228122504b57126d7
NGINX Config
location /myNodeApp/dynamic-uri {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
Of course the :fooId doesn't exist as a static path and to serve static files i have the following block which works fine
location ~* /myNodeApp/(static|img|media|app|css|js) {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}

Nginx not listening to ports

I am trying to set up a Nginx as a reverse proxy to access multiple NodeJS apps running on the same server.
I have my nodeJS apps running with PM2 and it all seems fine:
My nodeJS app is the simple nodeJS app generated with express-generator, so it is supposed to be running on port 3000.
I have also set up my Nginx with the following config
server {
listen 1004;
server_name pumadashboard.com;
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;
}
}
However when I curl 127.0.0.1:1004 I get a badGateway error from Nginx. I am also not able to access pumadashboard.com from anywhere on my local network, it just loads until timeout.
What do you get if you do:
curl http://locahost:3000
This should give back a response and like that you will understand if the application started properly.
This nginx configuration works for me
upstream pumadashboard.com {
server 127.0.0.1:3010;
}
server {
listen 80;
server_name pumadashboard.com;
root <path to your node application>;
access_log /var/log/nginx/your-access.access.log;
error_log /var/log/nginx/your-error.error.log;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://pumadashboard.com/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
Try adding
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port 80;
to your location / block

Hosting two Node.JS apps on same domain

I have two node js applications I am running on the same box and I would like for it to run the first node js app for all routing except if the url is www.domain.com/blog to go to the other node js application. Is this even possible with this setup or do I have to setup subdomains and use nginx or something?
You can achieve this using nginx as a reverse proxy.
Assuming you have your blog node process running on port 3000 and another node process on 3001 a simple config would look like;
upstream blog {
server 127.0.0.1:3000;
}
upstream other {
server 127.0.0.1:3001;
}
server {
listen 80;
server_name www.domain.com;
location /blog {
proxy_pass http://blog;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
location / {
proxy_pass http://other;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
}

Nginx conf for socket.io

I am using socket.io on my nginx server.
I followed the doc from nginx to configure my server (http://nginx.com/blog/nginx-nodejs-websockets-socketio/):
location = / {
proxy_pass_header Server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
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_set_header X-Forwarded-Proto $scheme;
proxy_pass http://frontend;
proxy_intercept_errors on;
error_page 501 502 503 504 /50x.html;
}
upstream frontend {
server 127.0.0.1:7300;
}
However I still get the following error in my browser:
WebSocket connection to 'ws://<MY DOMAIN>/socket.io/?EIO=3&transport=websocket&sid=oway-6XKt7_HvqfQAAAC'
failed: Error during WebSocket handshake: Unexpected response code: 400
Everything works fine locally so I get the problem comes from my nginx.
What should I change to make it work?
Many thanks

Resources