nginx负载均衡,服务器集群配置,服务器挂机自动切换

1、 首先打开nginx/conf/nginx.conf修改如下配置

http {

#在http中添加下面内容

#加载虚拟主机配置
include vhost/*.conf;

}

 2、然后在nginx/conf文件夹下创建 vhost文件夹,并创建 my.conf文件

把以下内容添加到nginx/conf/vhost/my.conf中

#服务器的集群 #服务器集群名字
upstream  dahua {
    #least_conn; #把请求分配到连接数最少的server
    #ip_hash; #每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,可以解决session的问题。
    server    127.0.0.1:3000  weight=3;
    server    127.0.0.1:3001  weight=5;
    #服务器配置   weight是权重的意思,权重越大,分配的概率越大。
}

############ 设定虚拟主机配置 ##############
server {
    #侦听端口
    listen		80;
    listen		443 ssl;
    #定义
    server_name www.dz.com;

    ssl_certificate      mycert.crt;
    ssl_certificate_key  mycert.key;

    location / {
        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        #禁用缓存
        #proxy_buffering off;

        #设置反向代理的地址
        proxy_pass  http://dahua;

        #这里就保证了自动切换服务器
        proxy_connect_timeout 10;  #连接超时 默认为60秒
        proxy_send_timeout 10;     #读取超时 默认为60秒
        proxy_read_timeout 10;     #发送超时 默认为60秒
    }

    location ~ asset
    {
        #设置缓存时间
        expires 5d;

        #设置反向代理的地址
        proxy_pass  http://dahua;

        #这里就保证了自动切换服务器
        proxy_connect_timeout 10;  #连接超时 默认为60秒
        proxy_send_timeout 10;     #读取超时 默认为60秒
        proxy_read_timeout 10;     #发送超时 默认为60秒
    }
}
############ 设定虚拟主机配置 END ##############

猜你喜欢

转载自blog.csdn.net/u011477914/article/details/84381509