服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - 浅谈Nginx七层反向代理和负载均衡

浅谈Nginx七层反向代理和负载均衡

2019-12-25 14:05君醉 Nginx

这篇文章主要介绍了浅谈Nginx七层反向代理和负载均衡,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1.介绍

1.1 Nginx不仅是一个出色的web软件,其七层代理和负载均衡也是相当出色。Nginx做前端代理,当用户请求服务时,可以根据url进行判断,然后分配到不同的后台webserver上。

1.2 Nginx的负载均衡实现原理:首先在http模块中配置使用upstream模块定义后台的web server的池子,名为proxy-web,在池子中我们可以添加多台后台webserver,其中状态检查、调度算法都是在池子中配置;然后在serverr模块中定义虚拟主机,但是这个虚拟主机不指定自己的web目录站点,它将使用location匹配url然后转发到上面定义好的web池子中,最后根据调度策略再转发到后台web server上

2.负载均衡配置项的介绍

2.1 upstream调度算法介绍

(1)rr轮询(默认)

按照请求顺序分配到每个RS,和lvs中的rr算法一样,如果RS宕机,会自动剔除,默认情况下只检测80端口,如果RS报402、403、503、504错误,会直接返回给客户端。

(2)weight(权重)

在rr的基础上再加上权重(默认是rr+weight),权重轮询和访问成正比,值越大分配的越多,可以根据服务器的配置设置权重,可以解决服务器性能不均进行请求分配的问题

(3)ip_hash

解决动态网页session共享问题

每个访问请求按照IP地址的hash值进行分配,ip的hash值只要相同就会被分配到同一台服务器上(lvs负载均衡的-p参数,keepalived配置里的persistence_timeout 50),该调度算法可以解决动态网页session共享问题,但有时会导致请求分配不均,

提示:由于国内用的都是nat模式,所以hash不适合使用

ip_hash不能和其他的算法一块使用,即不能使weight或backup

(4)fair(第三方)

按照后端服务器的响应时间来配置,响应时间短的优先分配,比上面的都更智能,此种算法可以按照页面大小和加载时间长短智能的进行负载均衡,nginx本身不支持fair,需要下载nginx的upstrea_fair模块

(5)url_hash(第三方)

主要应用于缓存服务器上

按照访问的url来分配请求,让相同的url定向到同一个服务器,后端服务器为缓存服务器的时候效果更显著,在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

缺点:如果有一台机器宕机了,那就苦了,consistent_hash可以解决这个问题

可以提高后端缓存服务器的效率,nginx本身不支持url_hash的,需要下载hash软件

(6)least_conn

最少连接数,哪个连接少就分配到哪台设备

(7)consistent_hash

 一致性算法

2.2 upstream健康检查配置介绍

?
1
2
3
4
5
6
7
8
9
10
11
upstream proxy_nginx {
 
        server 192.168.0.254 weight=1 max_fails=2 fail_timeout=10s ;
 
        server 192.168.0.253 weight=2 max_fails=2 fail_timeout=10s;
 
              server 192.168.0.252 backup;
 
              server 192.168.0.251 down;
 
    }

server 192.168.0.254 : 后台RS,可以是域名或IP,默认是80端口,也可加上:80指定

wight = 1 权重比 默认是1

max_fails=2 健康检查的最大失败次数,超过此次数表示该RS不可用,默认是1,0表示禁止失败尝试。生产环境一般设置2~3次

fail_timeout=10s  失败的超时时间,默认是10s

backup  热备配置,当前面的RS全部不可用时自动启动

down  表示该服务永远不可用

注意:max_fails设置的越低用户体验越好,但是设置低了也有个缺点,就是proxy可能会误判RS的状态,而且RS越少误判的几率越大,误判会对业务产生巨大影响,当RS的数量比较少时建议将该值设置的大点。

2.3 location指令的用法介绍

Location主要用来匹配url,如:http://www.beyond.com/nice,在这里对于location来说www.beyond.com是域名,/nice才是url。

