Serving Static Content
提供静态内容
原文地址:http://nginx.com/resources/admin-guide/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 http, server, 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指令设置。变量值取决于客户端IP地址。
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把请求发送到一个代理服务器。
-
location / {
-
try_files $uri $uri/ @backend;
-
}
-
-
location @backend {
-
proxy_pass http://backend.example.com;
-
}
(责任编辑:IT) |