Hosts Example
Serving files from a specific directory
Section titled “Serving files from a specific directory”For this example, we will configure a host to serve files from the /var/www/uploads directory for requests to uploads.example.com.
domain: uploads.example.comlocations:- 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/imagesdirectory. - Requests to
uploads.example.com/videos/*will serve files from the/var/www/uploads/videosdirectory. - All other requests will return a 404 Not Found message.
Proxying requests to a backend service
Section titled “Proxying requests to a backend service”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.
domain: api.example.comlocations:- match: '*' proxy: localhost:3000All requests to api.example.com will be forwarded to the backend service at localhost:3000.
Serving static content with a fallback
Section titled “Serving static content with a fallback”This example demonstrates how to serve static content for a specific path and provide a fallback message for all other requests.
domain: example.comlocations:- 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/welcomewill return the welcome message. - Requests to
example.com/infowill return a message containing the user’sUser-Agentand IP address. - All other requests will return a prompt to visit
/welcome.
Combining static content and proxying
Section titled “Combining static content and proxying”This example combines serving static content and proxying requests to different backend services based on the request path.
domain: example.comlocations:- match: '/static/*' root: /var/www/static- match: '/api/*' proxy: localhost:3000- match: '*' proxy: localhost:8000With this configuration:
- Requests to
example.com/static/*will serve files from the/var/www/staticdirectory. - Requests to
example.com/api/*will be proxied to the backend service atlocalhost:3000. - All other requests will be proxied to the backend service at
localhost:8000.