这个模块可以转发请求到其他的服务器。
HTTP/1.0无法使用keepalive(后端服务器将为每个请求创建并且删除连接)。nginx为浏览器发送HTTP/1.1并为后端服务器发送HTTP/1.0,这样浏览器就可以为浏览器处理keepalive。

注意当使用http proxy模块(甚至FastCGI),所有的连接请求在发送到后端服务器之前nginx将缓存它们,因此,在测量从后端传送的数据时,它的进度显示可能不正确。

◆ 简例:
用nginx反向代理2台web服务器(任何web服务器,如iis、nginx、apache)
nginx:192.168.18.240
iis1:192.168.18.10
iis2:192.168.18.11

nginx配置
        upstream backend {
                server 192.168.18.10;
                server 192.168.18.11;
        }
        server {
                listen             80;
                server_name    localhost;
                location / {
                        proxy_pass http://backend;
                }
        }
这将使对nginx的所有访问都被转发到iis服务器上,而且是轮循iis1和iis2,权重相同,每台iis获得的请求数相同。

◆ proxy_pass指令的参数最后的/问题
192.168.0.6:nginx
192.168.0.8:iis7

iis7的web根目录下有文件如下:
文件名         内容
/a.html         /a.html
/html/a.html    /html/a.html

看配置:
无/时:
location ^~ /html {
                                proxy_pass http://192.168.0.8;
}
[root@demo conf]# curl 192.168.0.6/html/a.html
/html/a.html

iis服务器的/html/a.html被访问到了

现在看有/时:
location ^~ /html {
                                proxy_pass http://192.168.0.8/;
}
[root@demo conf]# curl 192.168.0.6/html/a.html
/a.html

iis服务器的/a.html被访问到了

Note:由此可见当proxy_pass的参数的后面有/时,转发到后端机器的uri会去掉location中定义的uri。

◆ proxy_cache缓存功能使用例子
此功能不能和proxy_store一起使用,冲突,只能二选一,被镜像的文件目录结构类似squid
使用此功能可以取代Squid,在清除缓存时需要使用到第三方模块Cache Purge
下载地址:http://labs.frickle.com/nginx_ngx_cache_purge/
需要重新编译nginx使用--add-module=模块源码路径
nginx部分配置:
        upstream localhost {
                server 192.168.18.241:80;
                server 192.168.18.241:81;
                server 192.168.18.241:82;
        }
        proxy_cache_path    /data/webcache levels=1:2 keys_zone=one:10m inactive=1h max_size=10g;
        proxy_cache_valid any 10m;
        server {
                listen             80;
                server_name    localhost;
                root     html;
                index index.html;
                proxy_cache one;
                proxy_cache_key $uri$is_args$args;
                #proxy_cache_key $scheme$proxy_host$request_uri;
                location / {
                        proxy_pass http://localhost;
                        proxy_cache_purge PURGE from 127.0.0.1;
                }
                #location ~ ^/purge(/.*){
                #     proxy_cache_purge one $1$is_args$args;
                #}
        }
其中:
/data/webcache 缓存文件存放路径
levels=1:2 缓存目录层次结构,第一层目录为一个字母,第二层目录为2个字母,字母取自由proxy_cache_key指令设置的值经过md5哈希后从后向前取,如/data/webcache/ 9/ 7d/6666cd76f96956469e7be39d750cc 7d 9,9为第一级目录,7d为第二级目录
keys_zone=one:10m one为内存缓冲区的名称,10m为内存缓冲区的大小
inactive=1h 一小时内未访问的内容将被清除
max_size=10g 定义硬盘缓存空间大小为10g
配置中#号注释的proxy_cache_key指令的参数是默认的,需要修改,如不修改,purge清除缓存时就会有问题,根据变量可知如果访问的网址是http://localhost/,此时的key是httplocalhost/。在清除时就会报400错误。
proxy_cache_valid 定义何种响应状态被缓存,如200、404,10m代表10分钟。 默认没有状态,即不缓存任何,必须定义才行,这里定义为任何状态,上过当。
proxy_cache one 使用前面定义的one缓存区
proxy_cache_key 重新定义要哈希的key
proxy_cache_purge PURGE from 127.0.0.1 这是支持清理缓存的第一种方法,发送purge头来清理缓存,而且只有127.0.0.1的机器才可以清理,用其他机器会出现403。
最下面注释的那个location是另外一种清理缓存的方法,即使用GET方法清理的方式,可以通过浏览器直接清理。

