4、url_hash(第三方)
  按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
  例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
  upstream backend {
  server squid1:3128;
  server squid2:3128;
  hash $request_uri;
  hash_method crc32;
  }
  tips:
  upstream bakend{#定义负载均衡设备的Ip及设备状态}{
  ip_hash;
  server 127.0.0.1:9090 down;
  server 127.0.0.1:8080 weight=2;
  server 127.0.0.1:6060;
  server 127.0.0.1:7070 backup;
  }
  在需要使用负载均衡的server中增加
  proxy_pass http://bakend/;
  每个设备的状态设置为:
  1.down表示单前的server暂时不参与负载
  2.weight为weight越大,负载的权重越大。
  3.max_fails:允许请求失败的次数默认为1.当超过大次数时,返回proxy_next_upstream模块定义的错误
  4.fail_timeout:max_fails次失败后,暂停的时间。
  5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会轻。
  nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
  client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
  client_body_temp_path设置记录文件的目录 可以设置多3层目录
  location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
  ##配置虚拟机
  server
  {
  listen 80;
  配置监听端口
  server_name image.***.com;
  配置访问域名
  location ~* .(mp3|exe)$ {
  对以“mp3或exe”结尾的地址进行负载均衡
  proxy_pass http://img_relay$request_uri;
  设置被代理服务器的端口或套接字,以及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;
  以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上
  }
  location /face {
  if ($http_user_agent ~* "xnp") {
  rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  }
  proxy_pass http://img_relay$request_uri;
  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 404 502 = @fetch;
  }
  location @fetch {
  access_log /data/logs/face.log log404;
  rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  }
  location /image {
  if ($http_user_agent ~* "xnp") {
  rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  }
  proxy_pass http://img_relay$request_uri;
  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 404 502 = @fetch;
  }
  location @fetch {
  access_log /data/logs/image.log log404;
  rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
  }
  }
  ##其他举例
  server
  {
  listen 80;
  server_name *.***.com *.***.cn;
  location ~* .(mp3|exe)$ {
  proxy_pass http://img_relay$request_uri;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location / {
  if ($http_user_agent ~* "xnp") {
  rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  }
  proxy_pass http://img_relay$request_uri;
  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 404 http://i1.***img.com/help/noimg.gif;
  error_page 404 502 = @fetch;
  }
  location @fetch {
  access_log /data/logs/baijiaqi.log log404;
  rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  }
  }
  server
  {
  listen 80;
  server_name *.***img.com;
  location ~* .(mp3|exe)$ {
  proxy_pass http://img_relay$request_uri;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  location / {
  if ($http_user_agent ~* "xnp") {
  rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;
  }
  proxy_pass http://img_relay$request_uri;
  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 404 http://i1.***img.com/help/noimg.gif;
  error_page 404 = @fetch;
  }
  #access_log off;
  location @fetch {
  access_log /data/logs/baijiaqi.log log404;
  rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
  }
  }
  server
  {
  listen 8080;
  server_name ngx-ha.***img.com;
  location / {
  stub_status on;
  access_log off;
  }
  }
  server {
  listen 80;
  server_name imgsrc1.***.net;
  root html;
  }
  server {
  listen 80;
  server_name ***.com w.***.com;
  # access_log /usr/local/nginx/logs/access_log main;
  location / {
  rewrite ^(.*)$ http://www.***.com/ ;
  }
  }
  server {
  listen 80;
  server_name *******.com w.*******.com;
  # access_log /usr/local/nginx/logs/access_log main;
  location / {
  rewrite ^(.*)$ http://www.*******.com/;
  }
  }
  server {
  listen 80;
  server_name ******.com;
  # access_log /usr/local/nginx/logs/access_log main;
  location / {
  rewrite ^(.*)$ http://www.******.com/;
  }
  }
  location /NginxStatus {
  stub_status on;
  access_log on;
  auth_basic "NginxStatus";
  auth_basic_user_file conf/htpasswd;
  }