Is it possible to use the node-http-prox module and proxy.proxyRequest to a https server?
I tried to do the following but doesnt seem to work.
app.get('/c/users/moreuser', function(req, res) {
proxy.proxyRequest(req, res, {
host: 'api.example.com',
port: 80,
https: true
});
});
doing this i dont get any response from the server. But i can get response directly from the server.
try port: 443, which is the default port for HTTPS.
Related
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},
I'm trying to create my own proxy server in NodeJs with the library node-http-proxy the problem is that I want to create a http proxy server that will have the possibility to forward the request to a https target (ex. https://google.com).
I know it's possible, because there is already a library that does this called: proxy-chain
When I try to request a https target with node-http-proxy I get an error: ERR_EMPTY_RESPONSE.
It also states in the documentation that I need to add a PKCS12 client certificate, but the proxy-chain does not do this.
Is there a way I can replicate the proxy-chain library with the node-http-proxy library?
This is my code at the moment:
var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, {
target: {
hostname: 'github.com',
port: 433,
protocol: 'https',
},
secure: false,
changeOrigin: true
});
});
console.log("listening on port 5050")
server.listen(5050);
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.
Going nuts on..
This
var app = express(),
proxy = new httpProxy.HttpProxy({
target: {
host: 'host.com',
port: 5280 // Port of XMPP server
}
});
..
// Proxy BOSH request to XMPP server
app.all('/http-bind', function(req, res) {
util.puts('Request successfully proxied: ' + req.url);
util.puts(JSON.stringify(req.headers, true, 2));
proxy.proxyRequest(req, res);
});
results in
body rid="3965133021" xmlns="http://jabber.org/protocol/httpbind" to="host.com#localhost" .. etc
host.com#localhost? that localhost has to go but cannot get it off ;9
How can that be?! it should go to host.com instead! spend an hour looking trying, anyone knows whats wrong?
This was a local issue with strophe framework. its fixed now.
I want to rewrite http://www.xyz.com/abc to http://xyz.com/abc.
How can that be done in Node.js.
I'm using node-http-proxy on port 80 and forward requests to Express on port 3000.
Thanks in advance.
Return a 301 with appropriate location headers if request hostname == www.xyz.com
The browser will do the rest.
You can do it on either side of the proxy. But i'd assume it makes sense to do something like this:
http.createServer(function (req, res) {
//
// Put your custom server logic here, then proxy
//
if (req.headers.host == 'www.xyz.com') {
res.writeHead(301, {'Location': 'xyz.com/' + req.url});
res.end();
}
else {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
}
}).listen(80);