首页 > 代码库 > Nginx负载均衡之二配置负载均衡

Nginx负载均衡之二配置负载均衡

一、修改nginx配置文件

//进入配置目录
# cd /application/nginx/conf   

//筛选配置文件内容
# egrep -v "^$|#" nginx.conf.default >nginx.conf    

//编辑配置文件,改为以下内容
# vi nginx.conf   

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream backend {
    server 192.168.2.30:80    weight1 max_fails=3 fail_timeout=30s;
    server 192.168.2.40:80    weight1 max_fails=3 fail_timeout=30s;
}

    server {
        listen       80;
        server_name  www.etiantian.org;
        index  index.html index.htm;
 location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
:wq     //保存退出

 

二、重启服务

//重启服务
# /application/nginx/sbin/nginx  -s reload

 

三、测试

 

//访问测试,每次内容不一样为正常
# for n in `seq 100`;do curl 192.168.2.31;sleep 2;done  

 注:如果测试命令测试出的结果每次都不一样说明,nginx搭建成功

Nginx负载均衡之二配置负载均衡