Over a million developers have joined DZone.

Add A Fake Host For Development Purposes

DZone's Guide to

Add A Fake Host For Development Purposes

· ·
Free Resource
I don't like all these "localhost:3001", "localhost:3001", etc. urls so I use vhosts in apache (that is a proxy balancer for a mongrel cluster on my machine. This script helps me to add required information for my fake domain in the /etc/hosts and apache configs


HOSTS_FILE = "/etc/hosts"
VHOSTS_FILE = "/Library/Apache2/conf/extra/httpd-vhosts.conf"

if $*.size < 2
  puts %{addhost.rb - creates a fake domain and bind it to the working rails app
USAGE: addhost development_hostname mongrel_port

Example: if you have a rails app on a localhost:3002 and you want
         to bind it to the myrailsapp.dev than run:
         addhost myrailsapp.dev 3002
  exit 1

dev_host = $*[0]
mongrel_port = $*[1]

# Adding to the hosts file (absolute path must be in the HOSTS_FILE; usually /etc/hosts)
unless IO.read(HOSTS_FILE) =~ /^\t#{dev_host}$/
  File.open(HOSTS_FILE, "a") do |file|
    file << "\n127.0.0.1\t#{dev_host}"

unless IO.read(VHOSTS_FILE) =~ /^\tServerName #{dev_host}$/
  File.open(VHOSTS_FILE, "a") do |file|
    file << %{

  ServerName #{dev_host}
  ProxyPass / http://#{dev_host}:#{mongrel_port}/
  ProxyPassReverse / http://#{dev_host}:#{mongrel_port}
  ProxyPreserveHost on


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}