Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* 即时运用与P2P
* Node.js性能优化
* 集群与负载均衡
* [nginx的实现](/集群与负载均衡/nginx的实现.md)
* 数据库相关
* 性能测试

Expand Down
Binary file added assets/nginx-request.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions 集群与负载均衡/nginx的实现.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# nginx实现负载均衡

Nginx 的负载均衡功能,其实实际上和 nginx 的代理是同一个功能,只是把代理一台机器改为多台机器而已。

## 配置文件

```
upstream backend {
server 192.168.0.18;
server 192.168.0.28;
server 192.168.0.38;
}

server {
listen 80;
server_name 192.168.0.08;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```

通过上述配置,Nginx会作为HTTP反向代理,把访问本机的HTTP请求,均分到后端集群的3台服务器Server1、Server2、Server3。

![](/assets/nginx-request.png)

nginx的upstream目前支持4种方式的分配

* 轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

* weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

* iphash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

* fair 按后端服务器的响应时间来分配请求,响应时间短的优先分配。