Nginx 兩臺伺服器配置負載均衡!!!

lizhiqiang666發表於2019-03-05

伺服器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”來回切換則說明配置成功

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章