htbzwd 发表于 2018-12-7 11:47:03

Nginx+tomcat 实现负载均衡和动静分离

  转载于:http://blog.csdn.net/Yoara/article/details/41803153
  怎么下载和安装nginx/tomcat就不说了,谈谈在刚开始配置时最容易让人模糊的地方。
  1.配置动静分离和负载均衡,注意,upstram、server都是在http{}下面的。
  upstream t1.test.com {
  server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
  }
  其中,红色标注定义的服务器组的名字,后续通过改名字制定服务器组。
  server {
  listen       80;
  #server
  server_namet1.test.com;
  location / {
  proxy_connect_timeout   3;
  proxy_send_timeout      30;
  proxy_read_timeout      30;
  proxy_pass http://t1.test.com;
  }
  #配置Nginx动静分离
  location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
  root /data/www;
  #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力.
  expires 3d;
  }
  }
  特别注意,蓝色标注的是指域名,就是通过访问http://t1.test.com   将使用该server定义。后面红色标注,指的是使用upstream中定义的服务器组。这样,访问http://t1.test.com域名的所有请求都会被server   t1.test.com处理。
  2.理解了upstream的使用方法,负载均衡就不是问题了,weight表示权重,max_fails=2 fail_timeout=30s这两个参数是指,30s内请求失败2次就不会再访问该server了。fail_timeout时间过后才继续访问,这是容错的一种处理,服务器如果恢复了,还是能继续使用。完整的配置如下
   view plain copy https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

[*]  upstream t1.test.com {
[*]  server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
[*]  }
[*]  upstream t2.test.com {
[*]  server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;
[*]  }
[*]  upstream all.test.com {
[*]  server 192.168.235.1:8080 weight=1 max_fails=2 fail_timeout=30s;
[*]  server 192.168.235.1:8081 weight=1 max_fails=2 fail_timeout=30s;
[*]  }
[*]  server {
[*]  listen       80;
[*]  #server
[*]  server_namet1.test.com;
[*]
[*]  location / {
[*]  proxy_connect_timeout   3;
[*]  proxy_send_timeout      30;
[*]  proxy_read_timeout      30;
[*]  proxy_pass http://t1.test.com;
[*]  }
[*]
[*]  #配置Nginx动静分离
[*]  location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
[*]  root /data/www;
[*]  #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力.
[*]  expires 3d;
[*]  }
[*]  }
[*]  server {
[*]  listen       80;
[*]  #server
[*]  server_namet2.test.com;
[*]
[*]  location / {
[*]  proxy_connect_timeout   3;
[*]  proxy_send_timeout      30;
[*]  proxy_read_timeout      30;
[*]  proxy_pass http://t2.test.com;
[*]  }
[*]
[*]  #配置Nginx动静分离
[*]  location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
[*]  root /data/www;
[*]  #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力.
[*]  expires 3d;
[*]  }
[*]  }
[*]  server {
[*]  listen       80;
[*]  #server
[*]  server_nameall.test.com;
[*]
[*]  location / {
[*]  proxy_connect_timeout   3;
[*]  proxy_send_timeout      30;
[*]  proxy_read_timeout      30;
[*]  proxy_pass http://all.test.com;
[*]  }
[*]
[*]  #配置Nginx动静分离
[*]  location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
[*]  root /data/www;
[*]  #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力.
[*]  expires 3d;
[*]  }
[*]  }

页: [1]
查看完整版本: Nginx+tomcat 实现负载均衡和动静分离