Go NGINX+MYSQL+PHP-FPM Load Balancer Configuration Example

Source: Internet
Author: User
Tags fpm

Ext.: http://www.jbxue.com/article/7923.html

This paper introduces an example of configuring load balancing in Nginx, MySQL, PHP-FPM environment, and the friends who need it can be consulted.

The system environment is as follows:
Front nginx:192.168.93.137
Back end web1:192.168.0.11
Back end web2:192.168.0.12

1, the front-end nginx configuration:

Copy Codecode example:

HTTP {
......
Client_max_body_size 300m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 600;
Proxy_read_timeout 600;
Proxy_send_timeout 600;
Proxy_buffer_size 16k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;

Upstream www.jbxue.com {
#server 192.168.93.137:80;
Server 192.168.0.11:80;
Server 192.168.0.12:80;
}
Upstream www.jbxue007.com {
#server 192.168.93.137:80;
Server 192.168.0.11:80;
Server 192.168.0.12:80;
}
Server
{
Listen 80;
server_name www.jbxue.com;

               location/{
           proxy_pass       /HTTP/ www.jbxue.com;
          proxy_set_header   host              $host;
          proxy_set_header   x-real-ip         $remote _addr;
          proxy_set_header   x-forwarded-for  $ Proxy_add_x_forwarded_for;
              }

Log_format Jbxue ' $remote _addr-$remote _user [$time _local] $request '
"$status" $body _bytes_sent "$http _referer"
' "$http _user_agent" "$http _x_forwarded_for";
Access_log/home/logs/www.jbxue.log Jbxue;
}

Server
{
Listen 80;
server_name www.jbxue007.com;

               location/{
           proxy_pass       /HTTP/ www.jbxue007.com;
          proxy_set_header   host              $host;
          proxy_set_header   x-real-ip         $remote _addr;
          proxy_set_header   x-forwarded-for  $ Proxy_add_x_forwarded_for;
              }

Log_format jbxue007 ' $remote _addr-$remote _user [$time _local] $request '
"$status" $body _bytes_sent "$http _referer"
' "$http _user_agent" "$http _x_forwarded_for";
Access_log/home/logs/www.jbxue.log jbxue007;
}
}

The load machine of the backend web is defined by the upstream name {}, and then the Proxy_pass http://name is passed in the virtual host; To use upstream, and then customize the log format to get the user's IP.

2. Back-end Web configuration:

Copy Codecode example:

Server
{
Listen 80;
server_name www.jbxue.com;
Index index.html index.php;
root/home/www/www.jbxue.com;
Access_log/home/logs/access_www.jbxue.com.log;

if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $/HTTP $host/$1$2/permanent;
}

Error_page 502 503 504 404 403 http://www.jbxue.com;

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $ {
Expires 30d;
}

Location ~. *\. (JS|CSS)? $ {
Expires 6h;
}

Location ~. *\. (Log|txt) $
{
Deny all;
}

Location ~. *\. (PHP)? $
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include fcgi.conf;
}
}

Server
{
Listen 80;
server_name www.jbxue007.com;
Index index.html index.php;
root/home/www/www.jbxue007.com;
Access_log/home/logs/access_www.jbxue007.com.log;

if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $/HTTP $host/$1$2/permanent;
}

Error_page 502 503 504 404 403 http://www.jbxue007.com;

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $ {
Expires 30d;
}

Location ~. *\. (JS|CSS)? $ {
Expires 6h;
}

Location ~. *\. (Log|txt) $
{
Deny all;
}

Location ~. *\. (PHP)? $
{
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Include fcgi.conf;
}
}

Attached, the nginx load of five modes, namely the upstream mode.

1. Polling (default)
Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.

2, Weight
Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.

Copy Codecode example: Upstream www.jbxue.com {
Server 192.168.0.11 weight=10;
Server 192.168.0.12 weight=10;
}

3, Ip_hash
Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.

Copy Codecode example: Upstream www.jbxue.com {
Ip_hash;
Server 192.168.0.11:80;
Server 192.168.0.12:80;
}

4. Fair (third party)
The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.

5. Url_hash (Third Party)
Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.

Copy Codecode example: Upstream www.jbxue.com {
Server 192.168.0.11:80;
Server 192.168.0.12:80;
Hash $request _uri;
Hash_method CRC32;
}

The use of more than should be the 3rd, 5 of these two.
The backend Web file synchronization can choose NFS, rsync, and so on.

Load-balanced nginx+ multiple units of PHP-FPM

Https://code.google.com/p/sna/wiki/NginxWithPHPFPM

http://hyf.zjmp.com/article.asp?id=683

Related Article

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.