---
title: "利用nginx加速web访问 "
author: "wuce7758"
date: "2013-01-14T11:39:24Z"
tags: []
categories: ["企业架构"]
slug: "nginx-web"
draft: false
lastmod: "2013-01-14T11:39:24Z"
id: 1769306
url: "https://wuce7758.iteye.com/blog/1769306"
---

公司想把图片从阿里云的服务器上迁移到自己的服务器，顺便建立一套新的图片存储系统。

开始打算使用FastDFS，但是FastDFS有自己的文件名生成规则，我们网站已经有了很多图片，如果每个图片都重新生成的话，工作量比较大。由于时间原因，临时采用了nginx来实现。

 

采用了3台服务器，每台服务器上都安装了nginx。用户访问相应图片时，根据自己定义的nginx location的hash规则，2012年的图片映射到一台机器上，2013年的映射到另一台机器上。

 

由于nginx已经支持了web缓存，考虑到图片可能会被临时替换，所以需要一个可以清除缓存的组件ngx\_cache\_puge。

 

 

Linux代码 [![复制代码](http://isoftyh.iteye.com/images/icon_copy.gif)](http://isoftyh.iteye.com/blog/1768936 "复制代码") [![收藏代码](http://isoftyh.iteye.com/images/icon_star.png)![](http://isoftyh.iteye.com/images/spinner.gif)]()

1. #现在并解压
2. wget http://labs.frickle.com/files/ngx\_cache\_purge-2.0.tar.gz
3. tar zxvf ngx\_cache\_purge-2.0.tar.gz
4. #在安装nginx的时候 增加其组件
5. ./configure --add-module=../ngx\_cache-2.0 --prefix=/usr/local/nginx
6. make
7. make install

```
#现在并解压 
wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
tar zxvf ngx_cache_purge-2.0.tar.gz
#在安装nginx的时候 增加其组件
./configure --add-module=../ngx_cache-2.0 --prefix=/usr/local/nginx
make 
make install
```

 

 

我开始装的是purge-1.0在make的时候报错，后来考虑是nginx版本不兼容的问题，换成了2.0之后就不报错了。

 

废话不多说，直接上nginx的配置文件。

入口nginx配置 负责分发到两天机器上

 

Linux代码 [![复制代码](http://isoftyh.iteye.com/images/icon_copy.gif)](http://isoftyh.iteye.com/blog/1768936 "复制代码") [![收藏代码](http://isoftyh.iteye.com/images/icon_star.png)![](http://isoftyh.iteye.com/images/spinner.gif)]()

01. #配置缓存目录 两个目录必须在同一分区
02. proxy\_temp\_path /home/data0/proxy\_temp\_path;
03. proxy\_cache\_path /home/data0/proxy\_cache\_path levels=1:2 keys\_zone=cache\_one:200m inactive=1d max\_size=30g;
04. #两天图片服务器上面都通过nginx部署了web服务
05. upstream pic1{
06. server 192.168.2.6:8080;
07. }
08. upstream pic2{
09. server 192.168.2.2:8080;
10. }
11. server {
12. listen       8080;
13. server\_name  localhost;
14. #charset koi8-r;
15. #access\_log  logs/host.access.log  main;
16. #这个要放在前面，以免被提前拦截了，用来清除缓存的
17. location ~ /purge(/.\*){
18. #allow 127.0.0.1;
19. #allow 192.168.2.6;
20. #deny  all;
21. proxy\_cache\_purge cache\_one $host$1$is\_args$args;
22. }
23. location ~ /{
24. proxy\_cache cache\_one;
25. proxy\_cache\_valid 200 304 12h;
26. proxy\_cache\_valid 301 302 1m;
27. proxy\_cache\_valid any 1m;
28. #如果是图片路径总包含/2013/ 则映射到pic1
29. proxy\_cache\_key $host$uri$is\_args$args;
30. if ( $request\_uri ~* "\\w\*\\/2013\\/\\w\*\\.(ico|gif|bmp|jpg|jpeg|png|swf)$"){
31. proxy\_pass http://pic1;
32. }
33. if ( $request\_uri ~* "\\w\*\\/2012\\/\\w\*\\.(ico|gif|bmp|jpg|jpeg|png|swf)$"){
34. proxy\_pass http://pic2;
35. }
36. proxy\_pass http://pic2;
37. }
38. }

```
#配置缓存目录 两个目录必须在同一分区
proxy_temp_path /home/data0/proxy_temp_path;
proxy_cache_path /home/data0/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#两天图片服务器上面都通过nginx部署了web服务
upstream pic1{
          server 192.168.2.6:8080;
}

upstream pic2{
        server 192.168.2.2:8080;
}

server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #这个要放在前面，以免被提前拦截了，用来清除缓存的
        location ~ /purge(/.*){


            #allow 127.0.0.1;
            #allow 192.168.2.6;
            #deny  all;
            proxy_cache_purge cache_one $host$1$is_args$args;
         }

location ~ /{

        proxy_cache cache_one;
        proxy_cache_valid 200 304 12h;
        proxy_cache_valid 301 302 1m;
        proxy_cache_valid any 1m;

        #如果是图片路径总包含/2013/ 则映射到pic1
        proxy_cache_key $host$uri$is_args$args;
        if ( $request_uri ~* "\w*\/2013\/\w*\.(ico|gif|bmp|jpg|jpeg|png|swf)$"){
                proxy_pass http://pic1;
         }
        if ( $request_uri ~* "\w*\/2012\/\w*\.(ico|gif|bmp|jpg|jpeg|png|swf)$"){
                proxy_pass http://pic2;
         }
         proxy_pass http://pic2;
        }
}
```

 

 

  图片服务器nginx配置，这个类似fastdfs中的storage节点,会映射到服务器的/home/www路径

 

 

Linux代码 [![复制代码](http://isoftyh.iteye.com/images/icon_copy.gif)](http://isoftyh.iteye.com/blog/1768936 "复制代码") [![收藏代码](http://isoftyh.iteye.com/images/icon_star.png)![](http://isoftyh.iteye.com/images/spinner.gif)]()

1. server {
2. listen       8080;
3. server\_name  localhost;
4. #charset koi8-r;
5. #access\_log  logs/host.access.log  main;
6. location / {
7. root   /home/www;
8. index  index.html index.htm;
9. }

```
 server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/www;
            index  index.html index.htm;
        }
}
```

 

 

这样，通过路径就能访问到图片服务器上的图片。并且会对图片做12小时的缓存。当你需要清除缓存的时候，只要在图片的路径前面加一个/purge，再执行下即可。如图片路径[http://img.naomi.cn/pic/2012/100.jpg](http://124.205.194.254:8080/pic/2012/100.jpg) 那么清除缓存的路径就是http://img.naomi.cn/purge/pic/2012/100.jpg。