网站地图

nginx代替squid做缓存服务器

2013年7月10日 | 分类: nginx使用 | 标签: , ,

Nginx从0.7.48版本开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令,手动清除指定缓存页面,但是,通过一个第三方的Nginx模块,可以清除指定URL的缓存。

Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

proxy_cache和fastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid。我们已经在生产环境使用了 Nginx 的 proxy_cache 缓存功能超过两个月,十分稳定,速度不逊于 Squid。

在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。

nginx的安装请见:http:/www.phpjiayuan.com/?cat=60

修改nginxs配置文件,在http配置里面加上:


1
2
3
4
# 注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data/nginx_cache/temp;
# 设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data/nginx_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

并创建好缓存存放的文件夹:


1
mkidr -p /data/nginx_cache

我在nginx.conf结尾配置了引入vhosts文件夹下所有的.conf结尾的文件,用于把每个域名的配置单独存在文件:


1
include /etc/nginx/vhosts/*.conf;

vhosts文件里面配置的配置如下:


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
server {
    listen       80;
    server_name  phpjiayuan.com www.phpjiayuan.com;
    access_log off;
    error_log off;
    index index.html index.htm index.php;
    root /pathto/phpjiayuan.com;

    location ~ ^.*\.svn\/{
        deny all;
    }

    if ($host != 'www.phpjiayuan.com'){
        rewrite ^/(.*)$ http:/www.phpjiayuan.com/$1 permanent;
    }

    location / {
        # 如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_cache cache_one;
        # 对不同的HTTP状态码设置不同的缓存时间
        proxy_cache_valid  200 304 12h;
        #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
        proxy_cache_key $host$uri$is_args$args;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        # 将真实的请求转到81端口
        proxy_pass http:/127.0.0.1:81;
        expires 1d;
    }

     #用于清除缓存,假设一个URL为http:/192.168.8.42/test.txt,通过访问http:/192.168.8.42/purge/test.txt就可以清除该URL的缓存。
    location ~ /purge(/.*)
    {
        #设置只允许指定的IP或IP段才可以清除URL缓存。
        #allow            127.0.0.1;
        #allow            192.168.0.0/16;
        #deny            all;
        proxy_cache_purge cache_one $host$1$is_args$args;
    }    

    #扩展名以.php结尾的动态应用程序不缓存。
    location ~ .*\.php$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http:/127.0.0.1:81;
    }
}


# 配置81端口真实虚拟主机(非缓存)
server {
    listen       81;
    server_name  phpjiayuan.com www.phpjiayuan.com;
    access_log off;
    error_log off;
    index  index.html index.htm index.php;
    root   /pathto/phpjiayuan.com;

    location ~ ^.*\.svn\/{
        deny all;
    }

    if ($host != 'www.phpjiayuan.com'){
        rewrite ^/(.*)$ http:/www.phpjiayuan.com/$1 permanent;
    }

    location /
    {
        if ($request_filename ~ "favicon\.ico$") {
            break;
        }

        if (-e $request_filename) {
            break;
        }

        if (!-e $request_filename) {
            rewrite  ^.*$  /index.php  last;
            break;
        }
    }

    location ~ .*\.(php|php5)?$
    {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
目前还没有任何评论.