socket.io with nginx configuration - node.js

I am using Socket.IO on a Node server with Express and connection code of server side:
var http = require('http').Server(app);
var io = require('socket.io')(http);
Also client side connection like that:
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
var socket = io('https://example.com/example-service/');
And nginx conf is like that:
http {
upstream example-service {
server ip:8036;
}
server {
listen 6583;
#implementing WEB Socket
location / {
proxy_pass http://example-service/;
}
#my other application
location /test-app/ {
proxy_pass http://test-app/;
}
}
My socket connection code is in example-service and want to connect socket to other portal like test-app. If I given root path to socket service then its working proper. but if I passing nginx conf like that
# location /example-service/ {
# proxy_pass http://example-service/;
}
That time socket giving error 404.
Is there something I have to configure on client and server or Nginx?

Related

How to serve static files (CSS, ...) with multiples Express app + NGINX as reverse proxy server

Context
I'm runnig multiples nodesJS/Express app on the same server with the same IP adress.
I use Nginx to reverse proxy those apps and redirect it to subfolder adress (and not subdomain, i don't want to).
ex : http://123.0.0.1:8000 => http://monsite.com/Site1
Problem
My assets files (css, images, ...) do not load, I have a 404 error on those static files when the page loads. It happens only when I access the site via the proxy redirect http://monsite.com/Site1 and not when I use the IP adress : http://123.0.0.1:8000
I don't have this problem if a use the reverse proxy location from the root in the nginx conf :
location / {
but I want to access the site from a subfolder adress
My integration
Tree files:
var/www/html
|Site1/
| |server.js
| |Views/
| | |index.pug
| |Public/
| | |Css/
| | | |Style.css
|Site2/
|....
nodejs server code
const PORT = 8000;
const HOSTNAME = 'www.monsite.com';
// Dependencies.
const express = require('express');
const http = require('http');
// Initialization.
var app = express();
var server = http.Server(app);
app.set('port', PORT);
app.set('view engine', 'pug');
app.set('views','Views');
app.use(express.static('Public'));
app.use('/', (request, response) => {
response.render('index');
});
server.listen(PORT, HOSTNAME, function() {
console.log(`STARTING SERVER ON PORT ${PORT}`);
});
index pug code
doctype html
html
head
title Site 1
link(rel="stylesheet" href="/Css/style.css")
body
p Hello
nginx conf
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name www.monsite.com;
location / {
#Reserved for another site
}
location /Site1/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://123.0.0.1:8000/;
}
}
PS : I tried almost all the solutions and code I found searching for this problem and nothing worked, that's why I'm asking directly here. Thank you.
I think the issue is with the url in the link tag to load the css, the url is invalid because the url is actually /Site1/Css/style.css.

SSL certificate on NginX

I have a DO droplet with nginX running 8 node apps as proxy servers.
For one of those i have a specific domain (e.g. 192.22.XX.20: 8888 -> mydomain.com) and I need HTTPS to get audio from users mic.
I have a PositiveSSL certificate defined on NginX but when i try to use it on that node app, nothing works. What Am I doing wrong?
nginx.default.conf
...
ssl on;
ssl_certificate /etc/nginx/ssl/.../ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/.../private.key;
ssl_prefer_server_ciphers on;
...
#proxy to a node app running on 8005 port
location /interpretame/ {
#return 301 $scheme://localhost:8005$request_uri;
proxy_pass http://localhost:8005/;
}
node app.js
```
...
var https_options = {
ca: fs.readFileSync("./cert/example_ca.crt"),
key: fs.readFileSync("./cert/example.key"),
cert: fs.readFileSync("./cert/example.crt")
};
...
https.createServer(https_options, app).listen(port, function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening over HTTPS at ' + port);
});
```
How do you define the 'nothing works'? :)
The application does not boot at all? If yes, it is quite possible that this happens if you start the application with node app.js. You have to start the application with sudo node app.js and by the way this is very bad practice.

Node JS TCP Proxy: Reuse socket in callback function

I'm trying to implement a TCP proxy in Node JS. I only have some experience with Javascript so I met a lot of problems along the way. I've done a lot of searching for this one but had no luck.
The problem occurs when browser sends a CONNECT request for HTTPS. My proxy will parse the host name and port, and then create a new socket that connects to the server. If all these steps went well, I will start forwarding message.
Part of my code looks like this:
var net = require('net');
var server = net.createServer(function(clientSock) {
clientSock.on('data', function(clientData) {
var host = // get from data
var port = // get from data
if (data is a CONNECT request) {
// Create a new socket to server
var serverSock = new net.Socket();
serverSock.connect(port, host, function() {
serverSock.write(clientData);
clientSock.write('HTTP/1.1 200 OK\r\n');
}
serverSock.on('data', function(serverData) {
clientSock.write(serverData);
}
}
}
}
Since the CONNECT request needs both client socket and server socket open until one side closes the connection, the code above doesn't have this behavior. Every time I receive some data from client, I will create a new socket to server and the old one is closed.
Is there a way to store the server socket as a global variable so that the data event handler can reuse it? Or is there any other way to solve this?
Thanks a lot!!!!
You can just move the variable up to a higher scope so it survives across multiple events and then you can test to see if its value is already there:
var net = require('net');
var server = net.createServer(function(clientSock) {
var serverSock;
clientSock.on('data', function(clientData) {
var host = // get from data
var port = // get from data
if (data is a CONNECT request) {
// Create a new socket to server
if (!serverSock) {
serverSock = new net.Socket();
serverSock.connect(port, host, function() {
serverSock.write(clientData);
clientSock.write('HTTP/1.1 200 OK\r\n');
}
serverSock.on('data', function(serverData) {
clientSock.write(serverData);
}
} else {
serverSock.write(clientData);
}
}
}
}

Socket.IO connection error

I've an application in node.js with socket.io. Everything was running completely fine, but suddenly the browser started to send this error.
failed: Error in connection
establishment:net::ERR_TUNNEL_CONNECTION_FAILED
I didn't make any code change.
The protocol used by socket is ws:// and when I try to use this url in browser
'ws://highgarden-nodejs-91180.sae1.nitrousbox.com/socket.io/?EIO=3&transport=websocket&sid=T9Unec8KbWw-GAL8AAAF'
Chrome returns this error:
Failed to load resource: net::ERR_DISALLOWED_URL_SCHEME
This is a part of the socket setup code:
server.js:
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000
*------------------------------------------*
// routes ===
var routes = require('./config/routes.js');
var sock = {}
routes(app, passport, sock);
io.sockets.on('connection', sock.update);
// launch ===
server.listen(port);
Thanks advance.
Hi the exception ERR_TUNNEL_CONNECTION_FAILED happens when a tunnel connection through the proxy could not be established. And the ERR_DISALLOWED_URL_SCHEME happens when the scheme of the URL is disallowed.
May you need use it behind the proxy!
Chrome 45.0.2454.101 m says the page has been disabled or moved on the server.

Simple socket.io example not work

This is server file
var io = require('socket.io').listen(7000);
io.sockets.on('connection', function(socket) {
console.log("### new connection ###");
});
This is for client
$j(document).ready(function() {
var socket = io.connect('http://address.net:7000');
console.log(socket);
});
Server starts correct without any errors.
I copied from node_modules and attach to my site < head > section file "socket.io.min.js" file for client from dist folder.
And in output of server isn't any ### new connection ### when I run my site.
Why?
Problem resolved. It was about wrong server address in socket.io connection function.

Resources