nginx实现负载均衡

nginx实现负载均衡

  • http节点中添加
    upstream backend  {
            server  192.168.1.108:80;
            server  192.168.1.109:80;
        }
  • 修改server节点:
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;
        proxy_pass http://backend;
    } 
  • 多种应用按域名分发:
    upstream django  {
            server  192.168.1.108:80;
            server  192.168.1.109:80;
            server  192.168.1.110:80;
            server  192.168.1.111:80;
    }
    upstream php  {
            server  192.168.1.112:80;
            server  192.168.1.113:80;
    }

    server {
        listen       80;
        server_name  www.zhuangyan.cn;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_buffering off;
          proxy_pass http://php;
        }
       
    }

    server {
        listen       80;
        server_name  app.zhuangyan.cn;
        location / {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_buffering off;
          proxy_pass http://django;
      }
      location ~/static/ {
            root  /home/django/;
            index  index.html index.htm;
      }      
    }

你可能感兴趣的:(nginx实现负载均衡)