The following is a minimal configuration built into fasthttpd.
host: localhostlisten: ':8080'root: ./publiclog:output: stderrhandlers:'static':type: fsindexNames: [index.html]routes:- path: /handler: static
The following is a configuration that uses most of the current FastHttpd features.
host: localhost# NOTE: Define listen addr. It is supported ipv6 `[::1]:8080`listen: ':8080'root: ./public# Define fasthttp.Server settings.server:name: fasthttpdreadBufferSize: 4096writeBufferSize: 4096log:output: logs/error.log# NOTE: Flags supports date|time|microsecondsflags: [date, time]rotation:maxSize: 100accessLog:output: logs/access.logformat: '%h %l %u %t "%r" %>s %b'rotation:maxSize: 100maxBackups: 14maxAge: 28compress: truelocalTime: true# Define custom error pages (x matches [0-9])errorPages:'404': /err/404.html'5xx': /err/5xx.html# Define named filtersfilters:'auth':type: basicAuthusers:# WARNING: It is unsafe to define plain secrets. It is recommended for development use only.- name: fastsecret: httpdusersFile: ./users.yaml'cache':type: headerresponse:set:'Cache-Control': 'private, max-age=3600'# Define named handlershandlers:'static':type: fsindexNames: [index.html]generateIndexPages: falsecompress: true'expvar':type: expvar'hello':type: contentheaders:'Content-Type': 'text/plain; charset=utf-8'body: |Hello FastHttpd# The routes are processed in sequence and interrupted when the status or the handler is specified.routes:# Allows GET, POST, HEAD only.- methods: [PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH]status: 405statusMessage: 'Method not allowed'# Route to /index.html.- path: /match: equalhandler: static# Route to expvar handler.- path: /expvarmatch: equalhandler: expvar# Redirect to external url with status code 302.- path: /redirect-externalmatch: equalrewrite: http://example.com/status: 302# Redirect to internal uri with status code 302 and appendQueryString.# If "GET /redirect-internal?name=value" is requested then it redirect to "/internal?foo=bar&name=value"- path: /redirect-internalmatch: equalrewrite: /internal?foo=barrewriteAppendQueryString: truestatus: 302# Route to static resources using regexp.- path: .*\.(js|css|jpg|png|gif|ico)$match: regexpfilters: [cache]methods: [GET, HEAD]handler: static# Rewrite the path and route to next (no handler and no status).- path: ^/view/(.+)match: regexprewrite: /view?id=$1# Other requests are routed to hello with auth filter.- filters: [auth]handler: helloroutesCache:enable: trueexpire: 60000---host: localhostlisten: ':8443'ssl:certFile: ./ssl/localhost.crtkeyFile: ./ssl/localhost.keyhandlers:'backend':type: proxyurl: 'http://localhost:8080'routes:- path: /handler: backend---include: conf.d/*.yaml
host: localhost
Listen represents the IP adress and port.
listen: ':8080'
Path to the root directory to serve files from.
./
indicates the directory where config.yaml is located.
root: ./public
Server represents settings for fasthttp.Server.
server:name: fasthttpdreadBufferSize: 4096writeBufferSize: 4096readTimeout: 60swriteTimeout: 60s
Key | Description |
---|---|
name | Server name for sending in response headers |
readBufferSize | Per-connection buffer size for requests' reading. This also limits the maximum header size. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers (for example, BIG cookies). Default buffer size is used if not set. |
writeBufferSize | Per-connection buffer size for responses' writing. Default buffer size is used if not set. |
readTimeout | The amount of time allowed to read the full request including body. The connection's read deadline is reset when the connection opens, or for keep-alive connections after the first byte has been read. By default request read timeout is unlimited. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". |
writeTimeout | The maximum duration before timing out writes of the response. It is reset after the request handler has returned. By default response write timeout is unlimited. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". |
Other string, bool and numbers can be set. See fashttp/server.go for details.
Log represents settings for logging.
log:output: logs/error.log# NOTE: Flags supports date|time|microsecondsflags: [date, time]rotation:maxSize: 100maxBackups: 14maxAge: 28compress: truelocalTime: true
Key | Description |
---|---|
output | Output file path. stdout and stderr are special strings that indicate standard output and standard error output, respectively. |
flags | For example, flags [date, time] produce 2009/01/23 01:23:23 message |
rotation.maxSize | The maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes. |
rotation.maxBackups | The maximum number of days to retain old log files based on the timestamp encoded in their filename. Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc. The default is not to remove old log files based on age. |
rotation.maxAge | The maximum number of old log files to retain. The default is to retain all old log files (though MaxAge may still cause them to get deleted.) |
rotation.compress | Compress determines if the rotated log files should be compressed using gzip. The default is not to perform compression. |
rotation.localTime | LocalTime determines if the time used for formatting the timestamps in backup files is the computer's local time. The default is to use UTC time. |
The rotation is based on natefinch/lumberjack.
AccessLog represents settings for the access logging.
accessLog:output: logs/access.logformat: '%h %l %u %t "%r" %>s %b'rotation:maxSize: 100maxBackups: 14maxAge: 28compress: truelocalTime: true
Key | Description |
---|---|
output | Output file path. stdout and stderr are special strings that indicate standard output and standard error output, respectively. |
format | The characteristics of the request itself are logged by placing "%" directives in the format string Apache Custom Log Formats |
rotation.maxSize | The maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes. |
rotation.maxBackups | The maximum number of days to retain old log files based on the timestamp encoded in their filename. Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc. The default is not to remove old log files based on age. |
rotation.maxAge | The maximum number of old log files to retain. The default is to retain all old log files (though MaxAge may still cause them to get deleted.) |
rotation.compress | Compress determines if the rotated log files should be compressed using gzip. The default is not to perform compression. |
rotation.localTime | LocalTime determines if the time used for formatting the timestamps in backup files is the computer's local time. The default is to use UTC time. |
The rotation is based on natefinch/lumberjack.
# Define custom error pages (x matches [0-9])errorPages:'404': /err/404.html'5xx': /err/5xx.html
Key | Description |
---|---|
root | (Optional) Override root |
(http status) | Path to custom error page. A http status text can be contain 'x' as wildcard. (eg. '5xx', '40x') |
Any named filter can be defined for filters.
filters:'auth':type: basicAuthusers:# WARNING: It is unsafe to define plain secrets. It is recommended for development use only.- name: fastsecret: httpdusersFile: ./users.yaml'cache':type: headerresponse:set:'Cache-Control': 'private, max-age=3600'
The following filter types are supported.
BasicAuth represents settings for the basic access authentication.
Key | Description |
---|---|
users[].name | User name |
users[].secret | Plain secret |
usersFile | Path to users file. Refer to testdata/users.yaml |
Header represents settings for customizing request and response headers.
Key | Description |
---|---|
request.set | The header-value mapping. If the same header exists, it is overwritten. |
request.add | The header-value mapping |
request.del | The list of headers |
response.set | The header-value mapping If the same header exists, it is overwritten. |
response.add | The header-value mapping |
response.del | The list of headers |
Any named handler can be defined for handlers.
handlers:'static':type: fsindexNames: [index.html]generateIndexPages: falsecompress: true'hello':type: contentheaders:'Content-Type': 'text/plain; charset=utf-8'body: |Hello FastHttpd'backend':type: proxyurl: http://localhost:9000/
The following handler types are supported.
FS represents settings for request handler serving static files from the local filesystem.
Key | Description |
---|---|
root | Path to the root directory to serve files from. If omitted, the top-level root is used. |
indexNames | List of index file names to try opening during directory access. |
generateIndexPages | Index pages for directories without files matching IndexNames are automatically generated if set. |
compress | Transparently compresses responses if set to true. |
Other string, bool and numbers can be set. See fashttp/fs.go for details.
Content represents settings for serving a in-memory content.
Key | Description |
---|---|
body | Content body |
headers | The key-value mapping or 'Key: Value' list |
Proxy represents settings for proxy the single backend.
Key | Description |
---|---|
url | Backend URL |
Balancer represents settings for proxy the multiple backends. This handler using zehuamama/balancer.
Key | Description |
---|---|
urls | Backend URLs |
algorithm | ip-hash, consistent-hash, p2c, random, round-robin, least-load, bounded |
healthCheckInterval | health-check interval (seconds) |
# Balancer examplehandlers:'backend':type: balanceralgorithm: round-robinhealthCheckInterval: 5urls:- http://localhost:9000/- http://localhost:9001/- http://localhost:9002/routes:- path: /handler: backend
The routes are processed in sequence and interrupted when the status
or the handler
is specified.
routes:- path: / # The request pathmatch: prefix # The match type [ prefix | equal | regexp ]methods: [] # Allow HTTP methodsfilters: [] # Filter namesrewrite: '' # The rewrite pathrewriteAppendQueryString: false # Like apache QSA (Query String Append) flaghandler: '' # The handler namestatus: 0 # HTTP statusstatusMessage: '' # Custom HTTP status message
Rewrite the path and route to backend.
handlers:'backend':type: proxyurl: 'http://localhost:8080'routes:- path: ^/view/(.+)match: regexprewrite: /view?id=$1- path: /handler: backend
Redirect to external url with status code 302.
routes:- path: /redirect-externalmatch: equalrewrite: http://example.com/status: 302
ssl:certFile: ./ssl/localhost.crtkeyFile: ./ssl/localhost.key
ssl:autoCert: trueautoCertCacheDir: /etc/fasthttpd/cache
Virtual hosts can be defined in multiple YAML. The first document is the default host.
host: default.example.comlisten: ':80'---host: other.example.comlisten: ':80'
include: /etc/fasthttpd/conf.d/*.yaml