Skip to content

Hosts Example

For this example, we will configure a host to serve files from the /var/www/uploads directory for requests to uploads.example.com.

hosts/uploads.yml
domain: uploads.example.com
locations:
- match: '/images/*'
root: /var/www/uploads/images
- match: '/videos/*'
root: /var/www/uploads/videos
- match: '*'
content: '404 Not Found. The requested resource was not found.'

With this configuration:

  • Requests to uploads.example.com/images/* will serve files from the /var/www/uploads/images directory.
  • Requests to uploads.example.com/videos/* will serve files from the /var/www/uploads/videos directory.
  • All other requests will return a 404 Not Found message.

In this example, we will set up a host to proxy requests to a backend service running on localhost:3000 for the domain api.example.com.

hosts/api.yml
domain: api.example.com
locations:
- match: '*'
proxy: localhost:3000

All requests to api.example.com will be forwarded to the backend service at localhost:3000.

This example demonstrates how to serve static content for a specific path and provide a fallback message for all other requests.

hosts/static.yml
domain: example.com
locations:
- match: '/welcome'
content: 'Welcome to Static Example! This text is served directly from the host configuration, and does not require a backend server or static files.'
- match: '/info'
content: 'Your user agent is: $user_agent. Your IP address is: $remote_addr'
- match: '*'
content: 'Please visit /welcome for a welcome message!'

With this configuration:

  • Requests to example.com/welcome will return the welcome message.
  • Requests to example.com/info will return a message containing the user’s User-Agent and IP address.
  • All other requests will return a prompt to visit /welcome.

This example combines serving static content and proxying requests to different backend services based on the request path.

hosts/combined.yml
domain: example.com
locations:
- match: '/static/*'
root: /var/www/static
- match: '/api/*'
proxy: localhost:3000
- match: '*'
proxy: localhost:8000

With this configuration:

  • Requests to example.com/static/* will serve files from the /var/www/static directory.
  • Requests to example.com/api/* will be proxied to the backend service at localhost:3000.
  • All other requests will be proxied to the backend service at localhost:8000.