美文网首页
Nginx中upstream模块实现PHP服务器的负载均衡

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

作者: 师娘哪里去了 | 来源:发表于2020-04-02 12:55 被阅读0次

    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服务器的负载均衡

    相关文章

      网友评论

          本文标题:Nginx中upstream模块实现PHP服务器的负载均衡

          本文链接:https://www.haomeiwen.com/subject/rkcxphtx.html