以下内容翻译自官方文档,原文地址。
web服务器的一个重要任务是对外提供文件(比如图片或者静态HTML页面)。
An important web server task is serving out files (such as images or static HTML pages).
下面你会实现一个例子,根据请求,从不同的本地目录提供文件:/data/www(这个目录包含HTML文件)和/data/images(包含图片)。
You will implement an example where, depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (containing images).
这需要编辑配置文件,在http块内部设置一个server块,后者包含两个location块。
This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.
首先,创建/data/www目录并放入一个包含任意文本内容的index.html文件,创建/data/images目录并放一些图片文件进去。
First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.
然后,打开配置文件。
Next, open the configuration file.
默认配置文件已经包含了几个server block的例子,大多数都被注释掉了。
The default configuration file already includes several examples of the server block, mostly commented out.
现在注释所有的这些server块然后创建一个新的server块:
For now comment out all such blocks and start a new server block:
http {
server {
}
}
一般情况下,配置文件可能包含几个server块,用监听端口号和server名称进行区分。
Generally, the configuration file may include several server blocks distinguished by ports on which they listen to and by server names.
一旦nginx决定用哪个server处理一个请求,它会比对请求首部的URI和server内部的location指令。
Once nginx decides which server processes a request, it tests the URI specified in the request’s header against the parameters of the location directives defined inside the server block.
下面是server块内部的location块。
Add the following location block to the server block:
location / {
root /data/www;
}
这个location块指定了“/”前缀与请求的URI进行对比。
This location block specifies the “/” prefix compared with the URI from the request.
如果匹配了请求,URI就会添加到root指令指定的path前面,也就是,/data/www前面,形成请求的文件在本地文件系统中的路径。
For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system.
如果有多个location块都匹配了请求,nginx会选择前缀最长的一个。
If there are several matching location blocks nginx selects the one with the longest prefix.
上面的location块提供了最短的前缀,长度为一,所以只有其他全部location块都不匹配的时候,这个块才会被使用。
The location block above provides the shortest prefix, of length one, and so only if all other location blocks fail to provide a match, this block will be used.
下面,第二个location块:
Next, add the second location block:
location /images/ {
root /data;
}
它会匹配所有以/images/开头的请求(location / 也匹配这个请求,但是前缀更短)
It will be a match for requests starting with /images/ (location / also matches such requests, but has shorter prefix).
最终的server块的配置如下所示:
The resulting configuration of the server block should look like this:
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
这已经是一个可以工作的server配置,监听标准80端口,在本机可以访问http://localhost/进行访问。
This is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at http://localhost/.
为响应以/images/作为URI开头的请求,服务器会从/data/images目录读取文件。
In response to requests with URIs starting with /images/, the server will send files from the /data/images directory.
例如,为了响应http://localhost/images/example.png请求,nginx会提供/data/images/example.png文件。
For example, in response to the http://localhost/images/example.png request nginx will send the /data/images/example.png file.
如果这个文件不存在,nginx会发送一个指示404错误的响应。
If such file does not exist, nginx will send a response indicating the 404 error.
不以/images/开头的请求会被映射到/data/www目录。
Requests with URIs not starting with /images/ will be mapped onto the /data/www directory.
例如,为响应http://localhost/some/example.html 请求,nginx会发送/data/www/some/example.html文件。
For example, in response to the http://localhost/some/example.html request nginx will send the /data/www/some/example.html file.
为了使新的配置生效,如果nginx还没启动,就启动,否则想nginx主线程发送reload信号,执行:
To apply the new configuration, start nginx if it is not yet started or send the reload signal to the nginx’s master process, by executing:
nginx -s reload
一旦有什么与预期不符的事情,你可以尝试在access.log和error.log文件中查找原因,它们在/usr/local/nginx/logs或者/var/log/nginx目录下面。
In case something does not work as expected, you may try to find out the reason in access.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx.