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根据配置将请求分发到后端真实服务器
posted @ 2015-11-06 23:06  忧忧夏天  阅读(232)  评论(0)    收藏  举报