subdomains using http-proxy does not display node running app - node.js

I am following http://blog.nodejitsu.com/http-proxy-intro/ to write up my proxy server to point sub domains to node apps running on different port
var http = require('http'),
httpProxy = require('http-proxy');
//
// Just set up your options...
//
var options = {
hostnameOnly: true,
router: {
'localhost': '127.0.0.1:80'
'sub.localhost': '127.0.0.1:9012',
}
}
//
// ...and then pass them in when you create your proxy.
//
var proxyServer = httpProxy.createServer(options).listen(80)
When i run this file using node and try to access localhost or sub.localhost, I get this error back. I cant really understand whats going wrong.
Error: Must provide a proper URL as target
at ProxyServer.<anonymous> (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:68:35)
at Server.closure (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:125:43)
at emitTwo (events.js:87:13)
at Server.emit (events.js:172:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:525:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)

Try this:
var http = require("http");
var httpProxy = require("http-proxy");
// reverse proxy
var proxy = httpProxy.createProxyServer();
http.createServer(function (req, res) {
var target,
domain = req.headers.host,
host = domain.split(":")[0];
////////////////// change this as per your local setup
////////////////// (or create your own more fancy version! you can use regex, wildcards, whatever...)
if (host === "localhost") target = {host: "localhost", port: "2000"};
if (host === "testone.localhost") target = {host: "localhost", port: "3000"};
if (host === "api.testone.localhost") target = {host: "localhost", port: "4000"};
//////////////////
proxy.web(req, res, {
target: target
});
}).listen(8000);

Related

nodejs http-proxy not passing URL

