NaviServer Programmable Web Server

ns_serverrootproc(n)

NaviServer Built-in Commands – 5.1.0


[ Main Table Of Contents | Table Of Contents | Keyword Index ]

Name

ns_serverrootproc - Callback for Determining Server Root

Table Of Contents

Synopsis

Description

This command registers a Tcl callback that dynamically computes the server root directory. It is commonly used to determine the per-request "serverdir" based on attributes of the current connection (for example, using the Host: header field).

COMMANDS

ns_serverrootproc script ?arg ...?

Registers a script that returns the server root directory as an absolute or relative path. The result of this callback overrides the default value of the serverdir parameter. For more details, see Customizing File Locations.

If additional arguments are provided, they will be passed to the callback. Thus, the callback can be specified as either a (multi-line) script or as a command with arguments. (Note: When the callback is registered via a configuration parameter, the multi-argument invocation style is not available.)

CONFIGURATION

The server root can be computed dynamically by configuring a serverrootproc callback in the ns/server/$server section or by registering a callback with the ns_serverrootproc command.

In NaviServer 5 and newer, the preferred form is to define serverrootproc directly in the server configuration. This makes the callback available early during startup, which is important when the computed server root is used for path-related settings such as per-server log file locations.

Configuration File Registration

The following example computes the per-request server root from the Host header. If a directory matching the host name exists below the configured serverdir, that directory is used. Otherwise, the callback falls back to a directory named after the virtual server.

 ns_section ns/server/default {
    ns_param serverdir /var/www
 
    ns_param serverrootproc {
        set rootDir [ns_server serverdir]
        set host    [ns_conn host ""]
 
        if {$host ne "" && [file isdirectory $rootDir/$host]} {
            return $rootDir/$host
        }
 
        set serverDir $rootDir/[ns_info server]
        ns_log notice "... $rootDir/$host does not exist, use default '$serverDir'"
        return $serverDir
    }
 }

Runtime Registration

Before NaviServer 5, the server root callback had to be registered at runtime with the ns_serverrootproc command, typically from the server's Tcl initialization code. This form is still supported and may be familiar to users coming from NaviServer 4.99 configurations, but it is registered later during server startup and is therefore less suitable when early path computation is needed.

One way to register the callback during server initialization is to place the registration code in the server's Tcl initialization section.

 ns_section ns/server/default/tcl {
    ns_param initcmds {
        namespace eval ::default {
            proc serverroot {args} {
                set rootDir [ns_server serverdir]
                set host    [ns_conn host ""]
 
                if {$host ne "" && [file isdirectory $rootDir/$host]} {
                    return $rootDir/$host
                }
 
                set serverDir $rootDir/[ns_info server]
                ns_log notice "... $rootDir/$host does not exist, use default '$serverDir'"
                return $serverDir
            }
 
            ns_serverrootproc [namespace current]::serverroot
        }
    }
 }

For new configurations, prefer the serverrootproc parameter in ns/server/$server. Use runtime registration with ns_serverrootproc mainly for compatibility with older configurations or when the callback must be installed programmatically.

See Also

admin-config-params, ns_conn, ns_locationproc, ns_server, ns_set

Keywords

callback, global built-in, serverdir, virtual host