How to Resolve Stuck ‘localhost’?
Have you ever found yourself in a situation when
http://localhost:3000/ just won’t send requests to your development Rails server? I certainly have.
Here are a couple of steps to try out, before you start ripping your hairs out.
First, check that your Rails server is running:
ps ax | grep rails
Next, check that nothing else is listening on the same port as your development server:
sudo lsof -iTCP -sTCP:LISTEN -n -P
By default, local Ruby on Rails servers listen to the port
3000. If this port is taken on your machine, or you want to run multiple Rails servers at the same time, pass
-p <port> option with a port number you have available:
rails s -p 8080
Take into account that ports below 1024 are reserved for the system and require root access. So, for web server purposes, the most commonly used ports are between 1024 and 10000. Port 8080 is a good (and often used) choice because it is similar to the port 80, most commonly used for the HTTP requests.
If you are running a Rails server mapped to the domain name, you want to make sure
/etc/hosts file is setup correctly. First of all, if you have edited this file before, make sure you do not mess up any of the default values. Below is the default
/etc/hosts for OS X, which, among other things, contains localhost mappings for IPv4 and IPv6 protocols.
## ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
At the end of the file, add your dev domain mappings.
Different web servers such as Thin, Webrick or Puma bind to the localhost in a slightly different way, so it makes sense to specify both IPv4 and IPv6 mappings.
127.0.0.1 railsapp.dev ::1 railsapp.dev