modulepackage
0.4.1
Repository: https://github.com/aaronriekenberg/go-httpd.git
Documentation: pkg.go.dev
# README
go-httpd
A simple webserver in go based on ideas from OpenBSD httpd.
Features
- Simple configuration using JSON
- See
configfiles
directory for example working configurations.
- See
- Uses go's built-in
net/http
server- Supports HTTP/1.1 and HTTP/2.0
- Multiple servers can be configured with optional TLS.
- Easy to use with acme-client, see
configfiles/gohttpd.json
example. - Automatic thread creation by go, each request is run in its own goroutine.
- Optional request logging
- Uses
CombinedLoggingHandler
fromgithub.com/gorilla/handlers
- Uses
gopkg.in/natefinch/lumberjack.v2
to write rotate reuqest log files when they reach a configured size. - File I/O for request logging is asynchronous using a go channel.
- Uses
- Each HTTP server has a configured list of locations that are applied exactly in configured order for each request.
- Configurable response header values at server and server-location levels.
- Blocked locations and HTTP redirect locations.
- Static file and directory servers using standard go
http.FileServer
. - Pre-compressed file serving using
github.com/lpar/gzipped/v2
- Supports brotli and gzip files based on
Accept-Encoding
request header
- Supports brotli and gzip files based on
- Supports FastCGI with unix sockets using
github.com/yookoala/gofast
- Drops privileges at startup and uses
pledge()
. Roughly the following happens at startup:- go-httpd daemon is started as root
- Read configuration file and TLS certificates as root
- Create and bind server sockets (
net.Listener
) as root, allowing use of privileged ports 80 and 443. - Call
chroot
to change root to/var/www
or other configured directory - Call
setuid
andsetgid
to change to unprivigedwww
user/group or other configured user/group - Call
pledge
to limit system calls tostdio rpath wpath cpath inet unix
. - Create request logger if configured.
- Create request handlers and start the HTTP servers.
- A noop wrapper for pledge is provided so the app builds and runs on non-OpenBSD OS.
Usage on OpenBSD
- Install go if needed with
pkg_add go
- Clone this git repo
make
- buildgo-httpd
executabledoas 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.json
as needed
- Create directory for request logs if enabled
doas mkdir -p /var/www/gohttpd-logs
doas chown www:www /var/www/gohttpd-logs
- Enable and start daemon
doas rcctl enable gohttpd
doas rcctl start gohttpd
Command Line Arguments
-h
show usage and exit-f <config file path>
override default config file path/etc/gohttpd.json
-v
enable verbose logging
Configuration File
- Example config files in
configfiles
directory - Config file sections
dropPrivileges
- May be omitted to disable dropping privileges
- If
chrootEnabled
is true callschroot
at startup to change root directory tochrootDirectory
- Calls
setgid
at startup with gid forgroupName
- Calls
setuid
at startup with uid foruserName
requestLogger
- May be omitted to disable request logging
- If
logToStdout
is true, write request logs to stdout. Useful for debugging. - Else write request logs to
requestLogFile
(relative tochrootDirectory
)
servers
list of server configurationsserverID
string server id used for logging onlynetworkAndListenAddressList
list of addresses and ports to listen on.timeouts
read and write timeouts for server socketsresponseHeaders
response header keys and values at server level.locations
list of location configurations. Applied in configured order when each request is processed.locationID
string location id used for logging onlyhttpPathPrefix
url path prefix for matching location. If not specified defaults to""
which matches any URL.responseHeaders
response header keys and values at server-location level. Can be used to override server levelresponseHeaders
.- Each
location
contains one of the following location types: blockedLocation
- Always return the specified
responseStatus
with no body
- Always return the specified
directoryLocation
- Use go's
http.FileServer
to serve files in the specifieddirectoryPath
directoryPath
is relative tochrootDirectory
stripPrefix
may be specified to strip url prefix elements before file serving
- Use go's
compressedDirectoryLocation
- Use
github.com/lpar/gzipped/v2
to serve pre-compressed static files ending in.gz
or.br
based onAccept-Encoding
request header - Similar to
gzip-static
option in OpenBSD httpd - Configuration fields are the same as
directoryLocation
- Use
fastCGILocation
- Use
github.com/yookoala/gofast
to connect to a fastcgi application using a unix socket atnetwork
(unix
ortcp
) andaddress
. - Optionally specify a
connectionPool
block containing asize
andlifetimeMilliseconds
. Defaults to no connection pool if not specified.
- Use
redirectLocation
- Send a redirect response using the specified
redirectURL
andresponseStatus
redirectURL
may contain variables$HTTP_HOST
and$REQUEST_PATH
- Send a redirect response using the specified
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author