Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do 301 redirects #1030

Open
patrickroberts opened this issue Jul 5, 2016 · 3 comments
Open

How to do 301 redirects #1030

patrickroberts opened this issue Jul 5, 2016 · 3 comments

Comments

@patrickroberts
Copy link

I want to reopen this issue since 301 redirects still don't seem to work. Here's some reproducible code:

index.js:

var http = require('http');
var express = require('express');
var httpProxy = require('http-proxy');

var app = express();
var proxy = httpProxy.createServer();

app.get('/', function (req, res) {
  res.send('<h1>Welcome</h1>');
});

proxy.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

var server = http.createServer(function (req, res) {
  console.log(req.url);

  if (req.headers['host'] === process.env.HEROKU_APP_NAME + '.herokuapp.com') {
    app(req, res);
  } else {
    proxy.web(req, res, {
      target: req.url,
      protocolRewrite: true
    });
  }
});

server.listen(process.env.PORT || 8000);

Here was the output:

$ node index
http://nodejs.org/
url.js:398
  if (protocol && protocol.substr(-1) !== ':') protocol += ':';
                           ^

TypeError: protocol.substr is not a function
    at Url.format (url.js:398:28)
    at Array.setRedirectHostRewrite (/Users/patrick/Servers/open-proxy/node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js:70:40)
    at ClientRequest.<anonymous> (/Users/patrick/Servers/open-proxy/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:157:20)
    at emitOne (events.js:77:13)
    at ClientRequest.emit (events.js:169:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:415:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)
    at Socket.socketOnData (_http_client.js:305:20)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)

For reference I'm using node 4.2.4, and http-proxy 1.14.0.

@dancingfrog
Copy link

dancingfrog commented Oct 18, 2016

@patrickroberts

I'm also trying to route to a 301 redirect with http-proxy and so far I'm only seeing reverse proxy behavior. Even when I look at your example, there's no parameter that would specify a 301 response. The client should be sent an endpoint url to make a new request against, similar to Express' res.redirect(301, redirectUrl); which has the returns the response headers:

HTTP/1.1 301 Moved Permanently
Location: <redirctUrl>

protocalRewrite: true has no effect on the action of theweb({}) method.

Also, can you please print the request + headers that you are using because I'm not able to reproduce the TypeError exception above.

Thanks,
John

@morkeleb
Copy link

morkeleb commented Feb 5, 2017

I'm hitting the same issue with 1.14.0, basically any redirect I hit the browser traffic leaves the proxy for the real site.

I'm using the following setup:

  var proxy = httpProxy.createProxyServer({
    target:options.url,
    autoRewrite: true,
    hostRewrite: true,
    changeOrigin: true
  }).listen(options.localProxy);

@apsavin
Copy link

apsavin commented Aug 18, 2017

I think #1195 should fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants