Nginx provides static content

Source: Internet
Author: User

Serving Static Content
Provide static content

Original address:http://nginx.com/resources/admin-guide/serving-static-content/

This sections describes how to serve static content, how to use different ways of setting up the paths to look for files, a nd how to set up index files.

This chapter provides a static file. How to set the path to find files in different ways, assuming that the index file is set.

Root Directory and Index Files
root folder and index file

The root directive specifies the root directory which would be is used to search for a file. To obtain the path of a requested file, NGINX adds the request URI added to the path specified in root. The directive can placed on any level within the HTTP, server, or location contexts. In the example below, the root directive was defined for a virtual server. It'll be applied to all locations where the root was not redefined:

root Specifies the root folder of the lookup file. To get the path to the requested file, Nginx adds the request URI to the specified root. This command can be set in either the Http,server or the location environment, regardless of the level. In the following example, the root directive is defined to a virtual host. All location with no redefined root will use this value:

server {    root/www/data;    Location/{    }    location/images/{    } location    ~ \. ( Mp3|mp4) {        root/www/media;    }}

Here, the /images/some/path URI is mapped to /www/data/images/some/path on the file system, and NGI NX would try to get a file there. A request with a URI such as/any/path/file.mp3 is mapped to /www/media/any/path/file.mp3 Becaus e The corresponding location defines its own root.

Here, the URI "/images/some/path" is mapped to "/www/data/images/some/path" in the file system, and the Nginx tries to get the file in that path. The request with the URI "/any/path/file.mp3" is mapped to "/www/media/any/path/file.mp3" because the corresponding location defines its own root.

If a request ends with a slash, NGINX would treat it as a request for a directory and would try to find a index file there. The name of the index file is specified in the index directive and the default value is index.html. In the example above, to the request with the URI/images/some/path/ NGINX would respond with/WWW/DATA/IMAGES/S ome/path/index.html if that file exists. If This file is does not exist, a 404 error is returned by default. It is possible, however, to return a automatically generated directory listing when the "index file does not" exist by sett ing the autoindex directive to on.

Suppose the request ends with a slash. Nginx when it requests a folder, it will look up the index file under this folder. The file name of the index file is specified in the index directive, and the default value is index.html. In the example above, for the request URI "/images/some/path" Nginx will respond "/www/data/images/some/path/index.html" assuming that the file exists. Assuming that the file does not exist, a 404 file is returned by default.

However. If the autoindex command is set to ON, assume that the index file does not exist. You may also return a list of folders that you have actively generated.

location/images/{    autoindex on;}

The index directive can list more than one file name. Each file is checked in the order listed, and the first file that exists would be returned.

The index directive can list more than one file name. Each file is searched sequentially, returning the first found file.

Location/{    index Index. $geo. html index.htm index.html;}

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

$geo variable here is a self-defined variable, which is set by the Geo directive. The value of the variable depends on the ClientIP address.

To return the index file, NGINX checks it existence and then makes a internal redirect to the URI obtained from the Inde x file name and the base URI. The internal redirect results in a new search for a location and can end up in another location as in the following example :

To return an index file, Nginx retrieves whether he exists and then makes an internal redirect based on the URI of the requested URI and the index file name. Internal redirects can jump to a new location to find the results in a location, such as the following example:

Location/{    root/data;    Index index.html index.php;} Location ~ \.php {    Fastcgi_pass localhost:8000;    ...}

Here, if a request have the /path/URI, and it turns out that /data/path/index.html does not exist, but /data/path/index.php does, the internal redirect to /path/index.php would be mapped to the second location. As a result, the request would be proxied.

Here, suppose a request URI is "/path/", resulting in "/data/path/index.html" file does not exist, but "/data/path/index.php" exists, to "/path/index.php" 's internal redirection to the second location. As a result, the request was proxied.

Trying Several Options
Use multiple options

The try_files directive can be used to check whether the specified file or directory exists and make an internal Redirect, or return a specific status code if they don ' t. For example, to check the existence of a file corresponding to the request URI, use the try_files directive and T He $uri variable as follows:

The try_files directive is used to check whether a specified file or folder exists, assuming that there is no internal redirect. or returns a specified status code. For example, use the Try_files directive and the $uri variable to check whether a file exists based on the corresponding request URI. See the following example:

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 or virtual server. In this case, if the file corresponding to the original URI doesn ' t exist NGINX makes a internal redirect to the URI spec Ified in the last parameter returning /www/data/images/default.gif.

The last parameter can also is a status code (specified after =) or the name of a location. In the following example, a 404 error is returned if none of the options resolves to an existing file or directory.

The file is specified in the URI component, and the URI is handled by the root or alias instruction set in the context of the current location or virtual host. Such Assume that the corresponding file for the native URI does not exist. Nginx uses the last parameter as the URI to do an internal redirect, returning "/www/data/images/default.gif". The last parameter can also be a status code (specified with =) or a location name. In the following example. Assuming that all options cannot find the file or folder that exists, a 404 error is returned.

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 to an existing fi Le or directory, the request is redirected to the named location which passes it to a proxied server.

The following demo example assumes that the original URI and URI of the trailing slash are appended. is not resolved to an existing file or folder, the request is redefined to a named location, which sends the request to a proxy server.

Location/{    try_files $uri $uri/@backend;} Location @backend {    proxy_pass http://backend.example.com;}












Nginx provides static content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.