Nginx負載均衡配置

原文地址html

場景描述

單機缺點

  • 當量上來後因爲單機請求壓力大,存在單點故障等問題

負載優勢

  • 下降單點故障
  • 提升服務器處理能力
  • 靈活控制請求流量
  • 隱藏真實服務器地址

負載均衡

以下圖nginx

Nginx配置

配置以下後端

#user  nobody;
worker_processes  8;
worker_rlimit_nofile 65535;


events {
    use epoll;
    worker_connections  8192;
}


#http核心
http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 10M;
    client_body_buffer_size 256k;

    #日誌格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                        '"$status" $body_bytes_sent "$http_referer" '
                                        '"$http_user_agent" "$http_x_forwarded_for" '
                                        '"$gzip_ratio" $request_time $bytes_sent $request_length';

    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    upstream backend{
        server 192.168.1.1 weight=1;
        server 192.168.1.1 weight=1;
    }

    #虛擬服務器
    server {
        listen       80;
        server_name  www.go008.com;


        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://backend;
            proxy_redirect off;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Cookie $http_cookie;
            chunked_transfer_encoding off;
            proxy_pass_header Set-Cookie;
        }

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

白話描述

  • 當用戶請求www.go008.com的時候,因爲該域名IP地址解析到負載均衡nginx所在服務器
  • 因爲虛擬服務器server配置了監聽www.go008.com:80
  • 請求被分發到proxy_pass配置的反向代理模塊backend
  • upstream根據配置將請求分發到後端真實服務器
相關文章
相關標籤/搜索