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

Cannot change path in target site? #758

Closed
yleemj opened this issue Dec 22, 2014 · 8 comments
Closed

Cannot change path in target site? #758

yleemj opened this issue Dec 22, 2014 · 8 comments

Comments

@yleemj
Copy link

yleemj commented Dec 22, 2014

If the income request url is https://example.com/abc, and then we call

proxy.web(req, res, {target: "https://target.com:8080"})

the request will be proxied to https://target.com:8080/abc, can it be proxied to a different path, e..g https://target.com:8080/something_else/xxxx, or just https://target.com:8080?
Thanks!

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 22, 2014

@yleemj use the prependPath and set it to false. Then just provide the path you want to proxy to in the target URL.

@jcrugzz jcrugzz closed this as completed Dec 22, 2014
@yleemj
Copy link
Author

yleemj commented Dec 23, 2014

After I set prependPath to false, the problem still exists. Here is my code:

var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer( {
    prependPath: false
});

function doProxy(req, res, target) {
    proxy.web(req, res, {
        target: target
   });

I'm using expressjs. The req.originalUrl is '/user/A', req.baseUrl is '/user', and req.url is '/A'. the request is proxied to https://target.com/A, which doesn't exist. What I really want is https://target.com/. But if I set req.url to '/' manually, the path for the the static files in target server will be changed to '/user/'. E..g, https://target.com/static/app.js will be changed to https://originalSite.com/user/static/app.js. It should be https://originalSite.com/user/A/static/app.js, and then proxied to the target server.

@jcrugzz
Copy link
Contributor

jcrugzz commented Dec 23, 2014

@yleemj Just for posterity, req.originalUrl and req.baseUrl have zero impact on how the request is proxied. http-proxy only cares about the req.url, the options you give it and the target. Now I'm still unsure why manually setting the req.url to / changes other behavior but I have provided an ignorePath option which will ignore the path that is given by the incoming request. Will this solve your use case? Your latest description honestly made your use case less clear.

@jcrugzz jcrugzz reopened this Dec 23, 2014
@ferozed
Copy link

ferozed commented Apr 11, 2015

I have the same usecase. I want to run a proxy that forwards to a remote server, but changes the target path:

So for eg:

https://proxy.com/foo -> https://target.com/bar

I have tried ignorePath=true, and prependPath=false and none of them seem to work.

Here are my results:

prependPath: false, ignorePath: true -> https://target.com/foo
prependPath: true, ignorePath: true -> https://target.com/bar/foo
prependPath: true, ignorePath: false -> https://target.com/bar/foo
prependPath: false, ignorePath: false -> https://target.com/foo

How do I get it to forward to just the target Url I specify:

        // You can define here your custom logic to handle the request
        // and then proxy the request.
        proxy.web(req, res, { target: forwardUrl });

Essentially, I am trying to write a general purpose forwarding proxy, that will use the path of the incoming request to forward to any number of target applications.

@mereghost
Copy link

I have the same use case and issue right now. =/

The only way I got this working was to reassign req.url to the path I wanted.

@pwasem
Copy link

pwasem commented Apr 18, 2015

How and when did you reassign req.url? Could you post some code snippet please? Thank you!

@jcrugzz
Copy link
Contributor

jcrugzz commented Apr 19, 2015

ignorePath is currently an unmerged PR. @ferozed did you use that branch? Let me merge the pull-request and bump the version and see if this fixes the issue you guys are having.

@jcrugzz
Copy link
Contributor

jcrugzz commented Apr 20, 2015

please try 1.11.0 with ignorePath option. Otherwise open a new issue as this should be fixed.

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

5 participants