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;
}

nginx 提供静态内容的更多相关文章

  1. [Linux] Nginx 提供静态内容和优化积压队列

    1.try_files指令可用于检查指定的文件或目录是否存在; NGINX会进行内部重定向,如果没有,则返回指定的状态代码.例如,要检查对应于请求URI的文件是否存在,请使用try_files指令和$ ...

  2. Nginx作为静态内容服务器(Windows环境)

    1.简单安装 1)下载 http://nginx.org/en/download.html 2)解压后的路径 E:\Study\nginx\nginx-1.7.6 3)执行nginx.exe,访问ht ...

  3. Django之使用celery和NGINX生成静态页面实现性能优化

    性能优化原理: 当我们要给client浏览器返回一个页面时,我们需要去数据库查询数据并将数据和基本页面模板渲染形成页面返回给客户端,但如果每一个用户访问时都去查询一次首页的的数据时,当日访问量很大时那 ...

  4. nginx学习之静态内容篇(五)

    1.根目录和索引文件 server { root /www/data; location / { } location /images/ { } location ~ \.(mp3|mp4) { ro ...

  5. django无法加载admin的静态内容的问题(Centos7+Nginx+uwsgi环境下)

    Nginx静态资源无法加载,导致admin没有CSS样式: 这个问题,主要是要理解: 1.Django不会去解析静态内容(css,js,img)等,而是交给Nginx去处理,所以nginx.conf要 ...

  6. Nginx使用教程(五):使用Nginx缓存之缓存静态内容

    NGINX虽然已经对静态内容做过优化. 但在高流量网站的情况下,仍然可以使用open_file_cache进一步提高性能. NGINX缓存将最近使用的文件描述符和相关元数据(如修改时间,大小等)存储在 ...

  7. Keepalived+Nginx提供前端负载均衡+主从双机热备+自动切换

    原文链接:http://unun.in/linux/156.html 方案: 采用两台Nginx服务器作为前端,提供静态web内容,分发web请求,一主一从,Keepalived实现状态监测,保证 N ...

  8. Nginx配置静态资源

    静态服务器 静态服务器概念非常简单:当用户请求静态资源时,把文件内容回复给用户. 但是,要把静态服务做到极致,需要考虑的方面非常多: 正确书写header:设置content-type.过期时间等 效 ...

  9. 自己搭建CDN服务器静态内容加速-LuManager CDN使用教程

    为什么要自己来搭建一个CDN服务器实现网站访问加速?一是免费CDN服务稳定性和加速效果都不怎么行:二是用国内的付费CDN服务价格贵得要死,一般的草根站长无法承受:三是最现实的问题国内的CDN要求域名B ...

随机推荐

  1. BZOJ 1975 SDOI2010 魔法猪学院 A*k短路

    题目大意:给定一个值E 求起点到终点的最多条路径 使长度之和不超过E k短路的A*算法--每一个点有一个估价函数=g[x]+h[x] 当中g[x]是从源点出发已经走了的长度 h[x]是从这个点到汇点的 ...

  2. I深搜

    <span style="color:#330099;">/* I - 深搜 基础 Time Limit:1000MS Memory Limit:10000KB 64b ...

  3. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  4. SQL Server中的查询

          本博文简介一下SQL Server中经常使用的几类查询及相关使用的方法.       一.ExecuteScalar方法获取单一值       ExecuteScalar方法是SqlCom ...

  5. 苹果公司的新的编程语言 Swift 高级语言(十一)--初始化类的析构函数的一个实例

    一 .实例的初始化          实例的初始化是准备一个类.结构或枚举的实例以便使用的过程. 初始化包含设置一个实例的每个存储属性为一个初始值,以及运行不论什么其他新的实例可以使用之前须要的设置或 ...

  6. JavaScript动态更改页面元素

    通过JavaScript动态变化HTML元素 至HTML加元 首先需要创建一个标签,然后添加到标签中的相应的内容.然后创建添加到相应的位置好标签. <!DOCTYPE html PUBLIC & ...

  7. Maven和Gradle

    Maven和Gradle对比 Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山,而Gradle的发展则如日中天.笔者有幸见证 ...

  8. lua 远程调试 【zeroBrane 使用mobdebug】(good转)

    最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...

  9. 【Java先进】Lock、通过使用线程池

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...

  10. [Windows Phone] 在 Windows Phone 8 控制闪光灯

    原文:[Windows Phone] 在 Windows Phone 8 控制闪光灯 ? 前言 在 Windows Phone 如果想要控制闪光灯,该怎麽做?在 Windows Phone 8 提供类 ...