location的模式匹配按照优先级由低到高有以下四种:
1:location = URI { }花括号在的规则只对当前URI匹配,如果为目录只对目录匹配
2:location ^~URI { }不用正则表达式进行逐字符匹配
3:location ~* URI{ }不区分大小写花括号在的规则对URI进行模式匹配,URI可以用正则表达式
location ~ URI { }区分大小写花括号在的规则对URI进行模式匹配,URI可以用正则表达式
4:location URI { }花括号中的规则对URI中所有路径包括子目录都匹配
location URI { } 花括号中的规则对URI中所有路径包括子目录都匹配
如 location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
}
所有的以.php结尾的URI都有fastcgi转发到本机的9000端口处理(php监听在9000端口)
不用模式匹配的location定义
location /forum/ {
proxy_pass http://192.168.139.8:8080/bbs/;
}
如访问www.baidu.com/forum/ 则相当于访问后端的http://192.168.139.8:8080/bbs/
使用模式匹配定义location时,后面的http://路径只能写到端口处,不能出现URI
如 location ~* ^/forum {
proxy_pass http://192.168.139.8:8080;
}
不区分大小写匹配所有以forum开头的URI,转发到 http://192.168.139.8:8080/forum
如访问www.baidu.com/forum相当于访问http://192.168.139.8:8080/forum
[[email protected] html]# vim /etc/nginx/nginx.conf
location /forum {
proxy_pass http://192.168.139.8/bbs;
}
[[email protected] html]# service nginx reload
[[email protected] bbs]# vim /var/www/html/bbs/index.html
<h1>Nginx on Backup</h1>
[[email protected] bbs]# service httpd restart
[[email protected] html]# vim /etc/nginx/nginx.conf
location ~ ^/hehe {
proxy_pass http://192.168.139.8;
}
[[email protected] html]# service nginx reload
[[email protected] bbs]# vim /var/www/html/hehe/index.html
<h1>This Backup Server</h1>
[[email protected] html]# service httpd restart
将所有的请求都转发到后端192.168.139.8/bbs/
location / {
proxy_pass http://192.168.139.8/bbs;
}
通过分析后端server的日志可以看到所有的访问client_ip都是来自node2(node2只是个代理服务器,记录他的ip不能进行client来源分析)没有记录真正的client_ip
[[email protected] bbs]# tail /var/log/httpd/access_log
192.168.139.4 - - [24/Dec/2016:18:05:59 +0800] "GET /hehe HTTP/1.0" 301 313 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
192.168.139.4 - - [24/Dec/2016:18:05:59 +0800] "GET /hehe/ HTTP/1.0" 200 28 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
日志变量有:
$remote_addr client_ip
$remote_port client_port
$remote_user client_user (基于用户认证时)
$request_body 请求主题
$request_method 请求方法
put get post delete options trace connection head
$server_addr server_ip
$server_port server_port
$server_name server_name
$server_protol http1.0/1.1
$uri 请求的正真uri
定义日志记录client来源
location ~ ^/hehe {
proxy_pass http://192.168.139.8;
proxy_set_header X-Real-IP $remote_ddr;
}
real-ip由前端代理服务器传过来了,但也要改一下后端server的日志记录格式
[[email protected] html]# vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[[email protected] html]# service httpd restart
浏览器多访问几次 http://192.168.139.4/hehe/
[[email protected] html]# tail /var/log/httpd/access_log
192.168.139.1 - - [24/Dec/2016:18:23:28 +0800] "GET /hehe/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
192.168.139.1 - - [24/Dec/2016:18:23:28 +0800] "GET /hehe/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
192.168.139.1 - - [24/Dec/2016:18:23:28 +0800] "GET /hehe/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
192.168.139.1 - - [24/Dec/2016:18:23:29 +0800] "GET /hehe/ HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
可以看到client_ip 192.168.139.1(而不是node2的IP_192.168.139.4)
Nginx的upstream端定义:将多个server做成一个负载均衡的集群,默认使用wrr调度算法(权重一样则为rr,权重为0 ,则不加入集群)
[[email protected] html]# vim /etc/nginx/nginx.conf
upstream my_web_server { #upstream在server外定义
my_web_server为集群名,要引用 server 192.168.139.8 weight=1;
server 192.168.139.9 weight=1;
}
location / {
root /web/html;
index index.html index.htm;
proxy_pass http://my_web_server/; #转发到my_web_server集群
proxy_set_header X-Real_IP $remote_addr;
}
[[email protected] html]# service nginx reload
刷新
upstream还可以为后端server做健康状态检查,万一两个后端server都挂了,准备一个sorry页面
[[email protected] html]# vim /etc/nginx/nginx.conf
upstream my_web_server {
server 192.168.139.8 weight=1 max_fails=2 fail_timeout=2 ;
server 192.168.139.9 weight=1 max_fails=2 fail_timeout=2 ;
server 127.0.0.1:8080 backup ;
}
server { listen 8080;
server_name localhost;
root /web/error;
index index.html;
}
[[email protected] html]# vim /web/error/index.html
<h1>Sorry......</h1>
[[email protected] html]# service nginx reload
[[email protected] html]# service httpd stop
[[email protected] html]# service httpd stop
[[email protected] html]# service httpd start
[[email protected] ~]# service httpd start
Nginx支持三种负载均衡的调度算法:
1:wrr(weight round robin 加权轮调) 如果权重相同则为rr(轮调),每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。Weight 指定轮询权值,Weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下
2:ip_hash (ip_哈希) server端会将client的访问IP做一个哈希运算,并将结果保存在本地内存中的哈希表中,这样对IP运算结果的相同的client会被始终分发给通一个后端RS_Server,从而不会出现client因为访问的是不同server造成的没有session信息的问题(其实要根本解决session问题,还是要加一个共享存储,比如那台server挂了,这时client请求肯定会发给其他server,可以将session信息保存在memory cache中实现session共享)
3:least_conn (最小连接数)比较当前节点上活动连接数+非活动连接数,输小给谁发,这是一种动态调度算法,如active*256+inactive
注:使用ip_hash时要将backup去掉,万一定向到backup_server上,即使RS_Server恢复正常,也不会再给转发
[[email protected] html]# vim /etc/nginx/nginx.conf
upstream my_web_server {
server 192.168.139.8 weight=1 max_fails=2 fail_timeout=2 ;
server 192.168.139.9 weight=1 max_fails=2 fail_timeout=2 ;
ip_hash;
}
[[email protected] html]# service nginx reload
一直刷新都是定向在node4
为了减轻后端RS的压力,Nginx应该启用本地缓存,其缓存有两种形式
1:在共享内存中,缓存键和缓存对象的元数据(主要用于在内存中查找数据)
2:在磁盘空间中,存储数据(如静态、或者经过静态处理的动态数据)在磁盘中,为了提高效率,可以用SSD作为本地磁盘,且可以将多块SSD做成一个Raid0,那速度老快了
根据请求方法进行缓存
proxy_cache_methods GET HEAD POST;#这三种请求方法的请求进行缓存
根据状态码进行缓存
proxy_cache_valid 200 302 10m ;#状态码为200 302 的请求缓存10min
proxy_cache_valid 404 1m; #状态码为200 的请求缓存1min
proxy_cache_valid any 5m; #其他的状态码缓存5min
根据相同请求的次数进行缓存
proxy_cache_min_uses 5; #只有当相同请求出现5次才对其进行缓存
更详细的缓存介绍请看官网 https://www.nginx.com/resources/admin-guide/content-caching/
proxy_cache_path ; 缓存的保存路径,不能定义在server{ }段中
keys_zone=first:20m; 用来存储键的区域名叫first,大小为20M
max_size=1G ; 最多用1 G的内存进行缓存,如果缓存空间满了,Nginx的cache_manager进程会根据最近最少连接原则进行缓存清除
[[email protected] html]# vim /etc/nginx/nginx.conf
upstream my_web_server {
server 192.168.139.8 weight=1 max_fails=2 fail_timeout=2 ;
server 192.168.139.9 weight=1 max_fails=2 fail_timeout=2 ;
ip_hash;
}
proxy_cache_path /nginx/cache/my_cache levels=1:2 keys_zone=first:20M;
server {
listen 80;
server_name localhost;
add_header X_cache "$upstream_cache_status from $server_addr"
配置文件时;前忘了加"结果一直 nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/nginx.con,差点将整个文件删了^_^
location / {
root /web/html;
index index.html index.htm;
proxy_pass http://my_web_server/;
proxy_set_header X-Real_IP $remote_addr;
proxy_cache first;
proxy_cache_valid 200 10m;
}
.......
}
[[email protected] html]# mkdir -pv /nginx/cache/my_cache
[[email protected] html]# service nginx reload
浏览器访问:192.168.139.4 Ctrl+F5 强制刷新页面,按F12键,弹出开发者页面,点击Network便可看到下面内容,在Response Headers段 HIT from 192.168.139.4 (从192.168.139.4命中)
-
- Request URL:
http://192.168.139.4/
- Request Method:
GET
- Status Code:
304 Not Modified
- Remote Address:
192.168.139.4:80
- Request URL:
- Response Headersview source
- Connection:
keep-alive
- Date:
Sat, 24 Dec 2016 12:31:37 GMT
- ETag:
"dfea9-17-54411d5f3b69e"
- Last-Modified:
Tue, 20 Dec 2016 07:17:58 GMT
- Server:
nginx/1.10.2
- X-cache:
HIT from 192.168.139.4
- Connection:
- Request Headersview source
- Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- Accept-Encoding:
gzip, deflate, sdch
- Accept-Language:
zh-CN,zh;q=0.8
- Cache-Control:
max-age=0
- Connection:
keep-alive
- Host:
192.168.139.4
- If-Modified-Since:
Tue, 20 Dec 2016 07:17:58 GMT
- If-None-Match:
"dfea9-17-54411d5f3b69e"
- Upgrade-Insecure-Requests:
1
- User-Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
- Accept:
删除本地缓存再刷新一次
[[email protected] 5b]# rm /nginx/cache/my_cache/b/5b/d0f1246dc67a25097fa3a295a393f5bb
- Connection:
keep-alive
- Date:
Sat, 24 Dec 2016 12:38:29 GMT
- ETag:
"dfea9-17-54411d5f3b69e"
- Last-Modified:
Tue, 20 Dec 2016 07:17:58 GMT
- Server:
nginx/1.10.2
- X-cache:
MISS from 192.168.139.4 #MISS代表缓存未命中
再刷新一次
- Connection:
keep-alive
- Date:
Sat, 24 Dec 2016 12:39:37 GMT
- ETag:
"dfea9-17-54411d5f3b69e"
- Last-Modified:
Tue, 20 Dec 2016 07:17:58 GMT
- Server:
nginx/1.10.2
- X-cache:
HIT from 192.168.139.4 #缓存又命中了
- miss :缓存未命中
- hit :缓存命中
- expired : 缓存已过期
updating :缓存内容已经更新
stale : 缓存已经失效
除了以上缓存外,还有fastcgi_cache,可以缓存php脚本处理的结果,及php代码编译的opcode,但一般来说动态响应内容往往不一样,只能讲那些经常请求的动态资源进行缓存,这时可以根据最少相同访问次数来设定缓存,fastcgi也有自己的缓存配置(将尽量多的动态资源进行静态化,是一个好的大型站点必须做好的事)
open_log_cache: 还有日志缓存
open_file_cache:将文件的元数据缓存再Nginx的内存中
对于一个大型站点来说,一个集群组可能不能满足其需求,这时就需要多个集群组进行不同的分工处理不同的请求,可以采取如下操作解决
1:专门处理php动态请求的集群
upstream php_servers {
server 192.168.139.11....;
server 192.168.139.12.....;
......
}
2:专门处理图片请求的集群
upstream img_servers {
server 192.168.139.20....;
server 192.168.139.21.....;
......
}
3:处理其他请求的集群
upstream other_servers {
server 192.168.139.30....;
server 192.168.139.31.....;
......
}
定义location,根据URI进行匹配(记住location几种模式匹配优先级奥^_^)
location / {
proxy_pass http://other_servers;
}
location ~* \.php$ {
fastcgi_pass http://img_servers;
}
location ~* "\.(jpg|gpeg|gif|png)$" {
proxy_pass http://php_server;
}