对于url的匹配,可以使用字符串或者正则表达式,但如果是正则表达式,必须指定前缀,location指令来匹配不同的url,匹配成功后应用不同的配置

语法:location [=|~|~*|^~|@]/url {…..}

[=]精确匹配,如果找到匹配等号的内容,立即停止搜索,并立即处理请求(优先级最高)

[~] 表示匹配正则表达式,区分大小写

[^~]只匹配字符串,不匹配正则表达式,主要用来匹配目录

[~*]表示匹配正则表达式,不区分大小写

[@]指定一个命名的location,一般只应用于内部重定向请求,location @name {···}

示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
worker_processes 1;
 
events {
  worker_connections 1024;
}
 
http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile    on;
  keepalive_timeout 65;
  server {
    listen    80;
    server_name <a href="http://www.beyond.com;#No.1" rel="external nofollow">www.beyond.com;
 
#No.1
 
</a>    location / {
        return 502;
    }
 
#No.3
    location ~* \.jpg$ {
        return 403;
    }
 
#No.4
    location ^~ /a/ {
        return 402;
    }
 
#No.5
    location /a/1.jpg {
        return 401;
    }
 
#No.6
    location = /a/.jpg {
        return 400;
    }
}
}

小结:

匹配顺序:

1)优先先匹配普通url,但是匹配完之后会继续匹配正则

内部匹配规则为最大匹配前缀,即location /data/123 {} 要比 location /data {} 优先,和location的位置顺序没关系。匹配完之后会用最大匹配前缀 继续去匹配下面的正则匹配

2)正则匹配,

  正则匹配跟位置顺序无关,但是和逻辑顺序有关,会以最大匹配为准(越精确越优先)

3)location的执行逻辑基本上跟顺序没有关系;但是针对正则匹配的方式,匹配上第一个url,就不在继续匹配后面的url;

这种情况,如果匹配上普通localtion,没有正则匹配,则使用普通匹配;如果既有普通location的最大前缀匹配,也有正则匹配,则正则匹配覆盖最大前缀匹配。

4)匹配完“普通 location ”后,有的时候需要继续匹配“正则 location ”,有的时候则不需要继续匹配“正则 location ”。

两种情况下,不需要继续匹配正则 location :(1) 当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普通 location 一旦匹配上,则不需要继续正则匹配;(2) 当普通location 恰好严格匹配上,不是最大前缀匹配,则不再继续匹配正则。

2.4 proxy模块的介绍

Nginx转发模块是ngx_http_proxy_module,默认安装,可以直接使用

Proxy_pass此指令用于转发location匹配到的url到serve池子中。

语法:proxy_pass URL;

注意:使用该指令时需注意URL中是否包含URI。,如果URL中不包含URI,nginx将不会改变原地址的uri,如果包含uri,将使用新的uri代替原来的uri。

如:

?
1
2
3
4
location /test {
#1 proxy_pass http://1.1.1.1;
#2 proxy_pass http://1.1.1.1/tmp;
}

如果使用1的话,原来的url将不会改变,代理后是http://1.1.1.1/test

如果使用2的话,原来的url将会变为http://1.1.1.1/tmp

所以在配置的时候需要注意url末尾是否加“/”的问题

参数:

proxy_ignore_client_abort  on|off用于设置客户端中断网络请求时,nginx是否中断对被代理服务器的请求,默认为off,中断

proxy_headers_hash_max_size  size 设置http报文头的哈希表的大小,默认为512

proxy_headers_hash_bucket_size size 设置申请存放http报文头的哈希表容量的单位大小,默认为64字符

client_body_buffer_size  客户端请求缓存大小,可以理解为先保存本地在传给用户

proxy_connect_timeout time  与RS链接超时的时间,默认为60s

proxy_send_timeout time  RS回传数据的时间,必须在这个时间段内传完,否则断开连接

proxy_read_timeout time  等待RS响应的时间,标明连接已经成功,正在排队

