Javatpoint Logo
Javatpoint Logo

NGINX Serving Static Content

In this section, we will discuss how to configure the Nginx Plus and Nginx open source to serve static content.

Root Directory and Index Files

The root directive is used to define the root directory that will be used to search for a file. To obtain the requested file's path, NGINX appends the requested URI to the path defined by the root directive. The directive can be placed on any level within the server {}, http {}, or location {} contexts.

Let's see an example; here, the root directive is specified for a virtual server. It applies to all location {} blocks where the root directive is not added to redefine the root explicitly:

In the above example, NGINX searches for a URI (Uniform Resource Identifier) 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 finds for the file in the /www/media/ directory because it is specified in the matching location block.

If a request suffix 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 specifies the name of the index file (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 then by default NGINX returns HTTP code 404 (Not Found). To configure NGINX to return an automatically generated directory listing instead, add the "on" parameter to the auto index directive:

We can list more than one filename in the index directive. NGINX finds for files in the specified order and returns the first one it finds.

The $geo variable used here is a custom variable set through the geo directive. The value of the variable depends on the IP address of the client.

To return the index file, NGINX checks that the file exists or not 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 the results in a fresh search of a location and can end up in another location as in the following example:

In the above example, 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 defined 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 that the file exists or not corresponding to the request URI, use the try_files directive and the $uri variable as follows:

The file is defined 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 which is directly preceded by the equals sign or the name of a location. In the below example, a 404 error is returned if none of the parameters to the try_files directive resolve to an existing file or directory.

Optimizing Performance for Serving Content

Speed of the loading is a crucial factor for serving any content. Making minor optimizations to our NGINX configuration may increase the productivity and helps to reach optimal performance.

Enabling sendfile

By default, NGINX controls file transmission itself and copies the file into the buffer before sending it. When we enable the sendfile directive, then it will eliminate the step of copying the data into the buffer and enables direct copying data from one file descriptor to another. Alternatively, to stop a fast connection from entirely occupying the worker process, we can use the sendfile_max_chunk directive to limit the amount of data transferred in a single sendfile() call (in this example, to 1 MB):

Enabling tcp_nopush

Add the tcp_nopush directive together with the sendfile in the directive. This enables NGINX to send HTTP response headers in one packet right after the chunk of data has been obtained from the sendfile().

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA