| 1 |
[[static_files]] |
| 2 |
== Static files |
| 3 |
|
| 4 |
Cowboy comes with a ready to use handler for serving static |
| 5 |
files. It is provided as a convenience for serving files |
| 6 |
during development. |
| 7 |
|
| 8 |
For systems in production, consider using one of the many |
| 9 |
Content Distribution Network (CDN) available on the market, |
| 10 |
as they are the best solution for serving files. |
| 11 |
|
| 12 |
The static handler can serve either one file or all files |
| 13 |
from a given directory. The etag generation and mime types |
| 14 |
can be configured. |
| 15 |
|
| 16 |
=== Serve one file |
| 17 |
|
| 18 |
You can use the static handler to serve one specific file |
| 19 |
from an application's private directory. This is particularly |
| 20 |
useful to serve an 'index.html' file when the client requests |
| 21 |
the `/` path, for example. The path configured is relative |
| 22 |
to the given application's private directory. |
| 23 |
|
| 24 |
The following rule will serve the file 'static/index.html' |
| 25 |
from the application `my_app`'s priv directory whenever the |
| 26 |
path `/` is accessed: |
| 27 |
|
| 28 |
[source,erlang] |
| 29 |
{"/", cowboy_static, {priv_file, my_app, "static/index.html"}} |
| 30 |
|
| 31 |
You can also specify the absolute path to a file, or the |
| 32 |
path to the file relative to the current directory: |
| 33 |
|
| 34 |
[source,erlang] |
| 35 |
{"/", cowboy_static, {file, "/var/www/index.html"}} |
| 36 |
|
| 37 |
=== Serve all files from a directory |
| 38 |
|
| 39 |
You can also use the static handler to serve all files that |
| 40 |
can be found in the configured directory. The handler will |
| 41 |
use the `path_info` information to resolve the file location, |
| 42 |
which means that your route must end with a `[...]` pattern |
| 43 |
for it to work. All files are served, including the ones that |
| 44 |
may be found in subfolders. |
| 45 |
|
| 46 |
You can specify the directory relative to the application's |
| 47 |
private directory (e.g. `my_app/priv`). |
| 48 |
|
| 49 |
The following rule will serve any file found in the `my_app` |
| 50 |
application's private directory in the `my_app/priv/static/assets` |
| 51 |
folder whenever the requested path begins with `/assets/`: |
| 52 |
|
| 53 |
[source,erlang] |
| 54 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets"}} |
| 55 |
|
| 56 |
You can also specify the absolute path to the directory or |
| 57 |
set it relative to the current directory: |
| 58 |
|
| 59 |
[source,erlang] |
| 60 |
{"/assets/[...]", cowboy_static, {dir, "/var/www/assets"}} |
| 61 |
|
| 62 |
=== Customize the mimetype detection |
| 63 |
|
| 64 |
By default, Cowboy will attempt to recognize the mimetype |
| 65 |
of your static files by looking at the extension. |
| 66 |
|
| 67 |
You can override the function that figures out the mimetype |
| 68 |
of the static files. It can be useful when Cowboy is missing |
| 69 |
a mimetype you need to handle, or when you want to reduce |
| 70 |
the list to make lookups faster. You can also give a |
| 71 |
hard-coded mimetype that will be used unconditionally. |
| 72 |
|
| 73 |
Cowboy comes with two functions built-in. The default |
| 74 |
function only handles common file types used when building |
| 75 |
Web applications. The other function is an extensive list |
| 76 |
of hundreds of mimetypes that should cover almost any need |
| 77 |
you may have. You can of course create your own function. |
| 78 |
|
| 79 |
To use the default function, you should not have to configure |
| 80 |
anything, as it is the default. If you insist, though, the |
| 81 |
following will do the job: |
| 82 |
|
| 83 |
[source,erlang] |
| 84 |
---- |
| 85 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets", |
| 86 |
[{mimetypes, cow_mimetypes, web}]}} |
| 87 |
---- |
| 88 |
|
| 89 |
As you can see, there is an optional field that may contain |
| 90 |
a list of less used options, like mimetypes or etag. All option |
| 91 |
types have this optional field. |
| 92 |
|
| 93 |
To use the function that will detect almost any mimetype, |
| 94 |
the following configuration will do: |
| 95 |
|
| 96 |
[source,erlang] |
| 97 |
---- |
| 98 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets", |
| 99 |
[{mimetypes, cow_mimetypes, all}]}} |
| 100 |
---- |
| 101 |
|
| 102 |
You probably noticed the pattern by now. The configuration |
| 103 |
expects a module and a function name, so you can use any |
| 104 |
of your own functions instead: |
| 105 |
|
| 106 |
[source,erlang] |
| 107 |
---- |
| 108 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets", |
| 109 |
[{mimetypes, Module, Function}]}} |
| 110 |
---- |
| 111 |
|
| 112 |
The function that performs the mimetype detection receives |
| 113 |
a single argument that is the path to the file on disk. It |
| 114 |
is recommended to return the mimetype in tuple form, although |
| 115 |
a binary string is also allowed (but will require extra |
| 116 |
processing). If the function can't figure out the mimetype, |
| 117 |
then it should return `{<<"application">>, <<"octet-stream">>, []}`. |
| 118 |
|
| 119 |
When the static handler fails to find the extension, |
| 120 |
it will send the file as `application/octet-stream`. |
| 121 |
A browser receiving such file will attempt to download it |
| 122 |
directly to disk. |
| 123 |
|
| 124 |
Finally, the mimetype can be hard-coded for all files. |
| 125 |
This is especially useful in combination with the `file` |
| 126 |
and `priv_file` options as it avoids needless computation: |
| 127 |
|
| 128 |
[source,erlang] |
| 129 |
---- |
| 130 |
{"/", cowboy_static, {priv_file, my_app, "static/index.html", |
| 131 |
[{mimetypes, {<<"text">>, <<"html">>, []}}]}} |
| 132 |
---- |
| 133 |
|
| 134 |
=== Generate an etag |
| 135 |
|
| 136 |
By default, the static handler will generate an etag header |
| 137 |
value based on the size and modified time. This solution |
| 138 |
can not be applied to all systems though. It would perform |
| 139 |
rather poorly over a cluster of nodes, for example, as the |
| 140 |
file metadata will vary from server to server, giving a |
| 141 |
different etag on each server. |
| 142 |
|
| 143 |
You can however change the way the etag is calculated: |
| 144 |
|
| 145 |
[source,erlang] |
| 146 |
---- |
| 147 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets", |
| 148 |
[{etag, Module, Function}]}} |
| 149 |
---- |
| 150 |
|
| 151 |
This function will receive three arguments: the path to the |
| 152 |
file on disk, the size of the file and the last modification |
| 153 |
time. In a distributed setup, you would typically use the |
| 154 |
file path to retrieve an etag value that is identical across |
| 155 |
all your servers. |
| 156 |
|
| 157 |
You can also completely disable etag handling: |
| 158 |
|
| 159 |
[source,erlang] |
| 160 |
---- |
| 161 |
{"/assets/[...]", cowboy_static, {priv_dir, my_app, "static/assets", |
| 162 |
[{etag, false}]}} |
| 163 |
---- |