测试第一种方法:
先产生缓存
[root@vm3 conf]# curl 192.168.18.240
<font size="28px">w2</font>
[root@vm3 conf]# find /data/webcache/ -type f
/data/webcache/9/7d/6666cd76f96956469e7be39d750cc7d9
使用192.168.18.240这个ip用PURGE方法清理
[root@vm3 conf]# echo -e 'PURGE / HTTP/1.0\r\n' | nc 192.168.18.240 80
HTTP/1.1 403 Forbidden
Server: nginx/1.2.5
Date: Tue, 11 Dec 2012 22:05:10 GMT
Content-Type: text/html
Content-Length: 168
Connection: close

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.2.5</center>
</body>
</html>
发现403,拒绝访问,我们再用127.0.0.1清理
[root@vm3 conf]# echo -e 'PURGE / HTTP/1.0\r\n' | nc 127.0.0.1 80
HTTP/1.1 200 OK
Server: nginx/1.2.5
Date: Tue, 11 Dec 2012 22:06:43 GMT
Content-Type: text/html
Content-Length: 253
Connection: close

<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : /
<br>Path: /data/webcache/9/7d/6666cd76f96956469e7be39d750cc7d9
</center>
<hr><center>nginx/1.2.5</center>
</body>
</html>
发现200,清理成功,我们查查看缓存还有没
[root@vm3 conf]# find /data/webcache/ -type f
[root@vm3 conf]#
缓存已被清理,成功。

测试第二种方法:
先修改配置文件,将proxy_cache_purge PURGE from 127.0.0.1;行注释,并取消最下面的location的注释。
产生缓存
[root@vm3 conf]# curl 192.168.18.240
<font size="28px">w1</font>
[root@vm3 conf]# find /data/webcache/ -type f
/data/webcache/9/7d/6666cd76f96956469e7be39d750cc7d9
使用GET方法清理,可以像前面一样用echo配合nc清理,也可以用浏览器直接访问清理
[root@vm3 conf]# echo -e 'GET /purge/ HTTP/1.0\r\n' | nc 192.168.18.240 80
HTTP/1.1 200 OK
Server: nginx/1.2.5
Date: Tue, 11 Dec 2012 22:16:00 GMT
Content-Type: text/html
Content-Length: 253
Connection: close

<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : /
<br>Path: /data/webcache/9/7d/6666cd76f96956469e7be39d750cc7d9
</center>
<hr><center>nginx/1.2.5</center>
</body>
</html>
发现200,清理成功,我们查查缓存文件还存在不
[root@vm3 conf]# find /data/webcache/ -type f
[root@vm3 conf]#
ok,文件不存在了。我们用浏览器访问清理试试,先产生缓存不必说了吧,方法同上








再次查看缓存文件,不存在了。
在这个location里也可以限制访问ip,如:
allow 127.0.0.1
deny all

◆ proxy_store镜像功能使用例子
此功能不能和proxy_store一起使用,冲突,只能二选一,被镜像的文件目录结构和后端服务器一直,不像proxy_cache
nginx部分配置:
        upstream localhost {
                server 192.168.18.241:80;
                server 192.168.18.241:81;
                server 192.168.18.241:82;
        }
        proxy_store on;
        proxy_temp_path /data/proxy_temp;
        server {
                listen             80;
                server_name    localhost;
                root /data/webstore;
                index index.html;
                location ~* \.(mp3|css|js|gif|jpg|jpeg|png|bmp)$ {
                        if (!-f $request_filename){
                                proxy_pass http://localhost;
                        }
                }
        }
