nginx php 负载均衡

  • 内容
  • 评论
  • 相关

准备工作:部署两台centos系统 ,分别安装 nginx php

web1:192.168.1.189

web2:192.168.1.190

image

 

1.登陆192.168.1.189,配置nginx代理

upstream xiyou{
        server 192.168.1.189:80;
        server 192.168.1.190:80;
}

server{

        listen 80;
        server_name www.xiyou.com;
        location / {
                root /var/www/html/xiyou;
                proxy_pass http://xiyou;
                index  index.html index.htm index.php;
                autoindex on;

        }

}

 

image

 

2.配置web1,登陆192.168.1.189  /etc/nginx/conf.d/

新建web1-xiyou-com.conf

server {
    listen       80;
    server_name  192.168.1.189 web1.xiyou.com;

  

    location / {
        root   /var/www/html/xiyou;
        index  index.php index.html index.htm;
        autoindex on;
    }

       location ~ \.php$ {
        root           /var/www/html/xiyou;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

 

3.配置web2,登陆192.168.1.190 /etc/nginx/conf.d

新建web2-xiyou-com.conf

server {
    listen       80;
    server_name  192.168.1.189 web1.xiyou.com;

     location / {
        root   /var/www/html/xiyou;
        index  index.php index.html index.htm;
        autoindex on;
    }

       location ~ \.php$ {
        root           /var/www/html/xiyou;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

   location ~ /\.ht {
        deny  all;
    }
}

 

4.分别重启两台机器上的nginx 

 

5.本地hosts 绑定域名

192.168.1.189 www.xiyou.com

 

6.浏览器测试 http://www.xiyou.com

 

image

image