Nginx中upstream模块实现PHP服务器的负载均衡

Nginx中upstream模块实现PHP服务器的负载均衡

upstream模块介绍

Nginx 的负载均衡功能依赖于 ngx_http_upstream_module 模块,所支持的代理方式包括 proxy_pass 、fastcgi_pass 、memcached_pass 。upstream 是nginx作为代理及缓存的核心结构并且请求上游发送至下游都能由相关联的模块进行干预处理。

试验环境

Nginx服务器IP:192.168.58.134

PHP服务器1IP:192.168.58.132

PHP服务器2IP:192.168.58.130

实验搭建

配置Nginx服务器

首先搭建Nginx服务器,在上一篇博客中,Nginx服务器已经搭建好,这里我们需要修改Nginx.conf文件,在里面启用upstream模块,对于PHP服务器池进行配置,实现其负载均衡。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

    #gzip  on;
    upstream php {                               #定义定义php服务器池,权重都为1,相当于访问模式是轮询
        server 192.168.58.132:9000 weight=1;
        server 192.168.58.130:9000 weight=1;
       }
    server {
        listen       80;
        server_name  localhost;

        location ~ \.php$ {
            root           /var/www/html/webphp;   #两台php服务器中都必须要有这个目录,里面有不同的index.php文件
            fastcgi_pass   php;                     #这里要修改为php服务器池,而不是单个服务器
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
#重启Nginx服务

配置PHP服务器

两台php服务器一样配置,在上一篇博客中也有详细配置。然后都要启用php-fpm,查看启动正常。
Nginx中upstream模块实现PHP服务器的负载均衡
Nginx中upstream模块实现PHP服务器的负载均衡
Nginx中upstream模块实现PHP服务器的负载均衡
Nginx中upstream模块实现PHP服务器的负载均衡

测试

我们访问192.168.58.134/index.php可以看到两个php服务器轮流进行访问,最终实现了负载均衡。
Nginx中upstream模块实现PHP服务器的负载均衡
Nginx中upstream模块实现PHP服务器的负载均衡

猜你喜欢

转载自blog.51cto.com/10693404/2151987
今日推荐