利用nginx的fastcgi_cache模块来做缓存

nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache。
proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。
fastcgi_cache缓存减少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力,这比用memcached之类的缓存要轻松得多。
但是,缓存也有弊端,比如说评论了之后不能会当时显示等等,自己取舍,有得必有失。

Read More

使用fastcgi_cache加速你的Nginx网站

  很久以前在TW上挖了个坑,说nginx的fastcgi_cache是被大家忽视的一大金矿,今天把这个坑填上。

  对于变化不太频繁的数据,大家都比较喜欢存Memcached以减少数据库的读取,但还是会有语言解析运行上的消耗(比如运行PHP,Python等),当然这个时间很短,记得OP上有个同学说P字头的语言,效率都不高,如果能省去,当然最好。(已经用上Squid等的可以忽略本文)。

  还有一个问题就是很多时候一个页面由多个数据片断组成,为了提高页面速度,要么分别缓存,要么整体缓存(所谓的Page Cache),其实都比较麻烦,增加和减少数据片断的时,大多需要调整。

  最后一个问题,所有的数据都存Memcached是否经济?服务器资源足够多的无所谓,捉襟见肘的就要考虑了,当然,生成静态页面是一种方法,需要维护,还是比较累。

Read More

基于nginx的FastCGI的缓存配置

废话不多说了, 直接上配置, 其实 fastcgi_cache 和 proxy_cache 的配置基本一样:

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
# !缓存文件存放目录
# levels 缓存层次
# keys_zone 缓存空间名和共享内存大小(热点内容放在内存)
# inactive 失效时间, 1d = 1天
# max_size 最大缓存空间(硬盘占用)
fastcgi_cache_path /home/xwsoul/code/web/demo/cgi_cache/cache_dir/ levels=2:2 keys_zone=cgi_one:10m inactive=2h max_size=2g;

server {

listen 80;
server_name demo.xwsoul.com;

root /home/xwsoul/code/web/demo;
index index.html index.php;

# 清理 fastcgi_cache
location ~ /pcgi(/*) {
allow 127.0.0.1;
allow 192.168.2.0/24;
deny all;
fastcgi_cache_purge cgi_one "$scheme$request_method$host$1";
}

try_files $uri @missing;
location @missing {
rewrite ^/cgi_cache/(.*) /cgi_cache/index.php?code=$1 last;
}

location ~ .*\.php$ {
# 默认情况下关闭cache
set $xwsoul_nocache yes;
# 请求的 URL 在 /cgi_cache/ 的允许缓存
if ($request_uri ~ ^/cgi_cache/) {
set $xwsoul_nocache "";
}
# CGI指向地址
fastcgi_pass 127.0.0.1:9000;
# 调用 cache 的命名空间
fastcgi_cache cgi_one;
# 缓存触发的方法 HEADER, GET, POST 中的一个或多个
fastcgi_cache_methods GET;
# 生成的缓存名称的 key 的名字
# $scheme https/http
# $request_method 请求方法,基于上面的设置,这里为GET
# $host 主机
# $request_uri 请求地址
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# 生效值: 即代理目标的状态码以及缓存时间
fastcgi_cache_valid 200 302 1h;
# 确定缓存成为过期数据的情况
fastcgi_cache_use_stale error timeout invalid_header http_500;
# 请求过多少次相同的URL后, 缓存将开始
fastcgi_cache_min_uses 1;
# 关闭缓存
fastcgi_cache_bypass $xwsoul_nocache;
fastcgi_no_cache $xwsoul_nocache;
# 发送头信息到客户端 - 一般是浏览器
add_header X-Cache "$upstream_cache_status";
# fastcgi 的其他参数配置
include fastcgi.conf;
}
}

Read More

基于php缓存的详解

nginx缓存

nginx有两种缓存机制:fastcgi_cache和proxy_cache
下面我们来说说这两种缓存机制的区别吧
proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的
fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽
fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。

Read More

Nginx负载均衡与反向代理的配置实例

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
user  www www;

worker_processes 10;

error_log /data1/logs/nginx_error.log crit;

pid /usr/local/webserver/nginx/nginx.pid;

worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

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

#charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

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 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
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;

#允许客户端请求的最大单个文件字节数
client_max_body_size 300m;

#缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户
client_body_buffer_size 128k;

#跟后端服务器连接的超时时间_发起握手等候响应超时时间
proxy_connect_timeout 600;

#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理
proxy_read_timeout 600;

#后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有的数据
proxy_send_timeout 600;

#代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可
proxy_buffer_size 16k;

#同上 告诉Nginx保存单个用的几个Buffer 最大用多大空间
proxy_buffers 4 32k;

#如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
proxy_busy_buffers_size 64k;

#proxy缓存临时文件的大小
proxy_temp_file_write_size 64k;

upstream php_server_pool {
server 192.168.1.10:80 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.1.11:80 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=30s;
}

upstream message_server_pool {
server 192.168.1.13:3245;
server 192.168.1.14:3245 down;
}

upstream bbs_server_pool {
server 192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s;
}

#第一个虚拟主机,反向代理php_server_pool这组服务器
server
{
listen 80;
server_name www.yourdomain.com;

location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://php_server_pool;
proxy_set_header Host www.yourdomain.com;
proxy_set_header X-Forwarded-For $remote_addr;
}

access_log /data1/logs/www.yourdomain.com_access.log;
}

#第二个虚拟主机
server
{
listen 80;
server_name www1.yourdomain.com;

#访问http://www1.yourdomain.com/message/***地址,反向代理message_server_pool这组服务器
location /message/
{
proxy_pass http://message_server_pool;
proxy_set_header Host $host;
}

#访问除了/message/之外的http://www1.yourdomain.com/***地址,反向代理php_server_pool这组服务器
location /
{
proxy_pass http://php_server_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

access_log /data1/logs/message.yourdomain.com_access.log;
}

#第三个虚拟主机
server{
listen 80;
server_name bbs.yourdomain.com *.bbs.yourdomain.com;

location /
{
proxy_pass http://bbs_server_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

access_log off;
}
}