Virtual Hosting

From AOLserver Wiki
Jump to navigation Jump to 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:

/home/vinod/web/
                backend1/
                         backend1.tcl
                         log/
                         www/
                             index.adp
                backend2/
                         backend2.tcl
                         log/
                         www/
                             index.adp
                frontend/
                         frontend.tcl
                         log/

And here are the files:

frontend.tcl

   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/nssock.so
   ns_section ns/module/nssock
   ns_param   port            80
   ns_param   hostname        myhostname.example.com
   ns_param   address         0.0.0.0
   ns_param   defaultserver   backend1
   # Map headers to server-name
   ns_section ns/module/nssock/servers
   ns_param   backend1        backend1.com
   ns_param   backend1        backend1.com:80
   ns_param   backend2        backend2.com
   ns_param   backend2        backend2.com:80
   ns_param   backend2        myotherdomain.com
   ns_param   backend2        myotherdomain.com:80
   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}/nspostgres.so
   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}/nsdb.so
   ns_param   nslog           ${bindir}/nslog.so
   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 0.0.0.0:80 -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       nssock.so
   ns_param nsvhr        nsvhr.so
   #
   # 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   "www.domain.com"        "unix://domain.com.nsunix"
   ns_param   "www.domain.com:80"     "unix://domain.com.nsunix"
   ns_param   "domain.com"            "unix://domain.com.nsunix"
   ns_param   "domain.com:80"         "unix://domain.com.nsunix"

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

   ns_section "ns/server/${servername}/module/nsunix"
   ns_param Hostname     www.domain.com
   ns_param Port         80
   ns_param Location     http://www.domain.com
   ns_param SocketFile   domain.com.nsunix
   ns_section "ns/server/${servername}/modules"
   ...
   ns_param nsunix       nsunix.so

Easy Virtual Hosting


Category Documentation