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

Support location (path) proxy routing #80

Open
dlee opened this issue Jul 8, 2015 · 3 comments
Open

Support location (path) proxy routing #80

dlee opened this issue Jul 8, 2015 · 3 comments

Comments

@dlee
Copy link

dlee commented Jul 8, 2015

I develop on a complex web app with many services, and most of these services are proxy routed via path (like nginx's location) rather than subdomain.

Would it be possible for katon to route different paths on a single domain to different upstream services?

@typicode
Copy link
Owner

typicode commented Jul 9, 2015

Can you provide an example please? Not sure to understand.

@dlee
Copy link
Author

dlee commented Jul 9, 2015

For example, in nginx, you can proxy requests to the same domain to different upstream servers depending on URL path:

    location / {

      location ~ /api {
        proxy_pass $api_server;
      }

      location ~ ^/assets {
        proxy_pass $assets_server;
      }

      proxy_pass $app_server;
    }

Requests for /api/xyz will be proxied to $api_server, requests for /assets/pic.jpg will be proxied to $assets_server, and all other requests will be proxied to $app_server.

@typicode
Copy link
Owner

Thanks for the example. Unfortunately, katon doesn't support that.

But creating a simple proxy server could do the trick. I've not tested the code but it should give you the overall idea.

// proxy.js
var http = require('http')
var httpProxy = require('http-proxy')
var proxy = httpProxy.createProxyServer()

http.createServer(function(req, res) {
  // Extract id from /:id/*
  var arr = req.url.split('/')
  arr.shift()
  var id = arr.shift()

  // Reconstruct URL
  req.url = arr.join('/')

  // Proxy to http://:id.ka
  proxy.web(req, res, { target: 'http://' + id + '.ka'  })
}).listen(process.env.PORT)
~/proxy$ katon add 'nodemon proxy.js' --name my-project
~/api$ katon add 'cmd'

Requests to http://my-project.ka/api should then be proxied http://api.ka

For more information, see https://github.com/nodejitsu/node-http-proxy

Tell me if it doesn't work.

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

2 participants