Nginx 配置负载均衡

1:接上一篇博客:Nginx 源码安装

http://www.cnblogs.com/xiaoit/p/4062458.html

2:准备另外两台相同的配置机器

搭建Nginx的服务器主:101.69.178.208

负载均衡机器Nginx1:101.69.178.219

负载均衡机器Nginx2:61.153.100.239

3:查看主服务器上nginx.conf

cd /gechong/ruanjian/Nginx/conf

新装完Nginx的conf文件如下:

#user  nobody;

worker_processes  1;



events {

    worker_connections  1024;

}



http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

	

    server {

        listen       80;

        server_name  localhost;





        location / {

            root   html;

            index  index.html index.htm;

        }



        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }



    }



}

做如下调整


#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {

upstream 101.69.178.208 {
server 61.153.100.239:80;
server 101.69.178.219:80;
}

include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name 101.69.178.208;


location / {
# root html;
# index index.html index.htm;
#}
proxy_pass http://101.69.178.208;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
}

4:修改Nginx1和Nginx2机器上的nginx.conf 配置文件

cd /gechong/ruanjian/Nginx/conf

 

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {

include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name 101.69.178.208;

 

location / {
root html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
}

 

5:简单修改index.html以区别两台机器

6:测试负载均衡

浏览器访问:http://101.69.178.208/

Nginx 配置负载均衡

Nginx 配置负载均衡

7:测试成功。。。

但是单独拿出来一台机器做转发,感觉有点浪费资源,下一节将说明如何配置转发机器也提供访问。

 

 

你可能感兴趣的:(nginx)