Proxing Websocket Traffic from Nginx to Socket.io (without SSL) - node.js

As of yesterday nginx started to support websocket connections, therefore i was trying to get my nginx-nodejs-socket.io application to work without harproxy ect (not much luck though).
What i want exactly to achieve is nginx to send only websocket connection requests to a backed server,or websocket server ,socket.io to be more exact, while in the same time nginx will be serving php files, and all static content including html files.I dont want express to serve static content at all (if this is possible).
Here is my nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream backend {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name localhost;
charset UTF-8;
#access_log logs/host.access.log main;
location / {
root /website/html_public;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /website/html_public;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /website/html_public;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location /connection {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root /website/html_public;
# index index.php index.html index.htm;
# }
#}
}
Here is my server.js file in node
var express = require('express');
var app = express();
var port = 8080;
/* HTTP Server*/
server = require('http').createServer(app);
server.listen(port);
app.use(express.logger(':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'));
app.use(express.static(__dirname + '/html_public'));
app.use(express.favicon());
app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index.html');
});
/*
* Web Sockets
*/
io = require('socket.io').listen(server),
io.configure('production', function(){
io.enable('browser client etag');
io.set('log level', 1);
io.set('transports', [ 'websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling' ]);
});
console.log('Chat Server started with Node '+ process.version +', platform '+ process.platform + 'to port %d',port);
From my client, i try to connect like this :
socket = new io.connect('http://localhost/connection');
Now, the problem is that when i try to connect normally, typing localhost, i see on chrome console:
**GET http://localhost/socket.io/socket.io.js 404 (Not Found)** , and also when type in the browser http://localhost/connection i receive "Cannot GET /connection" which is telling me that nginx doesn't proxy websockets normally with my current configuration.
Thanks in advance for your answers.

Let me guess, you want:
location /connection {
proxy_pass http://backend/;
...
}
but you have:
location /connection {
proxy_pass http://backend;
...
}
http://nginx.org/r/proxy_pass

Just to make sure, do you have Nginx version 1.3.13 ? ( ex. Nginx's PPA don't have that version yet.)

Related

Nodejs not running in server

I am trying to launch nodejs in my server. this is my node.js code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'xx.xx.xx.xx');
console.log('Server running at http://xx.xx.xx.xx:8080/');
this is my /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server{
location / {
proxy_pass http://xx.xx.xx.xx:8080;
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 also deleted a few lines from default.conf because it was showing up error nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) and following #rednaw's answer
I am trying to run node.js in my xx.xx.xx.xx server. Now it's just showing this at http://xx.xx.xx.xx/ and This site can’t be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED at http://xx.xx.xx.xx:8080/
You have to mention the port number i.e not in use to listen.
Do it some thing like this----
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8083);
In my scenario the port number 8080 was already in use....So i used
another port number like 8083....It works...

how to check nginx is serving static file or not

