Nginx+apache+redis 负载均衡 session 共享配置

 

原理图:

 Nginx+apache+redis 负载均衡 session 共享配置

 

服务器:

  

   反向代理服务器 nginx:

IP:      192.168.1.190 

 

 

   Web服务器  两台 apache  

    

   Apache1:

    配置:apache + php  (php安装redis扩展)

IP:     192.168.1.191

 

   Apache2:

配置:apache + php  (php安装redis扩展)

 

IP:     192.168.1.192

  

 

   缓存服务器   redis:

  

IP:     192.168.1.6

 

配置:

 

Nginx配置文件 :  nginx.conf 

 

主要配置代码:

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format main  '$remote_addr - $remote_user[$time_local] "$request" '

    #                  '$status $body_bytes_sent"$http_referer" '

    #                  '"$http_user_agent""$http_x_forwarded_for"';

 

    #access_log logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

 

   

# 反向代理  lihao.test.nginx.jifenyun.com域名

upstream lihao.test.nginx.jifenyun.com {

         

                   server192.168.1.191:80 weight=10;    #apache1   weight 代表权重设置

                   server192.168.1.192:80 weight=10;    # apache2   weight 代表权重设置

 

    }

         

    #gzip  on;

 

    server {

        listen       80;

       server_name lihao.test.nginx.jifenyun.com;

 

        #charsetkoi8-r;

 

       #access_log logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm index.php;

                            proxy_passhttp://lihao.test.nginx.jifenyun.com;

                            proxy_set_headerHost $host;

                            proxy_set_headerX-Real-IP $remote_addr;

                            proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;

        }

 

       #error_page  404              /404.html;

 

        # redirectserver error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location =/50x.html {

            root   html;

        }

      

    }

}

 

 

两台Apache服务器配置:

 

     安装 apache + php

      安装php redis扩展

   unzip phpredis-master.zip //解压缩

   cd phpredis-master//进入解压目录(这一步很重要)

   /usr/local/php/bin/phpize//locate phpize,找到phpize(目录可能不同),并执行生成configture文件

    ./configure--with-php-config=/usr/local/php/bin/php-config//locate php-config,找到php-config(目录可能不同),并执行生成php扩展配置文件

    make 

     make test 

    make install

   vi /usr/local/php/etc/php.ini

      加入

    extension=redis.so

 

session 共享配置:

 修改php.ini文件:vi /usr/local/php/etc/php.ini

  session.save_handler =redis

session.save_path = "tcp://192.168.1.6:6379"

session.name = PHPSESSID  #redis 数据库中sessionIDkey 键名

 

 

配置完成!!!重启nginx  两台apache  redis 服务器配置允许其他人访问