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 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指令设置。变量值取决于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 提供静态内容的更多相关文章
- [Linux] Nginx 提供静态内容和优化积压队列
1.try_files指令可用于检查指定的文件或目录是否存在; NGINX会进行内部重定向,如果没有,则返回指定的状态代码.例如,要检查对应于请求URI的文件是否存在,请使用try_files指令和$ ...
- Nginx作为静态内容服务器(Windows环境)
1.简单安装 1)下载 http://nginx.org/en/download.html 2)解压后的路径 E:\Study\nginx\nginx-1.7.6 3)执行nginx.exe,访问ht ...
- Django之使用celery和NGINX生成静态页面实现性能优化
性能优化原理: 当我们要给client浏览器返回一个页面时,我们需要去数据库查询数据并将数据和基本页面模板渲染形成页面返回给客户端,但如果每一个用户访问时都去查询一次首页的的数据时,当日访问量很大时那 ...
- nginx学习之静态内容篇(五)
1.根目录和索引文件 server { root /www/data; location / { } location /images/ { } location ~ \.(mp3|mp4) { ro ...
- django无法加载admin的静态内容的问题(Centos7+Nginx+uwsgi环境下)
Nginx静态资源无法加载,导致admin没有CSS样式: 这个问题,主要是要理解: 1.Django不会去解析静态内容(css,js,img)等,而是交给Nginx去处理,所以nginx.conf要 ...
- Nginx使用教程(五):使用Nginx缓存之缓存静态内容
NGINX虽然已经对静态内容做过优化. 但在高流量网站的情况下,仍然可以使用open_file_cache进一步提高性能. NGINX缓存将最近使用的文件描述符和相关元数据(如修改时间,大小等)存储在 ...
- Keepalived+Nginx提供前端负载均衡+主从双机热备+自动切换
原文链接:http://unun.in/linux/156.html 方案: 采用两台Nginx服务器作为前端,提供静态web内容,分发web请求,一主一从,Keepalived实现状态监测,保证 N ...
- Nginx配置静态资源
静态服务器 静态服务器概念非常简单:当用户请求静态资源时,把文件内容回复给用户. 但是,要把静态服务做到极致,需要考虑的方面非常多: 正确书写header:设置content-type.过期时间等 效 ...
- 自己搭建CDN服务器静态内容加速-LuManager CDN使用教程
为什么要自己来搭建一个CDN服务器实现网站访问加速?一是免费CDN服务稳定性和加速效果都不怎么行:二是用国内的付费CDN服务价格贵得要死,一般的草根站长无法承受:三是最现实的问题国内的CDN要求域名B ...
随机推荐
- Unity3D发布WebPlayer时Failed to download data file解决方案
今天发布WebPlayer时, 发现直接打开html是可以正常运行的, 但是通过iis访问的话就会报错: Failed to download data file. 一开始以为是防火墙, 后来发现不是 ...
- zend studio代码字体修改字体和大小.
第一步:进入设置窗口 windows -> preferences 第二步:进入修改字体的选项卡. General -> Appearance -> Colors and ...
- JS 昵称,手机号,邮箱判断
<script type="text/javascript"> var leyou = document.getElementById('J-leyou'), _nam ...
- lightoj1027(期望dp)
有一个迷宫,有n个门,走每个的概率都是相同的 每个门有一个数字,如果是正数ai,那么表示走ai天就能走出迷宫,如果是负数,那么走-ai天会回到原地,然后会忘记之前的事情,继续选择门去走 所以,如果都是 ...
- Objective-C类的使用
Objective-C教程的例题: // // main.m // test // // Created by liwei on 14-10-28. // Copyright (c) 2014年 li ...
- hdu2818行列匹配+排序
题意:给定一个矩阵,矩阵上有的数字是1,有的是0,给定两种操作,交换某两行或者某两列,问是否能置换出对角线为1的矩阵 题解:能够置换出对角线是1的矩形要求有n个1既不在同一行也不再同一列,即行列匹配, ...
- UVA 10831 - Gerg's Cake(数论)
UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...
- InnoDB行格式(compact,redundant)对照
InnoDB行格式分两种格式(COMPACT,redundant)默觉得COMPACT compact的存储格式为 首部为一个非NULL的变长字段长度列表,并且是依照列的顺序逆序放置的,当列的长度小于 ...
- proxy pattern 代理模式
常用的几种代理模式简要说明如下: (1) 远程代理(Remote Proxy):为一个位于不同的地址空间的对象提供一个本地的代理对象,这个不同的地址空间可以是在同一台主机中,也可是在另一台主机中,远 ...
- HTML5管理与实际历史的分析(history物)
HTML5新进入历史的管理,更新history对象允许国家的经营历史更方便. 在现代Web应用.用户"前进"和"退却"button切换历史页面.这使得新的页码不 ...