php nginx 负载均衡简单配置过程

1、负载均衡
一台计算机的计算资源是有效的,当超大流量请求时,就可能导致请求等待或者服务器死机的情况,为了解决大流量访问的问题,可以搭建分布式,将请求分发到不同计算机,就可以解决大流量请求的问题。
长见的负载均衡方案有如下几种:
1、http重定向
2、反向代理负载均衡
3、 IP负载均衡
4、DNS负载均衡
5、DNS/GSLB负载均衡
详情介绍可以查看:
https://blog.csdn.net/s465564/article/details/77880868
对于一般的大流量请求,会用到nginx反向代理,下面将主要介绍如何搭建nginx反向代理的架构、php session共享,redis服务的配置。
nginx 和 php环境和redis的代建这里不做讲解,可自行搜索资料安装。

那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可,本次用三台服务。

步骤(本地虚拟机三台服务器Ubuntu):

172.10.19.104 主

172.10.19.101

172.10.19.97

在主服务上的nginx配置如下:

cd /etc/nginx     //注意实际环境上nginx配置文件路径

#代到本机 8080端口,处理用户请求

server{

    listen      8080;

    server_name load.com;

    location / {
        root   /opt/wwwroot;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /opt/wwwroot;
    }

    location ~ \.php$ {
         root           /opt/wwwroot;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include        fastcgi_params;
    }

}

#负载均衡模块,分发的服务器
upstream load.com{
    server 172.10.19.101:80;
    server 172.10.19.97:80;
    server 127.0.0.1:8080;
}

#监听80端口,并分发请求到其他服务器
server {

    listen       80;

    server_name  load.com;

    location  / {
        proxy_pass   http://load.com;
    }

}

因为80端口用于nginx监听用户请求,所以需要用8080端口接受nginx转发过来的请求。

其他服务器配置:

在其它两台服务器上编辑 nginx.conf 文件

 server{

    listen      80;

    server_name load.com;

    location / {
        root   /opt/wwwroot;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /opt/wwwroot;
    }

    location ~ \.php$ {
         root           /opt/wwwroot;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include        fastcgi_params;
    }

}

三台机器分别在在/opt/wwwroot/ 目录下建立下index.php ,

修改host文件,做域名劫持load.com 到

然后访问load.com 就可以看到页面的切换,这说明负载均衡配置完成。

2、php sesssion 共享

session是服务器与客户端进行信息交互用于验证身份的一种方式,用于存储客户端的一些信息数据,php默认session存储方式为服务器本地的文件,修改php.ini,具体配置如下:

session.save_handler = files #默认为文件存储方式
session.save_path ="D:/wamp/tmp"#session文件存储地址
session.name = PHPSESSID #存储到cookie的sessionID,每次客户端访问服务器,都会带着客户端本地的cookie信息发送到服务器

其他详细配置,可以参考php.ini,这里不过多详细说明

设计思路
单机服务器都是将session信息默认存储到自己的本地,当多台部署时,需要一个公用的session服务,保证每个服务器都可以通过公用服务获取到session信息,公用服务方式可以是文件,缓存,数据库,或者专门用于session共享服务的工具等等;
这里主要说明一下redis;
redis属于高性能服务,运行于内存,支持多种数据结构类型非常适合用于存储session信息,同事redis还有定时将内存数据固化到磁盘的功能,这在服务宕机,数据恢复方面也提供了便利,如果用memcache,则需要定时任务执行数据备份脚本;

设计方案:
一、通过php自身的session配置实现

session.save_handler = redis#配置为redis
session.save_path ="tcp://127.0.0.1:6379" #如果redis设置了密码则:session.save_path = "tcp://redis服务地址:端口?auth=密码"

该方案由于配置不支持多样化,只能用于用户少量时的应用。

二、通过框架扩展session应用,这里不贴具体代码实现,主体思路是用户所有请求均通过框架单一入口文件(现在流行框架都是这样设计),通过框架进行session扩展,开发者通过编码可以自由选择session存储机制,数据备份机制,数据恢复机制,
数据读取规则等;大体思路为session扩展类需要对外提供session信息的访问,存储,修改。session服务可以通过配置文件实现不同的架构设计,具体设计有hashMap等。
三、nginx中的ip_hash技术能够将某个ip的请求定向到同一台后端,这样一来这个ip下的某个客户端和某个后端就能建立起稳固的session,ip_hash是在upstream配置中定义的:

upstream nginx.example.com
       { 
                server 192.168.74.235:80; 
                server 192.168.74.236:80;
                ip_hash;
       }
       server
       {
                listen 80;
                location /
                {
                        proxy_pass
                       http://nginx.example.com;
                }
    }

ip_hash是容易理解的,但是因为仅仅能用ip这个因子来分配后端,因此ip_hash是有缺陷的,不能在一些情况下使用:
1.nginx不是最前端的服务器。

ip_hash要求nginx一定是最前端的服务器,否则nginx得不到正确ip,就不能根据ip作hash。譬如使用的是squid为最前端,那么nginx取ip时只能得到squid的服务器ip地址,用这个地址来作分流是肯定错乱的。
3、redis 配置,允许远程访问
redis默认只允许本地访问,要使redis可以远程访问可以修改redis.conf
打开redis.conf文件在NETWORK部分有说明

################################## NETWORK #####################################
 
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

解决办法:注释掉bind 127.0.0.1可以使所有的ip访问redis
若是想指定多个ip访问,但并不是全部的ip访问,可以bind到指定IP,注意防火墙配置,否则无法正常访问。
在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下:

  (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

修改办法:protected-mode no

你可能感兴趣的:(php)