Nginx 文档阅读记录

摘要:中文文档 Nginx 功能概述 --|Http 基础功能: 1. 处理静态文件 2. 反向代理加速(无缓存),简单的负载均衡和容错。 3. 模块化结构 4.  SSL 和TLS SNI支持 --|IMAP/POP3代理服务功能: --|其他Http 功能: 1. 基于IP和名称的虚拟主机服务 2. 基于客户端IP地址和HTTP基本认证的访问控制 3.支持FLV(Flash视频

中文文档

Nginx 功能概述

--|Http 基础功能:

1. 处理静态文件

2. 反向代理加速(无缓存),简单的负载均衡和容错。

3. 模块化结构

4. SSL 和TLS SNI支持

--|IMAP/POP3代理服务功能:

1 (84).jpg

--|其他Http 功能:

1. 基于IP和名称的虚拟主机服务

2. 基于客户端IP地址和HTTP基本认证的访问控制

3.支持FLV(Flash视频)

补充:Nginx 可以作为Web服务器、负载均衡服务器、邮件代理服务器。

其中,我使用它作为Tomcat的HTTP代理服务器。

Nginx主模块

include

配置文件的包含:

include vhosts/*.conf;

http 核心模块

alias

location /i/ { alias /spool/w3/images/; }

表示请求"/i/top.gif"将返回文件: "/spool/w3/images/top.gif".

default_type

指定标准MIME-type以外的MIME 类型。

location = /proxy.pac { default_type application/x-ns-proxy-autoconfig; }

index

指定index 的文件

internal

error_page 404 /404.html; location /404.html { internal; }

此位置只处理"internal"请求,如:

requests redirected by the instructionerror_page

subrequests created by the commandinclude virtualof the "ngx_http_ssi_module" module

requests changed by the instructionrewriteof the "ngx_http_rewrite_module" module

Nginx 0.7.x 引入了新的internal locations:@Location

location / { root /var/www/html; error_page 404 @40x; } location @40x { root /var/www/errors/40x.html; }

location

1. ~*大小写不敏感

2. ~ 大小写敏感

匹配顺序:

~ 先匹配一部分query,搜索不停止。

然后搜索正则,正则匹配了,就停止搜索。

如果没有正则匹配,则搜索字面量。

匹配修正:

“=” 前缀:仅匹配额外的query,如果匹配则搜索停止。比如query - ”/"频繁出现,可以使用"location = /" 去处理这类请求。

"^~"前缀:表示部分匹配后不再继续搜索。比如"location ^~/images/",表示以 "/images"开头的 query 即匹配此location,不再继续搜索正则。

location = / { # matches the query / only. [ configuration A ] } location / { # matches any query, since all queries begin with /, but regular # expressions and any longer conventional blocks will be # matched first. [ configuration B ] } location ^~ /images/ { # matches any query beginning with /images/ and halts searching, # so regular expressions will not be checked. [ configuration C ] } location ~* .(gif|jpg|jpeg)$ { # matches any request ending in gif, jpg, or jpeg. However, all # requests to the /images/ directory will be handled by # Configuration C. [ configuration D ] }

针对上述配置,可能的匹配结果:

/ -> configuration A

/documents/document.html -> configuration B

/images/1.gif -> configuration C

/documents/1.jpg -> configuration D

root

location /i/ { root /spool/w3; }

比如对于 请求"/i/top.gif" 将返回文件"/spool/w3/i/top.gif". 注意比较alias,这里会匹配"i"目录到query中。

server

配置 virtual server

指令listen 用于配置 addresses和ports

指令server_name用于配置 server_name

注意:如果 host和server都匹配的话,host将匹配。

server的搜索顺序:

全名

前通配符 *.example.com

后通配符 — www.example.*

正则匹配

types

types { text/html html; image/gif gif; image/jpeg jpg; }

The sufficiently complete table of mappings is included and is located in the file conf/mine.types

匹配所有类型,可以使用 types { }

location /download/ { types { } default_type application/octet-stream; }

变量

支持apache 内置变量,如:$http_user-agent,$http_cookie,and so forth.

还支持

$arg_PARAMETER 表示GET 请求的中变量PARAMETER的值。

$args/$query_string 请求行的参数

$content_length

$content_type

$cookie_COOKIE

$document_root 效果等同于root指令,但只针对当前请求。

$document_uri/$uri

$host 等同于,用于 host头不可得时。

$http_HEADER

...

HttpProxy模块

此模块专伺将请求导向其它服务.

It is an HTTP/1.0 proxy without the ability for keep-alive requests yet.

补充:无请求保持,可能需要多次建立Tcp连接。无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。

HTTP 协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。服务器没有保存客户端的状态信息吧,客户端必须每次带上自己的状态去请求服务器(比如cookie、session等)。另一方面,在服务器不需要先前信息时它的应答就较快。

示例 location / { : proxy_pass http://localhost:8000; : proxy_set_header X-Real-IP $remote_addr; }