Nginx负载均衡入口配置

以前做的一些网站都是放在Godaddy的虚拟主机下,也不考虑什么性能之类的事情,后来导致占用虚拟主机资源过高,没办法,只能换地方,就买了Burst的VPS,但是用了没多久,发现这个也撑不住,本来想买更高配置的VPS,后来转念一想,不如用几个便宜的搭个负载均衡,所以就索性买了4台VPS

一个入口VPS,两个负载VPS,一个DB,VPS 就这样搭了一个小型的负载均衡。

至于负载的VPS不用设置啥玩意,就是在入口的主机上搭上Nginx并且简单配置一下就好了。

我的代码如下:其中我把域名给换了123.com 和 321.com , 两个负载的IP分别是 184.82.107.126 和 184.82.2.222 ,如果有更多的负载可以在upstream 中继续追加就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 521000;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;

upstream *.123.com {
server 184.82.107.126:80;
server 184.82.2.222:80;

}

upstream *.321.com {
server 184.82.107.126:80;
server 184.82.2.222:80;

}
server
{
listen 80;
server_name *.123.com ;
location / {
proxy_pass http://*.123.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server
{
listen 80;
server_name *.321.com ;
location / {
proxy_pass http://*.321.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log off;
}
#access_log /var/log/nginx/access.log main;

#sendfile on;
#tcp_nopush on;

#keepalive_timeout 65;

#gzip on;

#include /etc/nginx/conf.d/*.conf;