Configuration#
Built-in configuration#
The following is a minimal configuration built into fasthttpd.
host: localhost
listen: ':8080'
root: ./public
log:
output: stderr
handlers:
'static':
type: fs
indexNames: [index.html]
routes:
- path: /
handler: staticCustom configuration#
The following is a configuration that uses most of the current FastHttpd features.
Expand full configuration
Host#
host: localhostListen#
Listen represents the IP adress and port.
listen: ':8080'Root#
Path to the root directory to serve files from.
./ indicates the directory where config.yaml is located.
root: ./publicServer#
Server represents settings for fasthttp.Server.
server:
name: fasthttpd
readBufferSize: 4096
writeBufferSize: 4096
readTimeout: 60s
writeTimeout: 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#
Log represents settings for logging.
log:
output: logs/error.log
# NOTE: Flags supports date|time|microseconds
flags: [date, time]
rotation:
maxSize: 100
maxBackups: 14
maxAge: 28
compress: true
localTime: 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#
AccessLog represents settings for the access logging.
accessLog:
output: logs/access.log
format: '%h %l %u %t "%r" %>s %b'
rotation:
maxSize: 100
maxBackups: 14
maxAge: 28
compress: true
localTime: 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.
ErrorPages#
# 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’) |
Filters#
Any named filter can be defined for filters.
filters:
'auth':
type: basicAuth
users:
# WARNING: It is unsafe to define plain secrets. It is recommended for development use only.
- name: fast
secret: httpd
usersFile: ./users.yaml
'cache':
type: header
response:
set:
'Cache-Control': 'private, max-age=3600'The following filter types are supported.
- basicAuth - Provides the basic access authentication.
- header - Customize request and response headers.
BasicAuth#
BasicAuth represents settings for the basic access authentication.
filters:
'auth':
type: basicAuth
users:
# WARNING: It is unsafe to define plain secrets. It is recommended for development use only.
- name: fast
secret: httpd
usersFile: ./users.yaml| Key | Description |
|---|---|
| users[].name | User name |
| users[].secret | Plain secret |
| usersFile | Path to users file. Refer to testdata/users.yaml |
Header#
Header represents settings for customizing request and response headers.
filters:
'cache':
type: header
response:
set:
'Cache-Control': 'private, max-age=3600'| 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 |
Handlers#
Any named handler can be defined for handlers.
handlers:
'static':
type: fs
indexNames: [index.html]
generateIndexPages: false
compress: true
'hello':
type: content
headers:
'Content-Type': 'text/plain; charset=utf-8'
body: |
Hello FastHttpd
'backend':
type: proxy
url: http://localhost:9000/The following handler types are supported.
- fs - Serve static files from the local filesystem
- content - Serve a in-memory content
- proxy - Proxy to a backend
- balancer - Proxy to multiple backends with some algorithm
FS#
FS represents settings for request handler serving static files from the local filesystem.
handlers:
'static':
type: fs
indexNames: [index.html]
generateIndexPages: false
compress: true| 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#
Content represents settings for serving a in-memory content.
handlers:
'hello':
type: content
headers:
'Content-Type': 'text/plain; charset=utf-8'
body: |
Hello FastHttpd | Key | Description |
|---|---|
| headers | The key-value mapping or ‘Key: Value’ list |
| body | Content body |
Proxy#
Proxy represents settings for proxy the single backend.
handlers:
'backend':
type: proxy
url: 'http://localhost:8080'| Key | Description |
|---|---|
| url | Backend URL |
If you want to proxy multiple backends, use Balancer.
Balancer#
Balancer represents settings for proxy the multiple backends. This handler using zehuamama/balancer.
handlers:
'backend':
type: balancer
urls:
- http://localhost:9000/
- http://localhost:9001/
- http://localhost:9002/
algorithm: ip-hash
healthCheckInterval: 5| Key | Description |
|---|---|
| urls | Backend URLs |
| algorithm | ip-hash, consistent-hash, p2c, random, round-robin, least-load, bounded |
| healthCheckInterval | health-check interval (seconds) |
If Proxy supports multiple backends, Balancer may be deprecated.
Routes#
The routes are processed in sequence and interrupted when the status or the handler is specified.
routes:
- path: / # The request path
match: prefix # The match type [ prefix | equal | regexp ]
methods: [] # Allow HTTP methods
filters: [] # Filter names
rewrite: '' # The rewrite path
rewriteAppendQueryString: false # Like apache QSA (Query String Append) flag
handler: '' # The handler name
status: 0 # HTTP status
statusMessage: '' # Custom HTTP status messageRoutes examples#
Rewrite the path and route to backend.
handlers:
'backend':
type: proxy
url: 'http://localhost:8080'
routes:
- path: ^/view/(.+)
match: regexp
rewrite: /view?id=$1
- path: /
handler: backendRedirect to external url with status code 302.
routes:
- path: /redirect-external
match: equal
rewrite: http://example.com/
status: 302Routes Cache#
Route calculations are cached, resulting in significant performance improvements, especially when using regular expressions.
routesCache:
enable: true
expire: 60000SSL#
ssl:
certFile: ./ssl/localhost.crt
keyFile: ./ssl/localhost.keySSL auto cert#
ssl:
autoCert: true
autoCertCacheDir: /etc/fasthttpd/cacheVirtual hosts#
Virtual hosts can be defined in multiple YAML. The first document is the default host.
host: default.example.com
listen: ':80'
---
host: other.example.com
listen: ':80'Include#
include: /etc/fasthttpd/conf.d/*.yaml