Nginx 两台服务器配置负载均衡!!!

服务器1:11.11.11.11
服务器2:22.22.22.22

一、配置域名到服务器1

www.test.com

二、在服务器1加入nginx配置

upstream  www{
     server 11.11.11.11:8081 weight=2 fail_timeout=2s max_fails=2;
     server 22.22.22.22:8081 weight=1 fail_timeout=2s max_fails=2;
}

server {
    listen       80;
    server_name  www.fuzaijunhen.com;

        location / {
                proxy_pass http://www/;
                #proxy_redirect default; 
                 proxy_set_header Host   $host;
                proxy_connect_timeout 2s;
        }
}

三、在服务器1和服务器2中继续加入配置(配置你的项目访问地址)

server {
        listen       8081;
        server_name  localhost;
        root   /opt;#你的项目路径

        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

四、在服务器1和服务器2目录opt下创建index.php

内容如下

<?php
echo "服务器1";
?>

五、访问www.test.com

如果输出内容“服务器1”“服务器2”来回切换则说明配置成功

lizhiqiang666
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!