I'm using http-proxy module and I'm trying to send the request to port 1234 and gets its reply back.
But in the apache logs I can see that request is only to /. The documentation says to use toProxy: true in order to pass the URL but it isn't working.
https://github.com/http-party/node-http-proxy
toProxy: true/false, passes the absolute URL as the path (useful for proxying to proxies)
Here is the code:
var http = require('http');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var fs = require('fs');
http.createServer(function(req, res) {
return proxy.web(req, res, { target: {host: 'localhost', port: 1234},
ssl: {
key: fs.readFileSync('/root/test.pem', 'utf8'),
cert: fs.readFileSync('/root/test_cert.pem', 'utf8')
},
toProxy: true
});
}).listen(3000);
I'm using curl to test and checking the apache logs which is listening on port 1234.
curl -v http://localhost:3000/getsomething.html
Figured it out. I had to include protocol.
return proxy.web(req, res, { target: { protocol: 'https:', host: 'localhost', port: 1234},

node http-proxy and HTTPS

I am having issues getting https working with node http-proxy.
I've created a server using node http-server
forever /usr/local/lib/node_modules/http-server/bin/http-server /home/blah/public_html/ -p 5000 -S -C /myencrypt/blah.com/cert.pem -K /myencrypt/blah.com/privkey.pem
If I go to https://blah.com:5000 the Certs are working correctly.
If I go to blah.com I get the following error
Error: unable to verify the first certificate
at TLSSocket.<anonymous> (_tls_wrap.js:1088:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:188:7)
at TLSSocket._finishInit (_tls_wrap.js:610:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38)
What am I missing here?
var fs = require('fs');
var http = require('http');
var https = require('https');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxy();
var options = {
'blah.com':{
target:'https://blah.com:5000',
ssl:{
key:fs.readFileSync('/myencrypt/blah.com/privkey.pem', 'utf8'),
cert:fs.readFileSync('/myencrypt/blah.com/cert.pem', 'utf8')
}
}
}
http.createServer(function(req, res) {
proxy.web(req, res, {
target: options[req.headers.host].target,
ssl : options[req.headers.host].ssl
});
}).listen(80);
I decided to solve my problem using redbird
var redbird = require('redbird')({
port: 80,
secure:false,
ssl: {
port:443,
key: "/myencrypt/blah.com/privkey.pem",
cert: "/myencrypt/blah.com/cert.pem",
}
});
redbird.register('blah.com', 'https://blah.com:5000', {
ssl: {
key: "/myencrypt/blah.com/privkey.pem",
cert: "/myencrypt/blah.com/cert.pem",
}
});

Using same port for IIS and Node JS

I have a web app built with PHP on IIS as backend and Node.JS socket.io for realtime communication, however I need to run both app (IIS and Node.JS) on a same port (443) to secure both connection, so im creating reverse proxy like this :
var httpProxy = require('http-proxy');
proxy = httpProxy.createProxyServer({}),
url = require('url');
http = require('http');
http.createServer(function(req, res) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
if(pathname == '/realtime_script')
proxy.web(req, res, { target: 'http://localhost:8000/socket.io/socket.io.js' });
else if(pathname.indexOf("/socket.io") > -1)
proxy.web(req, res, { target: 'http://localhost:8000'+req.url });
else
proxy.web(req, res, { target: 'http://localhost:8080' });
}).listen(80, function() {
console.log('proxy listening on port 9000');
});
I set socket.io to listen to port 8000 and IIS bind to port 8080, I managed to access home page using http://localhost, everything loads and connected perfectly (including socket.io), however every time a redirect happened (e.g submitting form or clicking a link), the node js crashed with this message :
C:\inetpub\wwwroot\node\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^Error: socket hang up
at createHangUpError (http.js:1472:15)
at Socket.socketCloseListener (http.js:1522:23)
at Socket.emit (events.js:95:17)
at TCP.close (net.js:465:12)
anybody can tell me what's wrong here?
Thank you

Include subdomain when proxying in node

I've setup a simple HTTPS server to handle the following to situations:
Requests to https://localhost:5000/ that have a matching file in my directory are served via connect.static(__dirname). This works great for everything like my index.html and my CSS files and is working exactly as I need.
Requests to https://localhost:5000/api should redirect to https://subdomain.mydomain.com:443/api.
The proxy is properly transferring everything over HTTPS and the SSL handshake part seems to be working exactly as I would expect. The problem is that my API uses the subdomain to determine what database to connect to and what data to return. So, my API sees the request
https://localhost:5000/api/something
instead of
https://subdomain.mydomain.com/api/something
and is throwing an error telling me I have to supply the subdomain.
How can I tell the node proxy to forward (or use) the domain/subdomain when doing the proxy?
Here is my code:
var fs = require('fs');
var connect = require('connect'),
https = require('https'),
httpProxy = require('http-proxy'),
options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
},
endpoint = {
host: 'subdomain.mydomain.com',
port: 443,
prefix: '/api',
target: { https: true }
};
var proxy = new httpProxy.RoutingProxy();
var app = connect()
.use(connect.logger('dev'))
.use(function(req, res, next) {
if (req.url.indexOf(endpoint.prefix) === 0) {
proxy.proxyRequest(req, res, endpoint);
} else {
next();
}
})
.use(connect.static(__dirname));
https.createServer(options, app).listen(5000);
console.log('Listening on port 5000');
Just in case someone bumps into this old question, you should use http-proxy's changeOrigin option.

Error in http-proxy module : Must provide a proper URL as target

I'm new in Node.JS and deployed the first application on VPS.
After running on port 8000, i decided create a http-proxy for forward each domain to its specific port .
I worte a little application like here :
var http = require('http'),
httpProxy = require('http-proxy');
var option = {
router : {
'domain.com' : 'http://88.198.86.100:8000'
}
};
var proxyServer = httpProxy.createServer(option);
proxyServer.listen(80);
88.198.86.100 is my server ip.
So, my problem here is shown , when i typed 88.198.86.100 in my browser PC (Google Chrome), my proxy application in server was carshed and gave this error :
C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^
Error: Must provide a proper URL as target
at ProxyServer.<anonymous> (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:68:35)
at Server.closure (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:125:43)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
error: Forever detected script exited with code: 1
I want to someone enter IP server into the each browser, my application will not crash.
//below code works for me
var httpProxy = require('http-proxy')
var proxy = httpProxy.createProxy();
var options = {
'foo.com': 'http://foo.com:8001',
'bar.com': 'http://bar.com:8002'
}
require('http').createServer(function(req, res) {
proxy.web(req, res, {
target: options[req.headers.host]
});
}).listen(80);

Resources