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. android 自己定义开关(SwitchButton)

    近期心血来潮,写了一个自己定义仿iPhone的开关. 有须要的同学能够来下载啦.支持点击自己主动滚动,速率能够自己依据须要改动.触摸滚动,大小自己定义,支持改动样式.就不录制动画,就上传了两张图给大家 ...

  2. ClientID 获取服务端控件,客户端id的方法

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  3. Python3.2官方文件翻译--课堂笔记和异常是阶级

    6.7备注 有时喜欢Pasca在"录"和C中"数据体"的数据类型很实用.集合一些数据项. 一个空类定义能够清楚地显示: class Employee: pass ...

  4. 《5》CentOS7.0+OpenStack+kvm云平台的部署—组态Horizon

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  5. Windows Phone开发(16):样式和控件模板

    原文:Windows Phone开发(16):样式和控件模板 在前面资源一文中也提过样式,样式就如同我们做HTML页排版时常用到的CSS样式表,它是对于特定娄型的可视化元素,应该可以直接说是针对控件的 ...

  6. MySQL Full Join的实现

    MySQL Full Join的实现 由于MySQL不支持FULL JOIN,以下是替代方法 left join + union(可去除反复数据)+ right join select * from ...

  7. 三种方法让你的Service不被“一键加速”和系统杀掉

    基本上大家都知道提高service优先级能够在非常大程度上让你的service免于由于内存不足而被kill,当然系统仅仅是在此时先把优先级低的kill掉,假设内存还是不够,也会把你的service干掉 ...

  8. HBuilder之初体验

    听闻HTML5定稿了,所以特意去了解了下.文章有提到HTML5的一款IDE(HBuilder,貌似出来好久了,孤陋寡闻....),于是来到官网http://dcloud.io/ ,被演示图震惊了!果然 ...

  9. WPF动态加载3D 放大-旋转-平移

    原文:WPF动态加载3D 放大-旋转-平移 WavefrontObjLoader.cs 第二步:ModelVisual3DWithName.cs public class ModelVisual3DW ...

  10. C#实现远程机器管理

    原文:C#实现远程机器管理 目前处于待离职状态,原先所有的工作都在进行交接,过程当中不乏有很多先前整理的和动手尝试实现的功能:我的主页中已经列出来一部分内容,有兴趣的可以前往看一看. 接下来的内容主要 ...