How does PHP implement load balancing? PHP Implementation Load Balancer instance (code)

Source: Internet
Author: User
Tags php session php load balancing version control system
There are many ways to load balance php, here, I will introduce you to PHP load balancing, if not much to say, we will take a direct look at the implementation of PHP load balancing.

1. php files

The first question is, if you have a large number of small servers, how do you upload your PHP files to all the servers? Here are some of the ways you could refer to:

1. Upload all the files to each server, this method brings the problem is: Imagine you have 20 servers, then the upload process this will easily lead to errors, and the update is very likely to lead to different versions of the file on the server.
2. Use ' rsync ' (or similar software). Such a tool can synchronize files on the local directory and on multiple remote host directories.
3. Use version control software (such as subversion). This is my favorite method. It's good to maintain my code, and when I publish my app, I can run the Svnupdate command synchronization on every server. This approach also makes it easier to switch the server to a previous version of the Code.
4. Use a file server (you may find NFS very well suited to do this thing). This approach is to use a file server to store your Web app. Of course, if your file server goes down, then more of all your sites will not be available. At this point, you need to spend more money to recover it.

Choose which method depends on your needs and the skills you have mastered. If you use a version control system, you may have to plan a method to update the code on all servers if you execute an update command at the same time. However, if you are using a file server, you will implement some failure recovery mechanisms to prevent requests from failing in the event of server downtime.

2. File Upload

File uploads are not an issue when there is only one server. But when we have more than one server, then how should the uploaded files be stored? The issue of uploading files is similar to the storage of PHP files across servers. Here are a few possible scenarios:
1. Store the files in the database.

Most data allows for the storage of binary data. When you request a file download, the Access data outputs the binary data and the corresponding file name and type to the user. Before using this scenario, you should consider how the database stores your files. The problem with this approach is that if the database server goes down it will make the file unusable.

2. Store the uploaded file on a file server.

As with the previous introduction, you will install a file server for all Web servers to share, upload all uploaded files here, upload all the Web server can use it. However, if the file server goes down, an image file download may be interrupted.

3. Design your own upload mechanism to transfer files to the server to each server.

This method does not have a single file server or database schema flaw, but will increase the complexity of your code. For example, what do you do if the server goes down while uploading to multiple servers?

Uploading files with database storage but designing a file caching mechanism is a good solution. When the server receives a file download request, it first checks to see if the file is in the cache system, and if it is found to be downloaded from the cache system, it is read from the database and cached to the file system.

3. Session (Sessions)

If you are familiar with PHP session processing, you will probably know by default that it stores session data in the server's temp file. Also, this file is only on the server you requested to process, but the next request may be processed by another server, which will generate a new session on the other server. This causes the session to be frequently unrecognized, such as the logged-on user always requires a re-login.

My recommendation is to either re-create PHP's built-in session processing mechanism to store session data to the database, or implement your own mechanism to ensure that a user's request is sent to the same server.

4. Configuration (config)

Although this topic is not specifically related to PHP, I feel it is necessary to mention. When running a clustered server, it is a good idea to keep profile synchronization between servers in some way. If the configuration file is inconsistent, it can cause some very strange intermittent behavior that makes it difficult to troubleshoot these issues.

I recommend using the version control system to manage them separately. This allows you to store different PHP configuration files for different project installations, or to keep all server configuration files synchronized.

5. Log (Logging)

Like configuration problems, logging is not just about PHP. But it is still very important to keep the server running healthily. Without the right logging system, how do you know if PHP code starts to generate errors (you always turn off the display_errors setting when the system is officially running, don't you?)

There are several ways you can implement logging:

1. Log logging on each server. This is the simplest method. Each machine only records one file. The benefits are simple, possibly as long as there are few configurations. However, as the number of servers increases, it becomes very difficult to monitor log files on each server.
2. Logging to a share this method is still available for each server, but they are stored on a central file server through a sharing mechanism, which makes monitoring logs easier. The problem with this scenario is that if the file server is unavailable, a simple log cannot be written to the problem that eventually causes the entire application to crash.
3. Logging to logging server you can use a logging software, such as a syslog, to write all the logs to a central server. Although this method requires more configuration, he also provides the most robust solution.

PHP Load Balancer Instance

If you want to use load balancing, you can modify the configuration HTTP node as follows:

#设定http服务器, using its reverse proxy feature to provide load balancing support for HTTP {#设定mime类型, the type is defined by the Mime.type file include/etc/nginx/            Mime.types;            Default_type Application/octet-stream; #设定日志格式 Access_log/var/log/nginx/access.log, #省略上文有的一些配置节点 # .... #设定负载均衡的服务器列表 upstream Mysvr {#weigth参数表示权值, the higher the weight, the greater the probability of being assigned to the server 192.168.8.1x:3128 weigh            t=5; #本机上的Squid开启3128端口 server 192.168.8.2x:80 weight=1;            Server 192.168.8.3x:80 weight=6;            }upstream MYSVR2 {#weigth参数表示权值, the higher the weight, the greater the probability of being assigned to the server 192.168.8.x:80 weight=1;            Server 192.168.8.x:80 weight=6;            #第一个虚拟服务器 server {#侦听192.168.8.x 80-Port Listen 80;      server_name 192.168.8.x; #对aspx后缀的进行负载均衡请求 location ~. *\.aspx$ {root/root;   #定义服务器的默认网站根目录位置 index index.php index.html index.htm; #定义首页索引文件的名称proxy_pass http://mysvr; #请求转向mysvr defined list of servers # Here are some of the reverse generationscan be deleted. Proxy_redirect off; #后端的Web服务器可以通过X-forwarded-for get the user real IP proxy_set_header Host $host;            Proxy_set_header X-real-ip $remote _addr;            Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;    Client_max_body_size 10m;  #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数, Proxy_connect_timeout 90;        #nginx跟后端服务器连接超时时间 (proxy connection timeout) Proxy_send_timeout 90;         #后端服务器数据回传时间 (proxy send timeout) proxy_read_timeout 90;             #连接成功后, back-end server response time (proxy receive timeout) Proxy_buffer_size 4k;               #设置代理服务器 (Nginx) Save the user header information buffer size Proxy_buffers 4 32k;    #proxy_buffers缓冲区, the average page below 32k, so set proxy_busy_buffers_size 64k;  #高负荷下缓冲大小 (proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小, greater than this value, will be passed from the upstream server}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.