nginx负载均衡配置

接上篇nginx配置,然后再准备两台web服务器:

nginx服务器:192.168.0.241

web1:192.168.0.141

web2:192.168.0.142

一、两台web服务器先安装http:

[root@host1 ~]# yum -y install httpd
[root@host1 ~]# /etc/init.d/httpd start
[root@host1 ~]# echo "Hello,I'm 192.168.0.141" > /var/www/html/index.html
[root@host1 ~]# curl http://192.168.0.141
Hello,I'm 192.168.0.141
[root@host2 ~]# yum -y install httpd
[root@host1 ~]# /etc/init.d/httpd start
[root@host2 ~]# echo "Hello,I'm 192.168.0.142" > /var/www/html/index.html
[root@host2 ~]# curl http://192.168.0.142
Hello,I'm 192.168.0.142
#注意先看下服务器上是否有安装http,如httpd启动不起来,看下是否80端口在被占用

二、nginx服务器配置负载均衡:

[root@host3 conf]# cd /usr/local/nginx/conf/
[root@host3 conf]# vim nginx.conf
…………
http {

#定义源服务器组
upstream team {
     server  192.168.0.141;
     server  192.168.0.142;
  }
…………
…………
server {
#定义监听端口
      listen 8080;
#定义访问域名
      server_name www.load.com;
     location / {
#调用服务器组
     proxy_pass http://team;
         root html;
         index index.html;
      }
   }
…………
…………
}
:wq
[root@host3 conf]# nginx -s reload
#继续添加nginx服务器访问域名
[root@host3 conf]# vim /etc/hosts
…………
192.168.0.241 www.nginx1.com www.nginx2.com www.load.com
:wq
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142

注意:如果访问不了看下是否防火墙限制了,8080端口如被占用可改为其他端口

三、设置权重:

#接上面的配置
[root@host3 conf]# vim nginx.conf
…………
http {

upstream team {
#设置权重参数,机器的性能越好,可以设置参数越大
     server  192.168.0.141 weight=1;
     server  192.168.0.142 weight=2;
  }
…………
:wq
[root@host3 conf]# nginx -s reload
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.141
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142
[root@host3 conf]# curl http://www.load.com:8080
Hello,I'm 192.168.0.142

附:nginx优化参数(http模块)

server_tokens off;      #添加此行,不显示nginx具体版本号

include       mime.types;   #设定mime类型,类型由mime.type文件定义

sendfile        on;      #提升nginx读文件性能

tcp_nodelay     on;     #关闭tcp的缓延迟发送数据,即立即相应

keepalive_timeout  65;   #连接超时时间

gzip  on;        #开启gzip压缩

gzip_min_length 1000;   #小于1000k的不压缩,越压缩越大

gzip_comp_level 4;      #压缩级别是4(1-9个级别)

gzip_types ……           #允许压缩的类型,/usr/local/nginx/conf/mime.types有类型

client_header_buffer_size 1k;      #设定请求缓存

large_client_header_buffers 4 4k;    #最大请求缓存个数与容量

#先根据client_header_buffer分配,如果不够,再根据large值分配 

#设定虚拟主机配置:

    server {
        #侦听80端口
        listen    80;
        #定义使用 www.nginx.com访问
        server_name  www.nginx.com;
        #定义服务器的默认网站根目录位置
        root html;
        #设定访问日志
        access_log  logs/nginx.access.log  main;
        #默认请求
        location / {
        #定义首页索引文件的名称
            index index.php index.html index.htm;  
        }
   # 定义错误提示页面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

  #禁止访问 .htxxx 文件

            location ~ /.ht {
            deny all;
  }
}
posted on 2018-03-10 15:55  Alan丶文  阅读(294)  评论(0编辑  收藏  举报