nginx在做负载均衡时如何配置


本文摘自PHP中文网,作者(*-*)浩,侵删。

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

## Basic reverse proxy server ## 

## Apache backend for www.baidu.com ## 

upstream henushang  { 

        # 不过最好换成你们的服务器测试,因为我测试的时候使用jd和baidu的都没有连接成功,         # 换成自己的服务器就行了,估计是那里有限制,如果哪位知道,请指教 

        server www.jd.com weight=1; # 或者ip:port这样形式也是可以的 

    server www.baidu.com weight=9; # 或者ip:port这样形式也是可以的 

   

## Start www.baidu.com ## 

server { 

    listen 80; 

    server_name  www.henushang.cn;#监听的域名 

   

    access_log  logs/henushang.access.log; 

    error_log  logs/henushang.error.log; 

    root   html; 

    index  index.html index.htm index.php; 

   

    ## send request back to apache ## 

    location / { 

        proxy_pass  http://henushang;#与上面的upstream名字相对应 

   

        #Proxy Settings 

        proxy_redirect     off; 

        proxy_set_header   Host             $host

        proxy_set_header   X-Real-IP        $remote_addr

        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for

        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; 

        proxy_max_temp_file_size 0; 

        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; 

   

}

nginx做负载均衡有如下几种方式:

1、RR(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 例如:

1

2

3

upstream tomcats {    

               server 10.1.1.107:88  max_fails=3 fail_timeout=3s weight=9;  

               server 10.1.1.132:80  max_fails=3 fail_timeout=3s weight=9;

2、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 例如:

1

2

3

4

5

upstream tomcats {  

           ip_hash;   

           server 10.1.1.107:88;   

           server 10.1.1.132:80;   

}

3、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。

4、url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

更多Nginx相关技术文章,请访问Nginx使用教程栏目进行学习!

以上就是nginx在做负载均衡时如何配置的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

利用nginx实现301跳转到https的根域名

为什么nginx很快?

centos7环境下如何安装nginx

使用 nginx 反向代理多个 docker 容器

如何修改nginx服务的默认端口

apache和nginx需要一起用吗

nginx反向代理两个不同服务器

怎么更改nginx配置文件路径

教你如何在linux中安装nginx服务器

nginx/1.14.0什么意思

更多相关阅读请进入《nginx》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...