This is my nginx.conf file. i want to serve all static contents that are located in different directories.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 8080;
server_name localhost;
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;
access_log C:/Program Files/nginx-1.11.7/logs/access.log;
}
location ~* \.(css|js|gif|jpe?g|png|html)$ {
root D:/gamma-master;
autoindex on;
access_log C:/Program Files/nginx-1.11.7/logs/access.log;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
How to check nginx is serving static file or not. Also how should give static file path if my static content is located in different folders under root folder

Nginx Reverse Proxy not working with Nodejs at localhost

I have a dedicated server with 5 usable IP address,say X.X.X.1 - X.X.X.5 below is my default schema for IP addresses
X.X.X.1 - ns1.ex.com - name server using BIND
X.X.X.2 - ns2.ex.com
X.X.X.3 - www.ex.com - using nginx as the web server
X.X.X.4 - nothing
X.X.X.5 - nothing
Now I am trying to do reverse proxying on nodejs(127.0.0.1:4501).... I have started the app and when I am trying to access the node app through reverse proxy it's not working. I have even tried to call curl like http://localhost:4501/ and also http://localhost:4501/test/ as the app.js is present at /var/www/test/app.js. I also tried changing the app ip addresses to X.X.X.3 but with no result
When I don't set the IP address of the node app then it is working on any port that I put in. I want to set the IP address to localhost so that it is only accessible through Nginx and I have my database that I also want to hide behind Nginx.
Below are my conf files:
nginx.conf: present at /usr/local/nginx/conf/nginx.conf
#===============================================================================
# Main Configuration Settings
#===============================================================================
user root admins;
worker_processes auto;
master_process on;
worker_rlimit_nofile 16384;
worker_priority 0;
#================================================================================
# Error Log Setting Goes HEre
#================================================================================
events {
multi_accept off;
worker_connections 5120;
}
http {
include mime.types;
default_type application/octet-stream;
open_file_cache max=10000 inactive=30s;
open_file_cache_errors on;
client_body_buffer_size 200M; #200 MB
log_not_found on; #LOG All 404 error code
log_format main '{'
' IP:"$remote_addr:$remote_port", Time:"$time_local", Request_Type:"$request", '
' Status:"$status", Referer:"$http_referer", '
' Agent:"$http_user_agent", Forwarded_By:"$http_x_forwarded_for" '
'}';
#===========================================
# Caching DNS records For 1 HR
#==========================================
resolver 8.8.8.8 8.8.4.4 valid=1h;
resolver_timeout 10s;
#sendfile on;
keepalive_timeout 10;
#=================================================================================
# Gzip Module
#=================================================================================
gzip on;
gzip_comp_level 4;
gzip_min_length 20;
gzip_vary on;
gzip_proxied any;
upstream localhost_servers {
server 127.0.0.1:4501;
keepalive 64;
}
server {
listen 80;
server_name www.ex.com ex.com;
charset UTF-8;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost_servers;
proxy_redirect off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
app.js: at /var/www/test/app.js
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(methodOverride());
app.get('/',function(req,res){
console.log(req.ip,req.host,req.path,req.originalUrl);
res.send(req.body);
});
app.listen('127.0.0.1',4501);
console.log('Magic happens on port 80');
It was a Very silly mistake from my side as pointed by #Ben Fortune express listen method takes port first and then ip address

Can't hide location's port with nginx

I'm trying to set up a domain for my node project with nginx (v1.5.11), i have succesfull redirected the domain to the web, but i need to use 3000 port, so now, my web location looks like http://www.myweb.com:3000/ and of course, i want to keep only "www.myweb.com" part like this: http://www.myweb.com/
I have search and try many configurations but no one seems to work for me, i dont know why, this is my local nginx.conf file, i want to change http://localhost:8000/ text to http://myName/ text, remember that the redirect is working, i only want to "hide" the port on the location.
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost:8000/ http://myName/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
pd. I'm trying to fix it on my local windows 8 machine, but if other OS is required, my remote server works on Ubuntu 12.04 LTS
Thanks you all.
Add this to your server block:
port_in_redirect off;
E.g.
server {
listen 80;
server_name localhost;
port_in_redirect off;
}
Documentation reference.
You should also change server_name to myName. server_name should be your domain name.
You should also be listening on port 80, and then use proxy_pass to redirect to whatever is listening on port 8000.
The finished result should look like this:
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 www.myweb.com;
location / {
proxy_pass http://localhost:8000/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Comments were removed for clarity.
Hiding the port during proxying needs these two lines in server body:
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
The conf is like:
server
{
listen 80;
server_name example.com;
server_name_in_redirect off;
proxy_set_header Host $host:$server_port;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
access_log off;
}

Setup of nginx with node.js

I have setup nginx as a front end to an node.js app.
My nginx conf is:
worker_processes 1;
error_log /tmp/logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /tmp/logs/access.log;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/sites-enabled/*;
# BELOW IS THE PART TO PROXY MY NODE.JS APP
upstream node_entry {
server unix:/tmp/express.sock
fail_timeout=0;
}
server {
listen 8888;
client_max_body_size 4G;
server_name localhost;
keepalive_timeout 5;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://node_entry;
}
}
}
My node.js app is:
express = require('express');
app = express.createServer();
app.get('/test', function(req, res){
res.send('TEST');
});
app.listen('/tmp/express.sock');
When I issue a:
curl -XGET 'http://localhost:8888/test'
I get an error instead of proxying to my node.js app.
Any idea ?
I'm doing something similar, but it's all on one host, and I'm using a predefined port number that nginx and node both know (though I'd rather use your way if you can get it working).
Does it work if you have node listen on a specific port, and proxy_pass to http://127.0.0.1:{that_port}? (assuming both are on the same server...)

Resources