proxy_buffering on|off 是否开启proxy buffer 默认为on

proxy_buffer_size 缓存区大小,默认等于指令proxy_buffers设置的大小 默认为4K或8K

proxy_buffers number size  缓冲区的数量和大小,从RS获取的响应信息会放置到缓冲区,默认为8 4K|8K

proxy_busy_buffers_size  系统很忙时可以使用的proxy_buffers大小,官方推荐的大小是proxy_buffers的两倍,默认为8K或16K

proxy_temp_path path [level1[level2]] 指定磁盘上的一个文件路径,用于临时存放代理服务器的大体积响应数据,如果buffer已经装满,但是响应数据仍然没有被nginx完全接收,响应数据就会被临时存放到该文件中

proxy_max_temp_file_size  用于配置所有临时文件的总体积大小

proxy_temp_file_write_size  用于配置同时写入缓存临时文件的数据量大小
proxy_set_header host $host 当RS有多个虚拟主机的时候需要逐个指定

proxy_set_header X-Forwarded-For $remote_addr   开启负载均衡器转发真实客户的IP地址给RS.   

注意:

“proxy_set_header”当我们的RS有多个虚拟主机(相同的ip,相同的端口)的时候如www、bbs、blog,代理服务器怎么知道将请求发到哪呢,这个时候nginx代理就会查找proxy_set_header参数,将请求发送到相应域名的虚拟主机上。

3.nginx负载均衡配置案例

1.有三个域名:www.beyond.com bbs.beyond.com film.beyond.com,四台webserver负责提供服务

2.有一个mailserver和cloud server在一台服务器上

规划:

web1   192.168.254.251    BBS、film

web2   192.168.254.252     BBS、film

web3   192.168.254.253       BBS、film

web4   192.168.254.254     www,负载均衡器

web5   192.168.254.250      mail   cloud

说明:web4是负载均衡器,同时也负责解析www站点。web1、web2、web3负责解析bbs和film站点。Mail和cloud在同一台设备上,也是web站点

Nginx的安装略了,有需要的可以看此教程 http://www.zzvips.com/article/37930.html

现在列出web4的nginx的配置文件供参考

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
nginx.conf:
user nginx;
worker_processes 4;
error_log logs/error.log;
pid    logs/nginx.pid;
 
