当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx负载均衡配置简单配置方法

Nginx负载均衡配置简单配置方法

2019年05月29日  | 移动技术网网络运营  | 我要评论

背景

当一个网站从小到大,访问量逐渐增大现有的服务器已经支撑不住,一般的解决方案就是缓存、加服务器、数据库读写分离、实行负载均衡分布式等等,本人对这些技术方案都没有在项目中具体的实践过,

但是一直听同事过说起,利用空闲时间自我学习了解下;

负载均衡

什么是负载均衡,就是当快要承受不住的时候,又给你一台服务器来分担压力,请求会分配到两台服务器上,两台服务器上部署相同的内容相当于一个分身,可以处理相同的事情;

nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的web服务器

nginx配置文件

 1)  从nginx官网下载

 2)    安装nginx,并找到nginx.conf文件(c:\nginx\conf\nginx.conf);

 在http中加入配置:

   加权轮询,按服务器的性能给予权重,本例是1:2分配

  upstream www.woizuqiu.com { 
    server 192.168.1.1:8080 weight=1;
   server 192.168.1.1:8090 weight=2;
  }

  ip_hash轮询方法,不可给服务器加权重,nginx会让相同的客户端ip请求相同的服务器 

upstream www.woizuqiu.com {
    server 192.168.1.1:8080;
    server 192.168.1.1:8090 max_fails=3 fail_timeout=30s ;
   ip_hash;
   }

  根据服务器的本身的性能差别及职能,可以设置不同的参数控制。

  down 表示负载过重或者不参与负载

  weight 权重过大代表承担的负载就越大

  backup 其它服务器时或down时才会请求backup服务器

  max_fails 失败超过指定次数会暂停或请求转往其它服务器

  fail_timeout 失败超过指定次数后暂停时间

server配置如下: 

server {
  listen  80;
  server_name www.woizuqiu.com;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  location / { 
   add_header backendip $upstream_addr;#被转发到的上游服务器地址
   add_header backendcode $upstream_status;#状态码
   proxy_pass http://www.woizuqiu.com;
   proxy_set_header host $host; 
   proxy_set_header x-real-ip $remote_addr; 
   proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;          
  } 
 }

1.查看nginx版本:

  c:\nginx>nginx -v

2.启动nginx:

  c:\nginx>start nginx

  启动nginx需要占用80端口,常见错误:bind() to 0.0.0.0:8080 failed (10013: an attempt was made to access a socket in a way forbidden by its access permissions),需要把系统的80端口关掉,

  检查端口:netstat -aon | findstr :80 

3.判断nginx是否启动:

  tasklist /fi "imagename eq nginx.exe"

4.停止:

  c:\nginx>nginx.exe -s stop

5.重新载入nginx:

  c:\nginx>nginx.exe -s reload

总结

以上所述是小编给大家介绍的nginx负载均衡配置简单配置方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网