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

栏目: 服务器 · Nginx · 发布时间: 5年前

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

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

以上所述就是小编给大家介绍的《Nginx中upstream模块实现PHP服务器的负载均衡》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Starfish and the Spider

The Starfish and the Spider

Ori Brafman、Rod A. Beckstrom / Portfolio Hardcover / 2006-10-05 / USD 24.95

Understanding the amazing force that links some of today's most successful companies If you cut off a spider's leg, it's crippled; if you cut off its head, it dies. But if you cut off a st......一起来看看 《The Starfish and the Spider》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器