refer to : https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/
Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.
This section describes how to configure NGINX and NGINX Plus to serve static content, how to define which paths are searched to find requested files, how to set up index files, and how to tune NGINX and NGINX Plus, as well as the kernel, for optimal performance.
Root Directory and Index Files
The root
directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root
directive. The directive can be placed on any level within the http {}, server {}, or location {}
contexts. In the example below, the root
directive is defined for a virtual server. It applies to all location {}
blocks where the root
directive is not included to explicitly redefine the root:
server {
root /www/data;
location / {
}
location /images/ {
}
location ~ \.(mp3|mp4) {
root /www/media;
}
}
Here, NGINX searches for a URI that starts with /images/
in the /www/data/images/
directory in the file system. But if the URI ends with the .mp3
or .mp4
extension, NGINX instead searches for the file in the /www/media/
directory because it is defined in the matching location
block.
If a request ends with a slash, NGINX treats it as a request for a directory and tries to find an index file in the directory. The index
directive defines the index file’s name (the default value is index.html
). To continue with the example, if the request URI is /images/some/path/
, NGINX delivers the file /www/data/images/some/path/index.html
if it exists. If it does not, NGINX returns HTTP code 404 (Not Found)
by default.
You can list more than one filename in the index
directive. NGINX searches for files in the specified order and returns the first one it finds.
To return the index file, NGINX checks for its existence and then makes an internal redirect to the URI obtained by appending the name of the index file to the base URI. The internal redirect results in a new search of a location and can end up in another location as in the following example:
location / {
root /data;
index index.html index.php;
}
location ~ \.php {
fastcgi_pass localhost:8000;
#...
}
Here, if the URI in a request is /path/
, and /data/path/index.html
does not exist but /data/path/index.php
does, the internal redirect to /path/index.php
is mapped to the second location. As a result, the request is proxied.
Trying Several Options
The try_files
directive can be used to check whether the specified file or directory exists; NGINX makes an internal redirect if it does, or returns a specified status code if it doesn’t. For example, to check the existence of a file corresponding to the request URI, use the try_files
directive and the $uri
variable as follows:
server {
root /www/data;
location /images/ {
try_files $uri /images/default.gif;
}
}
The file is specified in the form of the URI, which is processed using the root
or alias
directives set in the context of the current location or virtual server. In this case, if the file corresponding to the original URI doesn’t exist, NGINX makes an internal redirect to the URI specified by the last parameter, returning /www/data/images/default.gif
.
The last parameter can also be a status code (directly preceded by the equals sign) or the name of a location. In the following example, a 404
error is returned if none of the parameters to the try_files
directive resolve to an existing file or directory.
location / {
try_files $uri $uri/ $uri.html =404;
}
In the next example, if neither the original URI nor the URI with the appended trailing slash resolve into an existing file or directory, the request is redirected to the named location which passes it to a proxied server.
location / {
try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://backend.example.com;
}
The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.