nginx 提供静态内容

Serving Static Content
提供静态内容

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

这一章讨怎样提供静态文件。怎样使用不同方式设置查找文件的路径,假设设置索引文件。

Root Directory and Index Files
根文件夹和索引文件

The root directive specifies the root directory which will be 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 be placed on any level within the httpserver, or location contexts. In the example below, the root directive is defined for a virtual server. It will be applied to all locations where the root is not redefined:

root指令指定查找文件的根文件夹。要获取请求的文件的路径,Nginx把请求URI加到指定的root后面。这个指令能够在http,server或者location环境不论什么一层里设置。以下这个样例,root指令定义给一个虚拟主机。全部没有重定义root的location都将使用这个值:

server {
    root /www/data;

    location / {
    }

    location /images/ {
    }

    location ~ \.(mp3|mp4) {
        root /www/media;
    }
}

Here, the /images/some/path URI will be mapped to /www/data/images/some/path on the file system, and NGINX will try to get a file there. A request with a URI such as/any/path/file.mp3 will be mapped to /www/media/any/path/file.mp3 because the corresponding location defines its own root.

这里,URI”/images/some/path”会被映射到文件系统中的"/www/data/images/some/path”,然后Nginx会试着在这个路径获取文件。而带着URI”/any/path/file.mp3”的请求会被映射到”/www/media/any/path/file.mp3”由于对应的location定义了自己的根。

If a request ends with a slash, NGINX will treat it as a request for a directory and will try to find an index file there. The name of the index file is specified in the index directive, the default value is index.html. In the example above, to the request with the URI/images/some/path/ NGINX will respond with/www/data/images/some/path/index.html if that file exists. If this file does not exist, a 404 error will be returned by default. It is possible, however, to return an automatically generated directory listing when the index file does not exist by setting the autoindex directive to on.

假设请求以斜杠结束。Nginx当它请求一个文件夹,将在这个文件夹下找索引文件。索引文件的文件名称在index指令中指定,缺省值为index.html。在上例中,对于请求URI"/images/some/path"Nginx将响应"/www/data/images/some/path/index.html”假设这个文件存在的话。假设这个文件不存在,默认会返回一个404文件。

然而。把autoindex指令设为on的话,假设索引文件不存在的话。也可能返回一个自己主动生成的文件夹列表。

location /images/ {
    autoindex on;
}

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

index指令能够列出不止一个文件名称。每一个文件按顺序查找,返回第一个被找到的文件。

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变量在这里是一个自己定义变量,通geo指令设置。变量值取决于clientIP地址。

To return the index file, NGINX checks its existence and then makes an internal redirect to the URI obtained from the index file name and 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:

要返回索引文件,Nginx检索他是否存在然后依据请求的URI和索引文件名称得到的URI做一个内部重定向。内部重定向能跳到一个新的location进行查找,能够在还有一个location里找到结果,比如以下这个样例:

location / {
    root /data;
    index index.html index.php;
}

location ~ \.php {
    fastcgi_pass localhost:8000;
    ...
}

Here, if a request has 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 will be mapped to the second location. As a result, the request will be proxied.

此间,假设一个请求URI为”/path/“,导致”/data/path/index.html”文件是不存在的,可是”/data/path/index.php”存在,对”/path/index.php”的内部重定向映射到第二个location。结果,这个请求就被代理了。

Trying Several 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 the $uri variable as follows:

try_files指令用来检查指定的文件或者文件夹是否存在,假设不存在做一个内部重定向。或者返回一个指定的状态码。比如,使用try_files指令和$uri变量,依据对应的请求URI来检查文件是否存在。见下例:

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 in the last parameter returning /www/data/images/default.gif.

The last parameter can also be 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 into an existing file or directory.

文件在URI组件中指定,URI由设置在当前location或者虚拟主机的上下文环境中的root或alias指令处理。这样。假设原生URI相应的文件不存在。Nginx用最后一个參数做URI做一次内部重定向,返回”/www/data/images/default.gif”。最后一个參数也能够是一个状态码(用=指定)或者一个location的名字。以下这个样例中。假设全部选项都不能找到存在的文件或文件夹,返回一个404错误。

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.

以下这个演示样例,假设附加尾部斜线的原始URI和URI。都没有解析到一个存在的文件或者文件夹,请求被重定义到一个命名过的location,这个location把请求发送到一个代理server。

location / {
    try_files $uri $uri/ @backend;
}

location @backend {
    proxy_pass http://backend.example.com;
}












posted @ 2015-09-19 15:29  lcchuguo  阅读(247)  评论(0编辑  收藏  举报