Home  >  Article  >  Operation and Maintenance  >  Website load balancing solution

Website load balancing solution

藏色散人
藏色散人forward
2019-04-13 17:12:054429browse

Web load balancing (Load Balancing), simply put, is to allocate "work tasks" to our server cluster, and using appropriate allocation methods is very important for protecting the back-end web servers.

Website load balancing solution

Reverse proxy load balancing

The core work of the reverse proxy service is mainly to forward HTTP requests, playing the role of the browser and the role of backend web server relay. Because it works at the HTTP layer (application layer), which is the seventh layer of the seven-layer network structure, it is also called "seven-layer load balancing". There are many software that can be used as reverse proxy, and one of the more common ones is Nginx.

Website load balancing solution

Nginx is a very flexible reverse proxy software that can freely customize forwarding strategies, allocate the weight of server traffic, etc. In reverse proxy, a common problem is the session data stored by the web server, because the general load balancing strategy allocates requests randomly. There is no guarantee that requests from the same logged-in user will be allocated to the same web machine, which will lead to the problem that the session cannot be found.

There are two main solutions:

Configure the forwarding rules of the reverse proxy so that requests from the same user must fall on the same machine (by analyzing cookies ), complex forwarding rules will consume more CPU and increase the burden on the proxy server.

It is recommended to use an independent service to store information such as session, such as redis/memchache.

The reverse proxy service can also enable caching. If enabled, it will increase the burden on the reverse proxy and needs to be used with caution. This load balancing strategy is very simple to implement and deploy, and its performance is relatively good. However, it has the problem of "single point of failure". If it hangs, it will cause a lot of trouble. Moreover, as the number of Web servers continues to increase in the later stages, it itself may become a bottleneck of the system.

Configuration file sample:

#user nobody; worker_processes 1; #pid logs/nginx.pid; events { 
    worker_connections 1024; } http { 
    include mime.types; 
    default_type application/octet-stream; 
    sendfile on;  
    keepalive_timeout 65; 
    upstream www.hcoder.net { 
        server 192.168.1.188:80 weight=5; 
        server 192.168.1.158:80; 
    } 
    server { 
    listen 80; 
    server_name www.hcoder.net; 
    location / { 
       proxy_pass http://www.hcoder.net; 
       proxy_set_header Host $host; 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
   } }

The above is the detailed content of Website load balancing solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:hcoder.net. If there is any infringement, please contact admin@php.cn delete