#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. 04-THREE.JS 添加、删除物体,批量操作

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  2. LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  3. LeetCode OJ:Summary Ranges(概括区间)

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  4. 源码编译安装mysql5.6

    1.download the source package and unzip to the destination dir http://mirror.switch.ch/mirror/mysql/ ...

  5. MySql数据库中存放用户密码需要注意什么?

    前几天被电话面试了,问了一些比较实际的问题,其中一个问题关于PHP开发中MySql里存放用户密码需要注意什么,由于没有过大项目经验,一时语塞,回来网上找了找记下来,希望能对其他人有帮助,我也继续学习. ...

  6. UVALive - 4126 Password Suspects (AC自动机+状压dp)

    给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...

  7. BZOJ5206: [Jsoi2017]原力

    BZOJ5206: [Jsoi2017]原力 https://lydsy.com/JudgeOnline/problem.php?id=5206 分析: 比较厉害的三元环问题. 设立阈值,当点的度数大 ...

  8. hdu5542 The Battle of Chibi[DP+BIT]

    求给定序列中长度为M的上升子序列个数.$N,M<=1000$. 很容易想到方法.$f[i,j]$表示以第$i$个数结尾,长度为$j$的满足要求子序列个数.于是转移也就写出来了$f[i][j]+= ...

  9. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  10. java 连接oracle数据库

    package shujuku; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...