How to route all *.dev to subfolders on vagrant box

704 Views Asked by At

I want that every *.dev Host will be routed to my vagrant machine to /var/www/vhosts/*.dev/public, for example my local development environment project1.dev is located in /var/www/vhosts/project1.dev/public

So when I add a new (sub)project into my box, I do not need to change my config.yaml (Vagrant installed via puphpet.com) and reload the machine.

On my computer, I added the following to the hosts file in /private/etc:

192.168.56.101 *.dev

On my VM, I changed my 10-default_vhosts80.conf in /etc/apache2/sites-enabled to:

# ************************************
# Vhost template in module puppetlabs-apache
# Managed by Puppet
# ************************************

<VirtualHost *:80>
  ServerName default

  ## Vhost docroot
  DocumentRoot "/var/www/default"

  ## Directories, there should at least be a declaration for /var/www/default

  <Directory "/var/www/default">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Require all granted
  </Directory>

  ## Load additional static includes

  ## Logging
  ErrorLog "/var/log/apache2/default_vhost_80_error.log"
  ServerSignature Off
  CustomLog "/var/log/apache2/default_vhost_80_access.log" combined

  ## Custom fragment
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/default/$1
</VirtualHost>
<VirtualHost *.dev:80>
    ServerName dev
    VirtualDocumentRoot /var/www/vhosts/%0
</VirtualHost>

Unfortunately, this doesn't work. Any ideas? I am a beginner in this subject.

3

There are 3 best solutions below

0
On BEST ANSWER

In the end, I use dnsmasq to route all .localdev Domains to 127.0.0.1. Note that I am using .localdev instead of just .dev or .local as this seems to cause problem (OS X 10.10) because .dev is a proposed gTLD and .local is used by Apple's Bonjour.

Then I configured Apache by creating and enabling this site:

<VirtualHost *:80>
  ServerAlias localhost *.localdev #wildcard catch all
  VirtualDocumentRoot /hosts/%1/public
  UseCanonicalName Off
  <Directory "hosts">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

(from: http://brunodbo.ca/blog/2013/04/26/setting-up-wildcard-apache-virtual-host-wildcard-dns)

1
On

Unfortunately hosts files do not support using wildcards. You have to manually define each and every host to redirect.

Also, your hosts file is at /etc/hosts

0
On

I use a proxy auto configuration file. This works on Windows, MacOS and Linux. Easy, flexible and no additional software required. The following example routes all *.dev traffic to your vagrant box:

function FindProxyForURL(url, host) {
    if (dnsDomainIs(host, ".dev")) {
        return "PROXY 127.0.0.1:8080";
    }

    return 'DIRECT';
}

When needed, replace 127.0.0.1:8080 with the IP and webserver port of your vagrant box. Store this file somewhere. You can store it locally or let the webserver on your Vagrant-box host the file.

Windows: See here how to use the PAC file on Windows.

MacOS: See here how to use the PAC file on MacOS. You can link to the file using file:///Users/username/path/to/proxy.pac.

Linux: For linux it depends, but I'm sure linux users will be able to Google for their specific situation.