Rails: explicitly tell rails the port it's running on - ruby-on-rails

I've got the following problem. I want to explicitly tell rails the port it is running on, so that the _url helper will use that port instead of the autodiscovered port.
The underlying problem is, that I have thin serving my rails app and apache serving static content. This works fine as long as the website is accessed on port 80 (apache) but not when accessed on the port thin is running on. (Which is the port that get's auto discovered.)

Rails is supposed to create relative links, which do not include the fully qualified domain name, see example here, I quote :
link_to "Profile", profile_path(#profile)
# => Profile;
the *_url shortcuts, on the other hand, are using the full path, which is bad when you're behind a reverse proxy (that's apache in your case).
Have you tried using only *_path for links, and *_url for redirects, as, in the case of a redirect, apache should rewrite the url to match its port (assuming you're using the ProxyPass and ProxyPassReverse directives at Apache level) ?
If you use url_for, you can force it to be relative by setting the :only_path to true.
If your apache configuration is not based on ProxyPass, could you please copy/paste the interesting part so I can fix this answer ?
Thanks in advance.

Because Ruby on Rails uses its own server, users visiting your website (and subsequently your Ruby on Rails application) will need to be redirected to the appropriate port.
To change the port of Ruby on Rails server you should start the server with the -p option:
ruby s -p <port-number>

Related

Adding Subdomain for my ruby on rails server, how to do?

i've got some problems with my ruby on rails server.
It is running under localhost:3333 in my debian vm under a windows8 host.
I've installed apache2 and passengermodul for apache to get ruby. And then I've installed rails.
Now I need a subdomain wich calls the ruby on rails server.
for example admin.localhost:3333
Is something like that possible? And when how can I configurate it?
You can use the lvh.me domain. That domain has a DNS entry that will redirect to localhost. This also works for subdomains, so you can visit admin.lvh.me:3000 and it will redirect to localhost:3000 while still having the subdomain available in the Rails request.
The advantage is that you don't have to edit your localhost file.
Add custom hosts with subdomains to the hosts file, follow these steps
In your terminal, open hosts file
cd /etc
sudo nano hosts
Add the host as mentioned in the following lines to the hosts file, you can add as many as you want
127.0.0.1 admin.localhost
127.0.0.1 subdomain.localhost
Save the file, CTRL + X then press Y
Done.
To run with a custom port, specify the port number while starting the server,
rails s -p 3333
Now you can run your application with, admin.localhost:3333
Hope this helps!

What is localhost:8000 as used in Codecademy (for AngularJS and Ruby on Rails tutorials)?

I want to know what is this: localhost:8000, found in Codecademy tutorials for AngularJS and Ruby on Rails. I even installed Apache 2, but to work with it I need to dial: http://localhost/. While working on some html files, I often come across Firefox's Inspect Element where a section is to mention localhost and its number like this: localhost:8000. I want to know what's this and can I use it to access my host from my android device or some other PC as we do access Codecademy's localhost to learn AngularJS and Ruby on Rails. Pls help. Thanks in advance. :-)
Localhost is the loopback-address of your pc. The IP-address behind it is 127.0.0.1. With localhost, it is possible to simulate a web-server environment and it is mostly used to simulate running web-applications as if they are running on a webserver. :8000 stands for the port-number on which the browser connects to the server. This is because the application runs (in this case) on port 8000 of the server. So it is not enough to just install Apache 2 and surf to http://localhost/ you have to configure Apache so that it runs your web-application on the desired port. The port-number itself has no special meaning. The different ports are just a part of the url so the browser knows on which port it has to connect. Some protocols use default ports. (e.g. HTTP will always connect to port 80, unless your specify another port in your webbrowser)
I'm sure a lot of people can explain it much better, but here is a begin.
More info about running ruby on rails on an Apache webserver:
How can i run a ruby on rails project on apache server?
How to setup Ruby on Rails Hosting using Apache, from Development to Production
EDIT: Technically, the whole 127.0.0.0/8 address block is reserved for loopback purposes. The default one, configurged in hosts.txt is 127.0.0.1 and the most famous.

Port Redirection To Domain Issue

I am using Cent OS 5.8 with kloxo.
I have a ruby application. When i start the application , by default it starts at port nos 3000. So to access dat i need to put domain.com:3000 .
This doesn't look good , so I decided to run mongrel at port nos 80.
now i am able to access domain.com
But now i want that the application shall run in a sub domain and there should be a different website running in my main domain.
I tried putting code in httpd.conf and it didn't work.
Can any one help me on this.
Thank You
You can use nginx as a proxy to accomplish this. You can then run your apps on ports 3000, 3001 and 3002 (for example) and let nginx route the traffic based on the hostname requested. But I recommend you looking into Phusion Passenger for production environments because it enables you to run applications like you're used to in an Apache or nginx environment. According to Phusion, it also outperforms Mongrel, but your mileage may vary of course.

Redirect example.com to rails server running on localhost:3000 leaving apache sites intact?

I am working on a rails app which runs on localhost:3000. I want example.com to point to localhost:3000. I edited the /etc/hosts file but it doesn't take port number so the request goes to apache. I want apache sites to work as they are but redirect example.com to rails server.
Please suggest the best way to achieve this.
Can you just specify example.com:3000 when browsing to your site? If not, I'd suggest using Phusion Passenger so that Apache can continue to respond on port 80 while also running your app.
Use mod_proxy?
ProxyPass /your_rails_app http://your.server.address:3000
ProxyPassReverse /your_rails_app http://your.server.address:3000
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Add a reverse proxy to heroku

I have a rails app running on heroku at, e.g myapp.herokuapp.com.
Now I want to reverse proxy from myapp.heroku.com/proxy/ to somewhereelse.com/ (i.e: myapp.heroku.com/proxy/stuff is reverse proxifed to somewhereelse.com/stuff)
Is that possible on Heroku? How to achieve this?
For anyone coming to this question through a search, this can be done.
Check out https://github.com/ryandotsmith/nginx-buildpack to vendor nginx into your heroku instance. This will place nginx in front of your rails app, and allow you to reverse proxy requests on this domain, having your heroku app configured as apex and allowing somewhereelse.com/stuff go elsewhere.
You dont have access to frontend routing infrastructure so its not possible to do add something like nginx location based reverse proxying or apache's modproxy. From my understanding too you can only bind to one port (the $PORT) within the dyno so its not possible to shadow your Rails app with your own vendored version of nginx (unless it is possible to communicate over a non TCP/IP socket between nginx and your rack/rails app, if this is the case then perhaps you can get rack to listen to /tmp/mysocket.git and nginx to reverse proxy on this, this could be a no starter though, Im just throwing out ideas).
Which means the only probable option if you have to handle this yourself in your rails app, I have only a tiny tiny bit of rails/ruby experience, but if no proxy functionality exists in rails then you perhaps you can explicitly accept the route and then use a http client to invoke the other parts.

Resources