I am trying to proxy websocket to port 80 using nginx. We have a tomcat application running on port 8080 and the node application running on port 8888. I have been trying to proxy them to port 80 using nginx but for some reason the connection isn't being established. I am getting the following error in the console:
WebSocket connection to 'ws://chat-local-dev.guestops.com/ws/chat?access_token=bfb83713f8abecb6c3d36d3dc74c31b9&sessionId=undefined' failed: WebSocket is closed before the connection is established.
Here is my nginx configuration:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name chat-local-dev.guestops.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
server {
listen 80;
server_name api-local-dev.guestops.com;
location / {
proxy_pass http://localhost:8881;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
server {
listen 80;
server_name console-local-dev.guestops.com;
location / {
proxy_pass http://localhost:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
}
I am able to run the sites on port 80 but however I am unable to run the chat between client and the server.
Any help will be highly appreciated, I can provide with the node files as well but they are big bunch of files but I can provide with the necessary files if required.
I hope I am clear enough. Thanks in advance!
nginx added websocket support in version 1.3. So you have to upgrade to version 1.3.x to use it.
Related
I'm trying to run node-media-server on a EC2 instance, but i a'm not able to make OBS to connect to the server, here is my Nginx config:
server {
listen 8000;
listen 1935;
server_name example.com;
location / {
proxy_pass http://localhost:$server_port;
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;
}
}
And here is my security group set up:
Any idea what is going on?
Thanks in advance,
I found the problem, the first thing is to setup Nginx to listen on the port 80 only as node-media-server takes care of listening on the ports 8000 and 1935
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:$server_port;
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;
}
}
Second thing is to make sure the ports 8000 and 1935 are open in the server as by default they are not.
Hope it helps to anyone with the same problem.
I have an node.js application that is running on port 3000.
Infront of it i run an nginx reverse proxy. It works fine for port 80. I have tried to install an certificate with certbot. Now i have the certificate and set up my proxy to redirect all non HTTPS traffic to HTTPS and on the port 443 i listent to it and pass my connection to my application. Somehow my browser is pending and i dont know why.
Here i have added 2 server blocks:
server {
server_name mywebsite.at www.mywebsite.at;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/mywebsite.at/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.at/privkey.pem;
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;
}
}
server {
server_name mywebsite.at www.mywebsite.at;
listen 80;
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;
}
}
In this case i can enter http://mywebsite.at but i cant enter https://mywebsite.at. It says "cant reach the website". Any idea why this error appears?
I have runned sudo nginx -t there are no erros.
I have found the problem guys. My port 443 was not open. ufw allow 443 fixed the issue.
I am currently trying to deploy my NODEJS application on a nginx debian remote server .
It works very well in localhost.
But I have some difficulties to make websocket work in remote nginx server.
I use "ws" nodejs module.
This is how i declared my websocket server side :
var WebSocket_ = require('ws');
var wss = new WebSocket_.Server({port: 40510});
And this is how i opened my websocket client side :
var ws = new WebSocket('ws://localhost:40510');
I know i have to configure /etc/nginx/sites-available/default on my Nginx VPS :
Do i need to add a websocket block location and define a specific proxipass ?
If yes how ?
Do I have to replace var "ws = new WebSocket('ws://localhost:40510');" by another instruction in my client side code ?
Thank you in advance for your answers !
If you already have a server block, put this inside (usually inside sites-available or nginx.conf):
location /ws {
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;
proxy_pass http://localhost:40510;
}
Now, depending on your Nginx listening ports you will configure your client IP/Port (By default Nginx listens on port 80)
var ws = new WebSocket('ws://localhost:80/ws');
Full configuration file (example):
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
fastcgi_buffers 64 4K;
server {
listen localhost:80 default_server;
server_name localhost;
# Logs
access_log /var/log/nginx/main.access.log;
error_log /var/log/nginx/main.error.log;
# Websocket
location /ws {
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;
proxy_pass http://localhost:40510;
}
}
}
Thank you very much
I already use a block location, with a redirection proxi reverse :
location / {
proxy_pass http://localhost:8888;
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;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
}
should not I use my domain name instead?
Something like:
var ws = new WebSocket('http://vincentfritz-dev.fr/ws');
?
I have the need to run 2 servers, one in Node.js and one with Meteor, let's say on my_server.com
The Node server listens on my_server.com:8080,
The Meteor server listens on my_server.com:3000
I'd like to open just the port :80, and then redirect the user with vhost of Node according to the subdomain, so
node.my_server.com:80 should go to my_server.com:8080
meteor.my_server.com:80 should go to my_server.com:3000
and I want to open just one port. Is this possible?
Thank you
Yes, it's totally possible, you should use nginx of apache for that.
Here is example nginx config:
server {
listen *:80;
server_name node.my_server.com;
access_log /var/log/nginx/node.access.log;
error_log /var/log/nginx/node.error.log;
location / {
proxy_pass http://127.0.0.1: 8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $remote_addr;
}
}
And similar one for meteor
server {
listen *:80;
server_name meteor.my_server.com;
access_log /var/log/nginx/meteor.access.log;
error_log /var/log/nginx/meteor.error.log;
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 X-Forwarded-For $remote_addr;
}
}
I have a private messaging app built with nodejs and socketio which works in the url http://localhost:3000
I'm wondering how would I integrate it in a dedicated server like Amazon's EC2? I can understand that it will work on http://someip:3000 but I want the chat application to work inside a website, just like facebook. How do I set it up to work on all website pages?
Thank you
Basically you need a web server to proxy your app in the port 80 (http), this is a basic configuration for Nginx where the node app running in 127.0.0.1:3000 will be proxied to the port 80 of www.example.com that should point to the EC2 public IP:
upstream node {
ip_hash;
server 127.0.0.1:3000;
}
server {
server_name www.example.com;
access_log /var/log/nginx/www.example.com-access.log main;
error_log /var/log/nginx/www.example.com-error.log warn;
listen 80;
location / {
## Socket.io
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
## End Socket.io
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 Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node;
proxy_redirect off;
proxy_max_temp_file_size 0;
}
}