服务器1:192.168.0.1
服务器2:192.168.0.2
一、配置域名到服务器1
xxx.com
二、在服务器1加入nginx配置
upstream www{
server 192.168.0.1:8081 weight=2 fail_timeout=2s max_fails=2;
server 192.168.0.2:8081 weight=1 fail_timeout=2s max_fails=2;
}
server {
listen 80;
server_name xxx.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";
?>
五、访问xxx.com
如果输出内容“服务器1”“服务器2”来回切换则说明配置成功