本文的配置同样适用于 CentOS6.5(好象是废话哈,Nginx 是跨平台的)
反向代理
1 | location / { |
配置示例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
48server {
listen 80;
server_name mysite.com;
charset utf-8;
access_log logs/mysite.com.access.log;
error_log logs/mysite.com.error.log;
root D:/dev.workspace/php/mysite/web/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 1d; #xdebug调试时长
}
# 下面的配置需要根据你用的 php 框架灵活变更
# 我们的框架只有 index.php 一个 php 为后缀的文件,其它都是 html 为后缀的文件
# 我们的项目采用了 mvc 框架,配置路由来访问相关页面,url 没有文件后缀。
# 所以,必须这样配置,否则除了访问 index.php 之外,会报 404 错误。
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}
}
#下面是两个反向代理。通过路由 crm 和 rate 作为反向代理标志与 mysite 主站点整合到一起。让用户感觉不到是在访问多个站点。
#crm 为 java 开发的 web 应用站点,Session 是通过读取 Memcache 的关键信息,做了模拟登陆实现与主站统一用户信息的,实际上 session 是单独管理的。应该有更好的解决方案。
#生产环境可以把 IP 地址换成域名
location ^~ /crm {
proxy_pass http://127.0.0.1:8180/crm; # 直接在 IDEA 运行,需要设置 server.context-path: /crm,部署到 tomcat 则需要将应用目录命名为 crm
proxy_redirect http://127.0.0.1:8180/crm/ http://mysite.com/crm/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#rate 为另外一个 php 应用站点。
#都是 php 站点,可通过 Memcached 管理 session,轻松达到在多个站点之间共享 session 的目的。
location ^~ /rate {
proxy_pass http://p.rate.com;
proxy_redirect http://p.rate.com/ http://mysite.com/rate/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
示例2:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#负载均衡配置,这里可以添加多个server,weight为权重,如果添加多个,注意分布式session的处理
upstream backend {
server localhost:8080 weight=1;
}
server {
listen 80;
server_name mysite.me;
location / {
proxy_pass http://backend; #来自jsp请求交给tomcat处理
proxy_redirect off;
proxy_set_header Host $host; #后端的Web服务器可以通过X-Forwarded-For>获取用户真实IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 6 32k; #proxy_buffers缓冲区,网页平均在32k以下的话>,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
}
负载均衡
内置负载策略
轮循(默认)
Nginx根据请求次数,将每个请求均匀分配到每台服务器。可以通过其它参数来控制分配比例,后面有详细描述。
IP Hash
绑定处理请求的服务器。第一次请求时,根据该客户端的IP算出一个HASH值,将请求分配到集群中的某一台服务器上。后面该客户端的所有请求,都将通过HASH算法,找到之前处理这台客户端请求的服务器,然后将请求交给它来处理。
在配置上,与默认策略的区别在于,在 upstream 配置的第一行是否有:ip_hash;
nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
示例:1
2
3
4
5
6
7
8
9
10
11
12
13
14upstream webs {
#ip_hash; #加上这一行,则为 IP Hash 模式
server 192.168.0.101;
server 192.168.0.102;
server example.com;
}
server {
listen 80;
location / {
proxy_pass http://webs;
}
}
解释:
- proxy_pass http://webs:表示将所有请求转发到 webs 服务器组中配置的某一台服务器上。
- upstream 模块:配置反向代理服务器组,Nginx 会根据配置,将请求分发给组里的某一台服务器。webs 是服务器组的名称。
- upstream 模块下的 server 指令:配置处理请求的服务器 IP 或域名,端口可选,不配置默认使用 80 端口。通过上面的配置,Nginx 默认将请求依次分配给101,102来处理。
可以通过修改下面这些参数来改变默认的分配策略:
weight:默认为1,将请求平均分配给每台server。
下面的配置,表示6次请求中,192.168.0.100 分配3次,192.168.0.101 分配2次,192.168.0.102 分配1次1
2
3
4
5upstream tomcats {
server 192.168.0.100 weight=3; # 3/6次
server 192.168.0.101 weight=2; # 2/6次
server 192.168.0.102 weight=1; # 1/6次
}
max_fails
默认为1。某台Server允许请求失败的次数,超过最大次数后,在fail_timeout时间内,新的请求将不会分配给这台机器。如果设置为0,Nginx会将这台Server置为永久无效状态,然后将请求发给定义了proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream指令来处理这次错误的请求。
fail_timeout
默认为10秒。某台Server达到max_fails次失败请求后,在fail_timeout期间内,nginx会认为这台Server暂时不可用,不会将请求分配给它。1
2
3
4
5upstream tomcats {
server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
server 192.168.0.101:8080 weight=3;
server 192.168.0.102:8080 weight=1;
}
192.168.0.100这台机器,如果有3次请求失败,nginx 在15秒内,不会将新的请求分配给它。
backup
备份机,所有服务器挂了之后才会生效1
2
3
4
5
6upstream tomcats {
server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;
server 192.168.0.101:8080 weight=3;
server 192.168.0.102:8080 backup;
}
在 192.168.0.100 和 192.168.0.101 都挂了之前,192.168.0.102 为不可用状态,不会将请求分配给它。只有当 192.168.0.100 和 192.168.0.101 都挂了,192.168.0.102 才会被启用。
down
标识某一台server不可用。个人理解,应该是用于临时配置,等服务器确认可用了之后再去掉 down 标志。否则,如果一直不启用,干脆不要配置这台服务器好了。
max_conns
限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置1
2
3upstream tomcats {
server 192.168.0.100:8080 max_conns=1000;
}
表示最多给100这台Server分配1000个请求,如果这台Server正在处理1000个请求,nginx将不会分配新的请求给到它。假如有一个请求处理完了,还剩下999个请求在处理,这时nginx也会将新的请求分配给它。
resolve
将server指令配置的域名,指定域名解析服务器。需要在http模块下配置resolver指令,指定域名解析服务
1 | http { |
表示example.com域名,由10.0.0.1服务器来负责解析。
upstream模块server指令的其它参数和详细配置说明。请参考官方文档:Module ngx_http_upstream_module
开发环境可正常使用的配置示例
负载均衡配置
注意,本示例的监听端口 81 如果改为 80 端口,则在“嵌入”其它站点时,负载均衡 p.weblb.com 不能被正确的反向代理。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20upstream crm-webs {
ip_hash;
server p.javaweb.com:8180 weight=5 max_fails=1 fail_timeout=180;
#server p.javaweb.com:18081 weight=1 max_fails=1 fail_timeout=180;
#server p.javaweb.com:18082 backup;
}
server {
access_log logs/crm.web.access.log;
error_log logs/crm.web.error.log;
listen 81;
server_name p.weblb.com;#可选配置
location / {
proxy_pass http://crm-webs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
web 页面访问
http://p.weblb.com:81
如果 listen 配置为 80 端口,则不用在域名后面带端口号。直接访问:http://p.weblb.com
“嵌入”其它站点
如果这个应用站点需要“嵌入”其它站点,让访问者感觉不到这是两个站点,可以使用反向代理,这样配置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
33server {
listen 80;
server_name tb.qn.com;
charset utf-8;
access_log logs/tb.qn.com.access.log;
error_log logs/tb.qn.com.error.log;
root D:/dev.workspace/php/tb.qn/trunk/web/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 1d; #xdebug调试时长
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php last;
break;
}
}
location ^~ /crm {
proxy_pass http://p.javaweb.com:81/crm;
proxy_redirect http://p.javaweb.com:81/crm/ http://tb.qn.com/crm/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
其中 p.javaweb.com、p.weblb.com,均在 hosts 指向本地 IP1
2127.0.0.1 tb.qn.com
127.0.0.1 p.weblb.com