Nginx 服务器

nginx搭建负载均衡

2018-03-12  本文已影响10人  FrancisSoung

配置

vim /usr/local/nginx/conf/nginx.conf

在http内添加:

upstream proxy_test {
   server 192.168.10.87:80 weight=1;     #如果你要测试,把这里换成你自己要代理后端的ip
   server 192.168.10.86:80 weight=1;
   #ip_hash;                                              #当负载两台以上用ip来hash解决session的问题,一台就别hash了。
 }

在server内添加:

location / {
        proxy_pass       http://proxy_test;               #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口,   或url 
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

nginx平滑重启

nginx -s reload

测试

后端服务器开启,在192.168.10.87和86的网页文件位置添加测试文件test.html,内容87上: this is test 87, 86上:this is test 86,这样方便查看访问到哪台了。

在本地配置好host,192.168.10.65 www.test.com

ping www.test.com 是否能ping通这个192.168.10.65地址,如果ok,则继续打开浏览器用www.test.com去访问后端服务器的文件,如: www.test.com/test.html,浏览器打开显示有this...说明配置ok了。

然后F5刷新一下,如果是87和86不停的切换,说明负载ok了。

上一篇下一篇

猜你喜欢

热点阅读