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');
?
Related
So I used certbot to configure Nginx for https. That worked well (I used the automatic configuration).
Now how can I configure my Node.js back end to be able to make GET/POST requests from the front end that is being hosted with Nginx?
EDIT:
location /api {
proxy_pass http://localhost:3000; #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;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
If you have correctly configured your Nginx with SSl and your node application. The requests you send should work with https://URL . Please check your nginx.conf and verify the following things.
You can add ssl cert in Nginx with
ssl_certificate /etc/nginx/ssl/server.crt #cert location
ssl_certificate_key /etc/nginx/ssl/server.key #key location
in your server block in Nginx.And your nodejs App should be configured like this in the server block
location / {
proxy_pass http://localhost:3000; #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;
}
This will be sufficient for your server to work with SSL.
I'm running an application which accepts large file uploads, but also should allow the user to make other POST requests to the same app while the upload is it progress.
The app is running on node.js, with the upload being handled by formidable and s3stream to directly stream the binary content to S3 without using disk space on the server.
It seems like this is an NGINX configuration issue, as everything else works just fine locally.
My current NGINX config looks like this:
server {
listen 443 ssl http2;
server_name upload.app.io;
underscores_in_headers on;
location / {
proxy_request_buffering off;
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;
}
ssl_certificate /etc/nginx/ssl/ca-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
}
So why is it that NGINX blocks any parallel POST requests while the upload is in progress, and how do I overcome this problem?
To clarify, NGINX does not throw an error, but rather hangs the requests until it times out.
Greatly appreciate any help with this.
I've since figured out one approach to handling the problem. It may not be the best practice but it works for what I need.
I'm now using two separate location blocks to handle the requests. One for file uploads, and another one for everything else. This way, I'm able to using the /upload block without buffering directly streamed to S3 via the app, and have the / block handle all other requests normally with buffers.
Here's the config:
server {
listen 443 ssl http2;
server_name upload.app.io;
underscores_in_headers on;
location /upload {
client_max_body_size 0;
proxy_request_buffering off;
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;
}
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;
}
ssl_certificate /etc/nginx/ssl/ca-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
}
client_max_body_size 10m;enter link description here
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 tried proxying my nodejs application running at http://localhost:7000 to domainname.com/api using nginx config but domainname.com/api works perfectly however domainname.com/api/info is not
Here is my nginx config
server {
listen 80;
server_name domainname.com
location /api/ {
proxy_pass http://localhost:7000;
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;
} }
Figured out the issue. trailing slash is the one required as shown
proxy_pass http://localhost:7000/;
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.