#user nobody;
worker_processes 1;

error_log logs/static_source.error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

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

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/static_source.access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

#*************************************************************************
#nginx缓存区的使用,作用类似squid,第一次访问的时候加载资源,后面访问的时候直接使用资源,这里是使用Nginx的方式实现缓存,这里是缓存静态的资源类文件, fastcgi也可以可以缓存的,在server模块里面可以设置的,后面会提到
#注 proxy_temp_path, proxy_cache_path指定的路径必须在一个分区
proxy_temp_path /data/proxy_temp_path;

#设置web缓存区的名字为cache_one ,内存缓存空间大小为200M, 自动清除超过1天美意被访问的缓存数据,磁盘空间大小为30GB
proxy_cache_path /data/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#************************************************************************
server {
listen 81;
server_name localhost;

#charset koi8-r;

access_log logs/81.access.log main;
error_log logs/81.error.log info;

root /data/www/static; #下面的每一个location模块都共用这个root地址,每个location也可以单独的配置root地址
location / {
#root /data/www/static;
index index.php index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#root html; 共享上面的在server里配置的root值,注意,如果这里配置了root,那么php后缀的,都是去这里的root找哦,每个单独的location都是可以单独的配置root的
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

upstream my_server_pool
{
server 127.0.0.1:81 weight=1 max_fails=2 fail_timeout=30s;
}

server {
listen 80;
server_name www.test.com;

charset utf-8;

access_log logs/test.access.log main;

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

#用于清除缓存,假设URL为my.domain.com/test.gif 通过http://my.domain.com/purge/test.gif可以清除该URL的缓存
location ~ /purge(/.*)
{
#return 402;
#设置值允许指定的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;
#proxy_pass http://my_server_pool;
#proxy_cache cache_one;
#proxy_cache_key $host$uri$is_args$args;
#proxy_cache_purge PURGE from 127.0.0.1;
}

#使用nginx作为缓存,缓存静态文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
#使用web缓存区cache_one (在nginx.conf文件定义的)
proxy_cache cache_one;

expires 1h;
#对不同http状态马缓存设置不同的缓存时间
proxy_cache_valid 200 304 12h;         #这里表示如果访问的返回的是200或者304状态码,那么缓存12h,在这个12之内,即使静态资源有改变,也会返回缓存的,所以资源改变了,需要删除缓存的资源
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;

#设置web缓存的key值,nginx根据key值md5哈希存储缓存,这里根据域名,uri, 参数 组合合成key.
proxy_cache_key $host$uri$is_args$args;

#反向代理,访问后端内容源服务器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}

#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}

}
}

访问资源 http://192.168.66.138/b.css,会在/data/proxy_cache_path生成缓存文件

页面输出的内容:

body {
border-style:solid;
border-width:5px;
} 在缓存文件目录/data/proxy_cache_path生成文件6cd081e217db73624337f1521e858b6a
内容如下(下面那个不是乱码,是缓存文件里面的内容):

^C^@^@^@e¯HW/¶GW¥^FHW<9c>^L0^S^@^@<9b>^@<81>^A^M"5747b62f-2f"^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^^@
KEY: 192.168.66.138/b.css
HTTP/1.1 200 OK^M
Server: nginx/1.11.0^M
Date: Fri, 27 May 2016 08:34:45 GMT^M
Content-Type: text/css^M
Content-Length: 47^M
Last-Modified: Fri, 27 May 2016 02:51:27 GMT^M
Connection: close^M
ETag: "5747b62f-2f"^M
Accept-Ranges: bytes^M
^M
body {
border-style:solid;
border-width:5px;
}

访问http://192.168.66.138/purge/b.css页面会显示如下图片

【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件的更多相关文章

  1. 【摘自张宴的"实战:Nginx"】nginx配置

    user nobody;worker_processes 2; #error_log logs/error.log;error_log logs/error.log notice;#error_log ...

  2. 【摘自张宴的"实战:Nginx"】使用nginx的fastcgi_cache缓存php输出的内容

    亲自测试发现,fastcgi_cache虽然可以缓存生成的php输出的文件,但是有个弊端,在缓存的失效时间之内,你继续访问这个地址,输出的内容没有发生变化,即使数据库新增了数据或者删除了数据,所以不适 ...

  3. 【摘自张宴的"实战:Nginx"】http auth baseic模块(打开页面需要密码验证)

    location /admin { auth_basic "kingsoft"; auth_basic_user_file httppasswd;      #密码文件的路径  默 ...

  4. 【摘自张宴的"实战:Nginx"】try_files指令

    语法:try_files param1 [param2...paramN] fallback 默认值:none 使用环境: location 该指令用于告诉nginx测试每个文件是否存在,并且使用首先 ...

  5. 【摘自张宴的"实战:Nginx"】nginx模块开发

    Nginx的模块不能够像Apache那样动态的加载,所以模块都要预先编译进Nginx的二进制可执行文件中. Nginx的模块有三种角色: 1. Handler(处理模块)     用于处理Http请求 ...

  6. 【摘自张宴的"实战:Nginx"】Nginx的server指令

    server 语法:server name[parameters] 默认值:none 使用环境:upstream 该指令用于指定后端服务器的名称和参数.服务器的名称可以是一个域名.一个IP地址.端口号 ...

  7. 霸气!Nginx 中缓存静态文件秘籍

    导读 这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件)设 ...

  8. 用nginx缓存静态文件

        这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件) ...

  9. virtualBox 虚拟机下nginx设置不缓存静态文件不起作用解决办法

    最近开发的时候,调整js时会一直使用缓存文件,无法显示改动!nginx配置静态文件add_header Cache-Control no-cache;也不起作用,很苦恼! nginx配置代码:even ...

