Virtual Hosting

From AOLserver Wiki
Jump to: navigation, search

Virtual hosting is built in to AOLserver 4. (See below for version 3.x recipes)

Say you want to run 2 sites from one IP address. You'll need 3 config files - frontend.tcl, backend1.tcl and backend2.tcl. The frontend will run on port 80 and proxy requests to the backend servers depending on the hostname in the Header. You'll actually only run 1 AOLserver process. When you start nsd, you'll feed it frontend.tcl. The first thing frontend.tcl does is to read backend1.tcl and backend2.tcl. It sets up those virtual servers and then sets some global parameters.

Here's how I set up my directory structure:


And here are the files:


   ns_log notice "Starting to read frontend config file..."
   source /home/vinod/web/backend1/backend1.tcl
   source /home/vinod/web/backend2/backend2.tcl
   ns_log notice "Done loading backend server configs..."
   # Load nssock globally
   ns_section ns/modules
   ns_param   nssock          /usr/local/aolserver/bin/
   ns_section ns/module/nssock
   ns_param   port            80
   ns_param   hostname
   ns_param   address
   ns_param   defaultserver   backend1
   # Map headers to server-name
   ns_section ns/module/nssock/servers
   ns_param   backend1
   ns_param   backend1
   ns_param   backend2
   ns_param   backend2
   ns_param   backend2
   ns_param   backend2
   ns_section ns/servers
   ns_param   backend1        "My First Example Site"
   ns_param   backend2        "My Second Example Site"
   # Global server parameters
   ns_section ns/parameters
   ns_param   serverlog        /home/vinod/web/frontend/log/error.log
   ns_param   home             /usr/local/aolserver
   ns_section ns/threads
   ns_param   stacksize        expr 128*1024 ;# Per-thread stack size.
   ns_section ns/mimetypes
   ns_param   default          "*/*"     ;# MIME type for unknown extension.
   ns_param   noextension      "*/*"     ;# MIME type for missing extension.
   ns_log notice "nsd.tcl: finished reading frontend config file."

backend1.tcl and backend2.tcl (The only difference is the first line)

   set server                  "backend1" ;# or "backend2" for backend2.tcl
   ns_log notice "Starting to read $server config file..."
   set db_name                 $server
   set serverroot              "/home/vinod/web/${server}"
   set bindir                  [[file dirname [ns_info nsd]]]
   set pageroot                ${serverroot}/www
   # Tcl Configuration
   ns_section ns/server/${server}/tcl
   ns_param   library          ${serverroot}/tcl
   # Server parameters
   ns_section ns/server/${server}
   ns_param   directoryfile      index.tcl,index.adp,index.html,index.htm
   ns_param   pageroot           $pageroot
   # ADP (AOLserver Dynamic Page) configuration
   ns_section ns/server/${server}/adp
   ns_param   map           /*.adp    ;# Extensions to parse as ADP's
   ns_param   defaultparser fancy
   ns_section ns/server/${server}/adp/parsers
   ns_param   fancy    ".adp"
   # Database drivers
   ns_section "ns/db/drivers"
   ns_param   postgres        ${bindir}/
   ns_section ns/db/pools
   ns_param   ${server}pool1      "$server Pool 1"
   ns_param   ${server}pool2      "$server Pool 2"
   ns_section ns/db/pool/${server}pool1
   ns_param   driver             postgres
   ns_param   datasource         localhost::${db_name}
   ns_param   user               vinod
   ns_section ns/db/pool/${server}pool2
   ns_param   driver             postgres
   ns_param   datasource         localhost::${db_name}
   ns_param   user               vinod
   ns_section ns/server/${server}/db
   ns_param   pools              "${server}pool1,${server}pool2"
   ns_param   defaultpool        ${server}pool1
   # Access log -- nslog
   ns_section ns/server/${server}/module/nslog
   ns_param   file                 ${serverroot}/log/access.log
   # Modules to load
   ns_section ns/server/${server}/modules
   ns_param   nsdb            ${bindir}/
   ns_param   nslog           ${bindir}/
   ns_log notice "Finished reading $server config file."

This sets up 2 pretty plain virtual hosts. Once you get this setup working, you can configure backend1.tcl and backend2.tcl to your heart's desire.

Note that you can map multiple Headers to 1 server. It's also important to note that the server's error log is specified in ns/parameters, so you can't specifiy a separate error.log for each backend server.

Start your server:

   root:/home/vinod/web/frontend# /usr/local/aolserver/bin/nsd -t frontend.tcl -b -u nobody -g web

Hope this helps - Vinod Kurup 2005-07-21

AOLserver 3.x

The following three patches are required to get nsvhr/nssock and nsvhr/nsunix working under AOLserver 3.5.x:

NOTE: As of 2002-11-23, the changes to the core are no longer necessary and the changes to nsunix and nsvhr have been committed to CVS. Look for tag nsunix3_5 and nsvhr3_5.

In the nsvhr server's nsd.tcl:

   ns_section "ns/server/${servername}/modules"
   ns_param nssock
   ns_param nsvhr
   # Virtual Hosting redirector -- nsvhr
   ns_section "ns/server/${servername}/module/nsvhr"
   ns_param   Method          "GET"      ;# Methods allowed to proxy
   ns_param   Method          "POST"     ;# Methods allowed to proxy (can have >1)
   ns_param   Method          "HEAD"     ;# Methods allowed to proxy (can have >1)
   ns_param   Timeout         60         ;# Timeout waiting for back-end
   # Register hosts to proxy
   ns_section "ns/server/${servername}/module/nsvhr/maps"
   ns_param   ""        "unix://"
   ns_param   ""     "unix://"
   ns_param   ""            "unix://"
   ns_param   ""         "unix://"

In the nssock or nsunix virtual host's nsd.tcl:

   ns_section "ns/server/${servername}/module/nsunix"
   ns_param Hostname
   ns_param Port         80
   ns_param Location
   ns_param SocketFile
   ns_section "ns/server/${servername}/modules"
   ns_param nsunix

Easy Virtual Hosting

Category Documentation