nginx upstream模块和代理讲解

ngx_http_log_module

rewrite regex replacement[flag];
把用户请求的uri基于regex做检查,匹配到时将替换为replacement指定的子字符串
在同一个location中存在的都哦个rewrite规则会自上而下逐个被检查

ngx_http_fastcgi_module
LAMP:
proxy_fastcgi_module
LNMP

nginx缓存k=v结构 内存中存储的K 磁盘中存储的是value

K文件的名字 url

 

http

定义
fastcgi_cache_path /var/cache/nginx/fastcgi levels:1:2:2 keys_zone=fcgicache:10m;
调用

location {
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302;

}

nginx应用实例:

定义proxy缓存

 

调用缓存

例一:

 

例二:

 

ngx_http_proxy_module

正向代理: 一个web程序的代理程序一般代理80,443,21
打开浏览器属性设置代理服务器地址和端口,这种机制就叫正向代理
正向代理支持缓存,设置缓存策略
代表请求者的身份

反向代理:通常工作在服务器的前端,保护服务器安全
基于套接字响应,每个链接维护一个套接字文件
并发能力有限(代理服务器加缓存)

通过location判断请求的url数据来做动静分离
(1)
proxy_pass http://localhost:8080
location ,if in location ,limit_except
proxy_pass后面的路径不带uri时,其余将location的uri传递给后端主机
location /uri/{
proxy_pass http://host;
}

proxy_pass后面路径是一个uri时,其会将location的uri替换为proxy_pass后端主机的uri
location /uri/{
proxy_pass http://host/new_uri;

}

如果location定义其uri使用正则模式匹配时,则proxy_pass后d路径必须不能使用uri
location ~|~* pattern {
proxy_pass http://host;
}


实例:

location /bbs/ {
proxy_pass http://192.168.1.1/;

}

location ~*\.(jpg|gif|png)$ {
proxy_pass http://192.168.1.1;
}
请求资源的动静分离:
nginx做代理服务器,location匹配规则,所有静态资源发给静态资源主机
所有php结尾的发给php服务

proxy_set_header X-Rreal-ip $remote_addr; #设定向后端主机发送的请求报文的首部及其值
proxy_set_header X-Forwarded-For $proxy_addr_x_forwared_for;


缓存相关的选项(先定义后调用)
内存:key-value
url--->md5
磁盘:
2 2
定义缓存
proxy_cache_path /var/cache/nginx/proxy leves=1:2:2 keys_zone=pcache:10m inactive=365 max_size=10g
调用缓存
location中添加
proxy_cache_key pcache;
proxy_cache_key $request_uri;
proxy_cache_key $request_uri$scheme$proxy_host;
proxy_cache_valid 302 302 10m;
proxy_cache_use_stale error;

ngx_http_proxy_module
proxy_connect_timeout 30s;
后端服务器建立链接的超时时长,默认60s,最长75s
proxy_read_timeout 60s
等待后端主机发送响应报文的超时时长,默认60s
proxy_send_timeout 60;
向后端服务器发送请求报文超时时长,默认60s

ngx_http_headers_module
{http,server,location,if}

(1)add_header X-via $server_addr
(2)expires [modified] time;
expires epoch |max|off;
ngx_http_upstream_module
将多个后端主机定义一个组
用于http

upstream name{...}
定义后端服务器组,引入新的上下文,只能用于新的httpd
name :名称,字符串
servce
server address [参数]
定义服务器的地址和相关的参数:
地址格式:
ip[:port]
hostname[:port]
参数:
wegiht=number 权重
max_fails=number 最大错误尝试次数
fail_timeout=time 设置服务器不可用的超时时长
backup:备用主机
down:维护模式(手动标记,不再处理任何请求)(查看系统有没有tcp连接)

实例:
upstream webserver {
ip hash;
server 192.168.1.1:80 weight=1 max_fails=3;
server 192.168.1.2:80
#sticky cookie srv_id expires=1h path=/;
}
调用upsteram
proxy_pass http://webserver;

ip_hash
源地址哈希,调度算法;(客户端ip)
least_conn
最少链接调度算法;
health_check;
定义后端主机的健康状态检测
可用参数:
interval= 检测的频度,默认5s
fails=number 判断为失败的检测次数
passes=number:判断为成功的检测次数
match=name 基于那个match做检测结果为"成功"or"失败"的判定
port=number 向服务器的那个端口发起健康状态检测请求
match name {...}
仅能用户http上下文,对后端主机做健康状态检测时,定义其结果判断标准
专用指令
status:期望的响应码
status code
status !code
status code-code
header:基于响应首部进行判断
heard HEADER=VALUE
heard HEADER!=VALUE
header

 

 

定义

match health {
status 200;
body ~"ok";
}
通常用于location
health_check match=health interval=2 fails=3 uri=/.health.html;

hash key [consistent]
定义调度方法,可自定义基于何种信息key进行绑定
hash $remote_addr $request_uri
add_header X-cache $upstream_cache_status

 

posted @ 2017-03-06 20:54  ShinyBoy1  阅读(440)  评论(0编辑  收藏  举报