1. upstream lbs {
  2. server ip;
  3. server ip;
  4. }
  5. server {
  6. listen 80;
  7. server_name 域名;
  8. location / {
  9. proxy_pass http://lbs;
  10. }
  11. }

分配策略

轮询 (默认)

按顺序逐一分配

权重

weight 默认1;
值越大,分配越多

  1. upstream lbs {
  2. server ip weight=1;
  3. server ip weight=2;
  4. }

哈希

ip_hash
每个访客访问固定服务器,可解决session问题

  1. upstream lbs {
  2. ip_hash
  3. server ip;
  4. server ip;
  5. }

fair

按照后端响应时间分配

  1. upstream lbs {
  2. server ip;
  3. server ip;
  4. fair
  5. }

登录

  1. upstream lbs {
  2. sticky;
  3. server ip;
  4. server ip;
  5. }
  6. server {
  7. listen 80;
  8. server_name 域名;
  9. location / {
  10. proxy_pass http://lbs;
  11. client_max_body_size 1000m;
  12. proxy_set_header terminal_id $http_terminal_id;
  13. proxy_set_header Host $host:$server_port;
  14. proxy_set_header X_Read_IP $remote_addr;
  15. proxy_set_header X-Forwarded-For $proxy_add_x_forearded_for;
  16. proxy_set_header X-Forwarded-Proto "https";
  17. proxy_set_header X-Forwarded-Host $host;
  18. proxy_set_header X-Forwarded-Server $host;
  19. # keepalive_timeout 300;
  20. # proxy_connect_timeout 300;
  21. # proxy_read_timeout 300;
  22. }
  23. }

应用

  1. upstream lbs {
  2. server ip;
  3. server ip;
  4. }
  5. server {
  6. listen 80;
  7. server_name 域名;
  8. location / {
  9. proxy_pass http://lbs;
  10. client_max_body_size 1000m;
  11. proxy_set_header terminal_id $http_terminal_id;
  12. proxy_set_header Host $host:$server_port;
  13. proxy_set_header X_Read_IP $remote_addr;
  14. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  15. proxy_set_header X-Forwarded-Host $host;
  16. proxy_set_header X-Forwarded-Server $host;
  17. index index.html index.htm index.jsp;
  18. # add_header Access-Control-Allow-Origin *;
  19. # proxy_set_header X-Forwarded-Proto "https";
  20. }
  21. }