How to configure nginx for load balancing

(*-*)浩
Release: 2019-07-15 11:03:34
Original
4027 people have browsed it

nginx does load balancing based on reverse proxy

How to configure nginx for load balancing

The code is as follows:

## 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;  
   }  
}
Copy after login

nginx has the following methods for load balancing:

1. RR (default) Each request is assigned to different back-end servers one by one in chronological order. If the back-end server goes down , can be automatically eliminated. For example:

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;
Copy after login

2. ip_hash Each request is assigned according to the hash result of the access IP, so that each visitor has fixed access to a back-end server, which can solve the session problem. For example:

upstream tomcats {   
           ip_hash;    
           server 10.1.1.107:88;    
           server 10.1.1.132:80;    
}
Copy after login

3, fair (third party) Allocate requests according to the response time of the backend server, and those with short response times will be allocated first.

4. url_hash (third party) Allocates requests according to the hash result of the accessed URL, so that each URL is directed to the same back-end server. It is more effective when the back-end server is cached.

For more Nginx related technical articles, please visit the Nginx Usage Tutorial column to learn!

The above is the detailed content of How to configure nginx for load balancing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template