Nginx中upstream模塊實現PHP服務器的負載均衡

Nginx中upstream模塊實現PHP服務器的負載均衡

upstream模塊介紹

Nginx 的負載均衡功能依賴於 ngx_http_upstream_module 模塊,所支持的代理方式包括 proxy_pass 、fastcgi_pass 、memcached_pass 。upstream 是nginx做爲代理及緩存的核心結構而且請求上游發送至下游都能由相關聯的模塊進行干預處理。php

試驗環境

Nginx服務器IP:192.168.58.134html

PHP服務器1IP:192.168.58.132nginx

PHP服務器2IP:192.168.58.130web

實驗搭建

配置Nginx服務器

首先搭建Nginx服務器,在上一篇博客中,Nginx服務器已經搭建好,這裏咱們須要修改Nginx.conf文件,在裏面啓用upstream模塊,對於PHP服務器池進行配置,實現其負載均衡。vim

[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服務器的負載均衡服務器

相關文章
相關標籤/搜索