简体   繁体   中英

My rack-proxy app works with different ports, but not different URLs

I wrote a little Rack app the uses rack-proxy to make it look like apps that are running on different ports are actually different subdirectories of one domain. It works.

If I'm running notes on port 3001 and photos on 3002 , then I'm able to go to localhost:3000/notes or localhost:3000/photos and everything works properly.

That's only helpful for running it locally. But if I want to run this same setup on Heroku I need to be able to proxy to/from different URLs, not just different ports. So when I change my code from setting env["HTTP_HOST"] to http://localhost:3001 for /notes , to instead set env["HTTP_HOST"] to http://sbbme-note.herokuapp.com for /notes , Rack blows up on me.

I thought it would Just Work™. Unfortuntely, I'm missing something.

Errno::EADDRNOTAVAIL at /notes
Can't assign requested address - connect(2) for "http://sbbme-note.herokuapp.com" port 0

The full stack trace is in this gist.

https://gist.github.com/veganstraightedge/6d840377bf20b4b5f5db

The repo is homesteading/homesteading-router-rack . The master branch is the one working in local development mode only (multiple ports on localhost). The heroku-router branch is where I'm trying to use different (sub)domains instead of just different ports. heroku-router is the one that blows up (see gist above).

If you want to install this and try it on your own machine, this should work:

gem install homesteading
homesteading new mysite
cd mysite
homesteading server
open http://localhost:3000

I'm real close to being able to replace my years old pile of mono-rails with my constellation of apps approach in Homesteading. This router is the last main blocker. Any help would be greatly appreciated.

PS. My intial version is based on this blog post (livsey dot org/blog/2012/02/23/using-rack-proxy-to-serve-multiple-rails-apps-from-the-same-domain-and-port) and this Stack Overflow question/answer (stackoverflow dot com/questions/11057905/how-do-i-use-rackproxy-within-rails-to-proxy-requests-to-a-specific-path-to-an "How do I use Rack::Proxy within Rails to proxy requests to a specific path to another app - Stack Overflow").

Looking at the setup and the stack trace it looks like the proxy is atrying to connect to the sbbme-note app on port 0. Try adding

ENV['SERVER_PORT'] = '80'

You might also consider using the nginx build pack on heroku an implementing a forward proxy that way. I've used this build pack as a basic forward proxy and it has worked great. There are other nginx build packs that are designed to hook up to you application server (like unicorn) that might also be useful for this use.

The solution was a combination of Lukas' suggestion and fixing what I was originally doing.

I added this (as suggested by Lukas).

env['SERVER_PORT'] = 80

AND I removed the http:// from my env["HTTP_HOST"]

So, this:

env["HTTP_HOST"] = "http://#{app}"

became this:

env["HTTP_HOST"] = app

Thanks for your help, Lukas!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM