Nginx+mysql+php-fpm负载均衡配置实例

发布时间:2020-12-30编辑:脚本学堂
介绍一个nginx、mysql、php-fpm环境下配置负载均衡的例子,有需要的朋友,可以参考下。

系统环境如下:
前端nginx:192.168.93.137
后端web1:192.168.0.11
后端web2:192.168.0.12

1、前端nginx配置:
 

复制代码 代码示例:

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.jb200.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.jb200.com;

               location / {
          proxy_pass        http://www.jb200.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;
       }
}

通过upstream 名字 {}定义后端web的负载机器,然后在虚拟主机中通过 proxy_pass http://名字; 来使用upstream,再自定义一下日志格式,以获取用户的IP。

2、后端web配置:
 

复制代码 代码示例:

server
{
  listen       80;
  server_name www.jb200.com;
  index index.html index.php;
  root  /home/www/www.jb200.com;
  access_log  /home/logs/access_www.jb200.com.log;

  if (-d $request_filename){
          rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  }

  error_page   500 502 503 504 404 403 http://www.jb200.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   500 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;
  }
}