I have written several articles about recent projects. ^
This project was designed to be "very large" at the beginning, taking into account a lot of load balancing things, such as using nginx to allocate php requests to several independent servers running php fastcgi. This designIn addition to saving a copy of the program on each php fastcgi serverAnd there is another problem that needs to be solved urgently. Because php fastcgi processes run on different servers, the default php session data is stored on each server separately. In this way, after a php request, nginx sends the php request to another php fastcgi server in another cluster, which will cause session loss. After a series of considerations, the project team decided to use an independent memcached server to store the php session data of the entire project, and then rewrite the php session processing function to read and write data to the memcached server, then solve the problem of non-synchronization of sessions in various php fastcgi.
The project background is basically the ones mentioned above. Next we will talk about the relevant network environment, and then enter the specific configuration and implementation process:
1. One nginx server, IP Address: 172.16.236.208
2. Two php fastcgi servers with IP addresses 172.16.236.210 and 172.16.236.211 respectively
3. One memcached server with IP Address: 172.16.236.220
In order to simplify the operation process, the above environment is only the assumption that the project needs to be met, and there are many additional processes in the actual project.
First, modify the nginx virtual host configuration and distribute php requests to various php fastcgi servers,
For more information about the configuration process, see my previous article: Use nginx to configure multiple php fastcgi Server Load balancer instances.
Step 2: Install the php-memcache extension for the php fastcgi server. For details, refer to: memcached and php-memcache Installation notes.
In fact, this step only needs to be performed on one server. Other php fastcgi configurations can actually be lazy, that is, the previously compiled memcache. so copy to the server to be configured, and then modify php. ini can load this extension.
Step 3: Modify the php source code of the project so that the php session can be accessed using memcached.
I didn't carefully read the php manual before. I first wrote the session handler and encountered some strange problems. Finally, the simplest method is found in the official memcache instruction of php. In fact, you only need to execute the following code before calling session_start:
1 2 3 4 5 |
$ Host = '1970. 16.236.220 '; $ Port = '000000 '; $ Session_save_path = "tcp: // $ host: $ port? Persistent = 1 & weight = 2 & timeout = 2 & retry_interval = 10, tcp: // $ host: $ port "; Ini_set ('session. save_handler ', 'memcache '); Ini_set ('session. save_path ', $ session_save_path ); |
$ Host is the IP address of the memcached server, and $ port is the port on which memcached listens.
After the following code is executed, all session operations on the php fastcgi server are synchronized to the memcached server. For the php code of the project, how can this problem be used in the past, you don't need to rewrite the php session processing function as you did at the beginning of this blog.
I again lamented that the official instructions of php are very powerful... I shouldn't be so confident in the future. I should go to the official website to look for it carefully ~
Http://hi.baidu.com/pplboy/blog/item/a3a3e6a4008844e59152eecc.html