views:

512

answers:

1

OK. I'm pretty much an apache HTTPD noob so please bare with me.

I have an issue with serving a large amount of VirtualHosts and I was wondering if there is a more efficient way of doing things.

I'm currently using the Location directive to serve a large amount projects from different departments, over 300 projects from 19 departments in total. The structures is the same for every Location directive except for the directories the files are served from. What I currently have is a large file that looks something like this:

<VirtualHost *> 
   ServerName www.myserver.com 

   <Location /departmentA/project1> 
      AuthType Basic 
      AuthName "By Invitation Only" 
      AuthUserFile /usr/local/departmentA/project1/passwords 
      Require valid-user 
   </Location> 

   <Location /departmentA/project2> 
      AuthType Basic 
      AuthName "By Invitation Only" 
      AuthUserFile /usr/local/departmentA/project2/passwords 
      Require valid-user 
   </Location> 

   <Location /departmentB/project1> 
      AuthType Basic 
      AuthName "By Invitation Only" 
      AuthUserFile /usr/local/departmentA/project1/passwords 
      Require valid-user 
   </Location> 
</VirtualHost>

As you can see all these structures are the same except for the paths. What I would like is of course something where I can use variables for the department and project names and just maintain a single Location directive. I assume that this is also less heavy on the server and memory.

<VirtualHost *> 
   ServerName www.myserver.com 

   <Location /$1/$2> 
      AuthType Basic 
      AuthName "By Invitation Only" 
      AuthUserFile /usr/local/$1/$2/passwords 
      Require valid-user 
   </Location> 
</VirtualHost>

Any help is very much appreciated.

+1  A: 

I don't know of any way to do that specifically, as I don't think AuthUserFile accepts a dynamic argument. It might be easiest to write a little script in your language of choice that takes a template for the <Location> block and repeats it for each directory you want to protect. Example, in Python:

#!/usr/bin/python
import sys
print '<VirtualHost *:80>'
print '   ServerName www.myserver.com'
for path in sys.stdin:
    print '   <Location /%s>' % path.strip()
    print '      AuthType Basic'
    print '      AuthName "By Invitation Only"'
    print '      AuthUserFile /usr/local/%s/passwords' % path.strip()
    print '      Require valid-user'
    print '   </Location>'
print '</VirtualHost>'

For each line of the form 'departmentA/project1' it reads on standard input, it prints out the corresponding <Location> section.

David Zaslavsky
Hi. I understand what you're saying. However, what I don't quite get is how to integrate this. Do you mean that Apache can call this in real-time or do you mean that I have to generate every Location upfront?
Luke
No, Apache cannot do this automatically. You need to run it to regenerate the Apache configuration every time you want to change the set of protected locations (and then you need to reload Apache). I didn't think it wasn't the kind of thing you would need to do frequently.
David Zaslavsky