A simple webserver in go based on ideas from OpenBSD httpd.
- Simple configuration file using JSON, examples in
configfilesdirectory - Uses go's built-in
net/httpserver, supporting HTTP/1.1 and HTTP/2.0 - Optional request logging
- Each HTTP server has a configured list of locations that are applied exactly in configured order for each request. Location types:
- Static files and directories
- Pre-compressed files and directories
- FastCGI
- Blocked locations
- Redirect locations
- Configurable response header values at server and server-location levels.
- Drops privileges at startup using
chroot,setuid,setgid, andpledge.
- Install go if needed with
pkg_add go - Clone this git repo
make- buildgo-httpdexecutabledoas make install- Install executable to
/usr/local/sbin/go-httpd - Install rc.d script to
/etc/rc.d/gohttpd
- Install executable to
- Copy example config file to /etc:
doas cp configfiles/gohttpd.json /etc- Modify
/etc/gohttpd.jsonas needed
- Create directory for request logs if enabled
doas mkdir -p /var/www/gohttpd-logsdoas chown www:www /var/www/gohttpd-logs
- Enable and start daemon
doas rcctl enable gohttpddoas rcctl start gohttpd
-hshow usage and exit-f <config file path>override default config file path/etc/gohttpd.json-venable verbose logging
- Example config files in
configfilesdirectory - Config file sections
dropPrivileges- May be omitted to disable dropping privileges
- If
chrootEnabledis true callschrootat startup to change root directory tochrootDirectory - Calls
setgidat startup with gid forgroupName - Calls
setuidat startup with uid foruserName
requestLogger- May be omitted to disable request logging
- If
logToStdoutis true, write request logs to stdout. Useful for debugging. - Else write request logs to
requestLogFile(relative tochrootDirectory)
serverslist of server configurationsserverIDstring server id used for logging onlynetworkAndListenAddressListlist of addresses and ports to listen on.timeoutsread and write timeouts for server socketsresponseHeadersresponse header keys and values at server level.locationslist of location configurations. Applied in configured order when each request is processed.locationIDstring location id used for logging onlyhttpPathPrefixurl path prefix for matching location. If not specified defaults to""which matches any URL.responseHeadersresponse header keys and values at server-location level. Can be used to override server levelresponseHeaders.- Each
locationcontains one of the following location types: blockedLocation- Always return the specified
responseStatuswith no body
- Always return the specified
directoryLocation- Use go's
http.FileServerto serve files in the specifieddirectoryPath directoryPathis relative tochrootDirectorystripPrefixmay be specified to strip url prefix elements before file serving
- Use go's
compressedDirectoryLocation- Use
github.com/lpar/gzipped/v2to serve pre-compressed static files ending in.gzor.brbased onAccept-Encodingrequest header - Similar to
gzip-staticoption in OpenBSD httpd - Configuration fields are the same as
directoryLocation
- Use
fastCGILocation- Use
github.com/yookoala/gofastto connect to a fastcgi application using a unix socket atnetwork(unixortcp) andaddress. - Optionally specify a
connectionPoolblock containing asizeandlifetimeMilliseconds. Defaults to no connection pool if not specified.
- Use
redirectLocation- Send a redirect response using the specified
redirectURLandresponseStatus redirectURLmay contain variables$HTTP_HOSTand$REQUEST_PATH
- Send a redirect response using the specified