【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件
#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,缓存静态文件的更多相关文章
- 【摘自张宴的"实战:Nginx"】nginx配置
user nobody;worker_processes 2; #error_log logs/error.log;error_log logs/error.log notice;#error_log ...
- 【摘自张宴的"实战:Nginx"】使用nginx的fastcgi_cache缓存php输出的内容
亲自测试发现,fastcgi_cache虽然可以缓存生成的php输出的文件,但是有个弊端,在缓存的失效时间之内,你继续访问这个地址,输出的内容没有发生变化,即使数据库新增了数据或者删除了数据,所以不适 ...
- 【摘自张宴的"实战:Nginx"】http auth baseic模块(打开页面需要密码验证)
location /admin { auth_basic "kingsoft"; auth_basic_user_file httppasswd; #密码文件的路径 默 ...
- 【摘自张宴的"实战:Nginx"】try_files指令
语法:try_files param1 [param2...paramN] fallback 默认值:none 使用环境: location 该指令用于告诉nginx测试每个文件是否存在,并且使用首先 ...
- 【摘自张宴的"实战:Nginx"】nginx模块开发
Nginx的模块不能够像Apache那样动态的加载,所以模块都要预先编译进Nginx的二进制可执行文件中. Nginx的模块有三种角色: 1. Handler(处理模块) 用于处理Http请求 ...
- 【摘自张宴的"实战:Nginx"】Nginx的server指令
server 语法:server name[parameters] 默认值:none 使用环境:upstream 该指令用于指定后端服务器的名称和参数.服务器的名称可以是一个域名.一个IP地址.端口号 ...
- 霸气!Nginx 中缓存静态文件秘籍
导读 这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件)设 ...
- 用nginx缓存静态文件
这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件) ...
- virtualBox 虚拟机下nginx设置不缓存静态文件不起作用解决办法
最近开发的时候,调整js时会一直使用缓存文件,无法显示改动!nginx配置静态文件add_header Cache-Control no-cache;也不起作用,很苦恼! nginx配置代码:even ...
随机推荐
- Range 函数 与break 用法
range 函数,这个比什么java ,C++的for (int i = 0; i < 5; i++),确实舒服很多. 写这么一句就可以了 for i in range(0,5). 翻译一遍更容 ...
- Hadoop单机模式和伪分布式搭建教程CentOS
1. 安装JAVA环境 2. Hadoop下载地址: http://archive.apache.org/dist/hadoop/core/ tar -zxvf hadoop-2.6.0.tar.gz ...
- Linux-压缩与解压缩命令
常用的压缩格式:.zip .gz .bz2 .tar.gz .tar.bz2 1.Zip 压缩文件:zip 压缩文件名 源文件 压缩目录:zip -r 压缩文件名 源文件 解压缩.z ...
- New Concept English three (54)
打字练习: 27w/m 45errors We have been brought up to fear insects. We regard them as unnecessary creature ...
- 在Mac上激活Adobe产品
1.在任意位置下载需要的Adobe软件(推荐官网正版) 网速不好或者不通推荐下载离线安装包: https://helpx.adobe.com/download-install/kb/creative- ...
- 【机器学习】集成学习之xgboost的sklearn版XGBClassifier使用教程
XGBClassifier是xgboost的sklearn版本.代码完整的展示了使用xgboost建立模型的过程,并比较xgboost和randomForest的性能. # -*- coding: u ...
- UVA 11637 Garbage Remembering Exam
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...
- 「新手向」koa2从起步到填坑
前传 出于兴趣最近开始研究koa2,由于之前有过一些express经验,以为koa还是很好上手的,但是用起来发现还是有些地方容易懵逼,因此整理此文,希望能够帮助到一些新人. 如果你不懂javascri ...
- TP中讲的两种ajax方法
一. 控制器中public function add(){ $this->display(); } public function ajaxSave(){ if(IS_AJAX){ echo ' ...
- 用Azure上Cognitive Service的Face API识别人脸
Azure在China已经发布了Cognitive Service,包括人脸识别.计算机视觉识别和情绪识别等服务. 本文将介绍如何用Face API识别本地或URL的人脸. 一 创建Cognitive ...