创建配置中需要用到的目录:
[root@vm3 ~]# mkdir /data
[root@vm3 ~]# mount /dev/sdb1 /data
[root@vm3 ~]# install -d -o nobody -g root -m 0700 /data/proxy_temp
[root@vm3 ~]# install -d -o nobody -g nobody -m 0700 /data/webstore
其中proxy_temp目录不用创建也行,nginx启动时会自己创建,以启动时的身份root创建。
webstore目录需要手动创建。如不,nginx启动后根据需要以运行时的身份nobody创建,如果父目录允许就没问题,否则就会创建失败。
访问测试:
[root@vm3 ~]# curl 192.168.18.240/1.css
css
[root@vm3 ~]# ls /data/webstore/
1.css
[root@vm3 ~]# curl 192.168.18.240/js/index.js
<script type="text/javascript">alert("ok");</script>
[root@vm3 ~]# ls /data/webstore/
1.css    js
[root@vm3 ~]# ls /data/webstore/js
index.js
ok镜像成功!
Note:
proxy_store 需要指定一个路径,用于存储镜像文件,如果设置为on,将使用root或alias指定的路径存储。
proxy_temp_path 用来缓存临时文件,当文件缓存完毕,就将其移动到proxy_store目录,因此这两个目录要在一个文件系统上,以减少文件数据块的移动操作。因为在同一个文件系统上,文件的移动只是inode的变更,不需要移动数据块,所以操作少,速度快。通过监控文件系统(inotify)变化可以发现。
当用作整站镜像访问目录时会出现问题,要特别注意!

现在问题又来了,如何设置过期expires呢,怎么清理过期文件呢?
方法一:
用cron来做,能起到一定作用,但是不够实时
*/20 * * * * /usr/bin/find /data/webstore -type f -iname "*.mp3" -o -iname "*.css" -o -iname "*.js" -o -iname "*.gif" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.bmp" -mtime +2 -print0 | xargs -0 rm -f &> /dev/null
每20分钟清理一次超过2天未修改的文件。

方法二:
用fastcgi模拟purge处理方式,在server段里加入如下配置
                location ^~ /purge/ {
                        allow 127.0.0.1;
                        deny all;
                        fastcgi_pass 127.0.0.1:9000;
                        include fastcgi.conf;
                        rewrite (.*) /purge.php break;
                }
purge.php文件内容如下:
<?php
$filename = $_SERVER['REQUEST_URI'];
$filename = substr_replace($filename,'',0,6);
$filepath = $_SERVER['DOCUMENT_ROOT'];

if ($filename == '' || $filename == '/' || $filename == '/purge.php'){
                $status = 'Failed';
                $result = "File: / Does Not Purge";
}elseif (@unlink("$filepath$filename")){
                $status = 'Successful';
                $result = "File: $filename";
}else{
                $status = 'Failed';
                $result = "File: $filename Purge Failed";
}
?>
<html>
<head><title><?= $status ?> purge</title></head>
<body bgcolor="white">
<center><h1><?= $status ?> purge</h1>
<br><?= $result ?>
</center>
<hr><center>nginx/1.2.5</center>
</body>
</html>
这种方式类似squid的Purge方法。下面测试
[root@vm3 webstore]# curl 127.0.0.1/1.css
w2
[root@vm3 webstore]# ls
1.css    purge.php
[root@vm3 webstore]# curl 127.0.0.1/purge/1.css
<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>File: /1.css</center>
<hr><center>nginx/1.2.5</center>
</body>
</html>
[root@vm3 webstore]# ls
purge.php
ok成功

方法三:
修改nginx源码,呵呵。