Access Apache virtualhosts from LAN without using port numbers

apache-http-serverlanlocalhostvirtual-host

As far as I can tell, there are two ways to access an Apache virtualhost on a server without a static IP address (i.e. my laptop):

  • Set the host name to foo.localhost in its Apache config file and assign fo.localhost to 127.0.0.1 in /etc/hosts. This makes them memorable but doesn't support clients on the LAN.

  • Listen to different ports. This supports clients on the LAN but makes them hard to remember.

Is there any way to host all virtualhosts on port 80 while making them accessible to other devices? Possibly a reverse proxy that routes 127.0.0.1:80/foo/theactualurl to 127.0.0.1:42908/theactualurl, 127.0.0.1:80/bar/anotherurl to 127.0.0.1:39539/anotherurl?

I'm running Ubuntu 12.04.

Best Answer

I'm a beginner myself, but this came to mind... Can't hurt to try :)

Just set the ServerName directive to foo.localhost and use the ProxyPass to direct it to a LAN address.

Something like:

<VirtualHost *:80>
   ServerName foo.localhost
   DocumentRoot /var/www/foo  #doesn't matter because we're not going to hit it
   <Location />
      ProxyPass http://192.168.x.x/
      ProxyPassReverse http://192.168.x.x/
   </Location>
</VirtualHost>

Then your other internal site is accessed like:

<VirtualHost *:80>
   ServerName foo2.localhost
   DocumentRoot /var/www/foo2  #again, doesn't matter
   <Location />
      ProxyPass http://192.168.x.y/
      ProxyPassReverse http://192.168.x.y/
   </Location>
</VirtualHost>

I'll admit, I don't know if that'll work, but it seems reasonable to me ;)

Also, you didn't mention how many computers were on the LAN, but if it's only a few, maybe an edit to the /hosts file would be your solution.

Related Question