随机推荐

  1. Ubuntu下使用tmux实现分屏,以及tmux快捷键

    最近用到了终端的复用,使用了tmux,写一下自己的使用和一些快捷键. tmux是指通过一个终端登录远程主机并运行后,在其中可以开启多个控制台的终端复用软件. 来个效果图: 截图我使用的命令是  gno ...

  2. websphere6.1升级

    1.到IBM websphere官方网站下载相应版本的更新包,如6.1.0-WS-WASSDK-WinX32-FP0000021.pak和6.1.0-WS-WAS-WinX32-FP0000021.p ...

  3. Spring mvc服务端消息推送(SSE技术)

    SSE技术是基于单工通信模式,只是单纯的客户端向服务端发送请求,服务端不会主动发送给客户端.服务端采取的策略是抓住这个请求不放,等数据更新的时候才返回给客户端,当客户端接收到消息后,再向服务端发送请求 ...

  4. uva10780(分解质因数)

    可以直接用高精度来暴力求. 也可以不用高精度: 把m分解质因数,记录每个因数和它的次数.然后计算每个因数在n的阶乘里出现了多少次,再把这个次数除以它在m中的次数,就是可能的k值.取最小的k. #inc ...

  5. 团队队列(列和map结合的经典运用)

    Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...

  6. MLCC 电容的的 NP0 C0G 材质

    MLCC 电容的的 NP0 C0G 材质 随手记一下. MLCC 中最稳定的材质 NP0 C0G,NP0 和 C0G 是相同的,只是不同的产商不同的名字而已. 注意中间的是 0 不是 英文字母 O,虽 ...

  7. ubuntu tftp server config

    1.安装tftp-server sudo apt-get install tftpd-hpa sudo apt-get install tftp-hpa(如果不需要客户端可以不安装) tftp-hpa ...

  8. 洛谷【P2024】[NOI2001]食物链

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://www.luogu.org/problemnew/show/P202 ...

  9. Java基础--ThreadLocal

    Java中的ThreadLocal 可以看做以线程标识为key的Map,在多线程开发中使用非常方便. 示例 class ThreadEnv { // 用匿名内部类覆盖ThreadLocal的initi ...

  10. svn-clearup 报错的处理(Cleanup failed to process the following paths...)

    在使用 svn 客户端执行操作失败后,执行 Clean up 操作也报错:Cleanup failed to process the following paths... ,一直不知道是什么原因.通常 ...