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

Rewrite of "location" header #59

Open
pre-martin opened this issue Apr 6, 2016 · 8 comments
Open

Rewrite of "location" header #59

pre-martin opened this issue Apr 6, 2016 · 8 comments

Comments

@pre-martin
Copy link

Hi,

it would be great if this module could rewrite the "location" header of a http response. I think that the matching Apache httpd option would be "ProxyPassReverse".

Regards,
Martin

@tinganho
Copy link
Owner

tinganho commented Apr 8, 2016

@planetrenner-martin Can you just specify a use case?

@pre-martin
Copy link
Author

Hi,

we have a client in Javascript, which calls a REST API on a backend server. connect-modrewrite acts as a proxy to this backend server. During authentication, the backend server answers with a "HTTP redirect" ("location" header in the HTTP response). Because modrewrite is not acting as a reverse proxy, the client suddenly sees the IP address of the backend server in the "location" header. This breaks the authentication process, because now the client tries to communicate directly with the backend server (and thus bypassing modrewrite).

@tinganho
Copy link
Owner

tinganho commented Apr 9, 2016

OK I see seems legit. I will try to look into it in the near future.

@rbperegrino
Copy link

I had the same problem here.
In apache i use this parameters:

Header edit* Location http://${page_address}/(.*) http://${proxy_address}/$1

@tinganho
Copy link
Owner

tinganho commented May 3, 2016

@rbperegrino @planetrenner-martin any suggestion of how the API should look like?

@pre-martin
Copy link
Author

Hi,

good question. I am only an ordinary user and have no in-depth knowledge.

The reverse proxy makes only sense for request patterns with the "proxy" [P] flag set. But to keep the API clean, I would introduce a second and optional list of tuples, like

modRewrite([
    '^/test(.*)$ http://backend.example.org/$1 [P]'
], [
    '/test http://backend.example.org/'
])

(I would use the same syntax as the Apache directive ProxyPassReverse).

In this example, if a request was made to http://example.org/test/something modRewrite should rewrite the "Location" header in the response from the backend Location: http://backend.example.org/some-new-target to the new header Location: http://example.org/test/some-new-target.

@tinganho
Copy link
Owner

tinganho commented May 4, 2016

I think it is probably easier with:

modRewrite([
    '^/test/(.*)$ http://backend.example.org/$1 [PR]'
])

And let modrewrite magically figure out how it should rewrite the location header.

@daroay
Copy link

daroay commented Jun 8, 2017

No no no
Dont use modrewrite as a reverse proxy.
It is not even possible in apache (for that you will have to use the reverse proxy directive)

Actually, in apache, it discourages you to even use mod_rewrite as a regular proxy.

Do you want to reverse proxy, and still rewrite rules? Do it with a proxy!

Here is how to configure a reverse proxy with connect-modrewrite and proxy-middleware

var connect = require('connect');
var modRewrite = require('connect-modrewrite');
var proxy = require('proxy-middleware');
var url = require('url');


var app = connect()
  .use(modRewrite([
    "^\/api\/(.*)        /send-to-api/api/$1    [L]",
    "^(.*)\/css\/(.*)    /send-to-ui/css/$2     [L]",
    "^(.*)\/js\/(.*)     /send-to-ui/js/$2      [L]",
    "^(.*)\/images\/(.*) /send-to-ui/images/$2  [L]",
    "^(.*)\/fonts\/(.*)  /send-to-ui/fonts/$2   [L]",
    "^(.*)               /send-to-ui/index.html [L]"
  ]))
  .use('/send-to-api', proxy(url.parse('http://api.server.dev/'))) // Don't forget the last backslash
  .use('/send-to-ui',  proxy(url.parse('http://ui.server.dev/' ))) // Don't forget the last backslash
  .listen(9000)

Check that I use [L] flag because I want it to rewrite and skip the rest of the rules.

In this case, only the /api urls get proxied to api.server.dev, the rest goes to ui.server.dev.
The url prefixes /send-to-api and /send-to-ui are temporary and I use them to differentiate what will go where, it is removed by the connect before sent to their respective servers.

And yes, in case of redirects, proxy-middleware will change the Location header to be localhost:9000

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

No branches or pull requests

4 participants