events {
 
  use epoll ;
 
    worker_connections 1024;
 
http {
 
   include    mime.types;
 
   default_type application/octet-stream;
 
   server_tokens off;
 
   sendfile    on;
 
   tcp_nopush    on;
 
   keepalive_timeout 65;
 
   fastcgi_connect_timeout 300;
 
   fastcgi_send_timeout  300;
 
   fastcgi_read_timeout  300;
 
   client_header_buffer_size   32k;
 
   large_client_header_buffers 4 128k;
 
   client_max_body_size 10m;
 
   gzip on;
 
   gzip_min_length 1k;
 
   gzip_buffers   4 8k;
 
   gzip_http_version 1.1;
 
   gzip_comp_level 6;
 
   gzip_vary on;
 
   gzip_types    text/javascript text/plain application/x-javascript text/css application/xml;
 
  log_format main ' $http_host $http_x_forwarded_for ${request_time}s [$time_local] "$request" $status $body_bytes_sent $http_referer $http_user_agent $remote_addr';
 
    include vhosts/*.conf;         #每个虚拟主机一个配置文件
 
    include upstream.conf;            #存放web的后台server
 
     fastcgi_intercept_errors on;
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Vhosts/{bbs,film,www,cloud,mail}.conf:
 
 server {
 
    listen    80;
 
    server_name bbs.beyond.com;
 
    index index.php;
 
    location / {
 
    proxy_pass http://web;
 
    proxy_set_header host $host;
 
    proxy_set_header X-Forwarded-For $remote_addr;
 
    client_body_buffer_size  4K;
 
    proxy_connect_timeout  90;
 
    proxy_send_timeout  90;
 
    proxy_read_timeout  90;
 
    proxy_buffer_size  4K;
 
    proxy_buffers   4 32K;
 
    proxy_busy_buffers_size 64K;
 
    proxy_temp_file_write_size 64K;
 
}
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server {
 
    listen    80;
 
    server_name cloud.beyond.com;
 
    index index.php;
 
    location / {
 
    proxy_pass http://192.168.254.250:8000;
 
    proxy_set_header host $host;
 
    proxy_set_header X-Forwarded-For $remote_addr;
 
    client_body_buffer_size  4K;
 
    proxy_connect_timeout  90;
 
    proxy_send_timeout  90;
 
    proxy_read_timeout  90;
 
    proxy_buffer_size  4K;
 
    proxy_buffers   4 32K;
 
    proxy_busy_buffers_size 64K;
 
    proxy_temp_file_write_size 64K;
}
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
 
    listen    80;
 
    server_name film.beyond.com;
 
    index index.php;
 
    location / {
 
    proxy_pass http://web;
 
    proxy_set_header host $host;
 
    proxy_set_header X-Forwarded-For $remote_addr;
 
    client_body_buffer_size  4K;
 
    proxy_connect_timeout  90;
 
    proxy_send_timeout  90;
 
    proxy_read_timeout  90;
 
    proxy_buffer_size  4K;
 
    proxy_buffers   4 32K;
 
    proxy_busy_buffers_size 64K;
 
    proxy_temp_file_write_size 64K;
 
}
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server {
 
    listen    80;
 
    server_name mail.beyond.com;
 
    index index.php;
 
    location / {
 
    proxy_pass http://192.168.254.250:80;
 
    proxy_set_header host $host;
 
    proxy_set_header X-Forwarded-For $remote_addr;
 
    client_body_buffer_size  4K;
 
    proxy_connect_timeout  90;
 
    proxy_send_timeout  90;
 
    proxy_read_timeout  90;
 
    proxy_buffer_size  4K;
 
    proxy_buffers   4 32K;
 
    proxy_busy_buffers_size 64K;
 
    proxy_temp_file_write_size 64K;
}
 
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {
 
    listen    80;
 
    server_name monitor.beyond.com;
 
    index index.php;
 
    location / {
 
    proxy_pass http://192.168.254.220;
 
    proxy_set_header host $host;
 
    proxy_set_header X-Forwarded-For $remote_addr;
 
    client_body_buffer_size  4K;
 
    proxy_connect_timeout  90;
 
    proxy_send_timeout  90;
 
    proxy_read_timeout  90;
 
    proxy_buffer_size  4K;
 
    proxy_buffers   4 32K;
 
    proxy_busy_buffers_size 64K;
 
    proxy_temp_file_write_size 64K;
 
 
 
}
 
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
 
   listen    80;
 
   server_name www.beyond.com;
 
   root /usr/local/nginx/html/www;
 
   index index.html;
 
   access_log logs/current/www.beyond.com-access.log main;
 
   error_log logs/current/www.beyond.com-error.log;
 
    error_page  500 501 502 503 504 /error/5-error.html; 
 
   error_page 400 403 404 405 408 410 411 412 413 414 415 /error/4-error.html;
 
 
 
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
 
       expires 30d;
 
   }
 
 
 
   location ~.*\.(js|css|javascript|fluash)$ {
 
       expires 24h;
 
   }
 
   location /error {
 
   root /usr/local/nginx/logs/error;
 
   }
 
 
 
 }

注意每个server是一个.conf的文件

upstream.conf文件:

?
1
2
3
4
5
6
7
8
9
upstream web {
 
server 192.168.254.253 weight=1 max_fails=2 fail_timeout=10s ;
 
server 192.168.254.252 weight=1 max_fails=2 fail_timeout=10s ;
 
server 192.168.254.251 weight=1 max_fails=2 fail_timeout=10s ;
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/pycode/p/6588910.html

延伸 · 阅读

精彩推荐