- 浏览: 545587 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (618)
- java (109)
- Java web (43)
- javascript (52)
- js (15)
- 闭包 (2)
- maven (8)
- 杂 (28)
- python (47)
- linux (51)
- git (18)
- (1)
- mysql (31)
- 管理 (1)
- redis (6)
- 操作系统 (12)
- 网络 (13)
- mongo (1)
- nginx (17)
- web (8)
- ffmpeg (1)
- python安装包 (0)
- php (49)
- imagemagic (1)
- eclipse (21)
- django (4)
- 学习 (1)
- 书籍 (1)
- uml (3)
- emacs (19)
- svn (2)
- netty (9)
- joomla (1)
- css (1)
- 推送 (2)
- android (6)
- memcached (2)
- docker、 (0)
- docker (7)
- go (1)
- resin (1)
- groovy (1)
- spring (1)
最新评论
-
chokee:
...
Spring3 MVC 深入研究 -
googleyufei:
很有用, 我现在打算学学Python. 这些资料的很及时.
python的几个实用网站(转的) -
hujingwei1001:
太好了找的就是它
easy explore -
xiangtui:
例子举得不错。。。学习了
java callback -
幻影桃花源:
太好了,謝謝
Spring3 MVC 深入研究
使用nginx的proxy_cache做网站缓存
2012年12月5日mood发表评论阅读评论
2014年放假安排 手机号码吉凶测试 智能计算器
为什么要做web cache,我想大家最主要的是解决流量的压力。随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器,可以参看这里配置nginx反向代理配置
前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。
要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:
root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7
nginx的所有模块必须在编译的时候添加,不能再运行的时候动态加载,默认的编译选项下包含的模块,如果你不是显示的用参数关闭它。
nginx默认安装的模块如下
模块名称 描述 版本 如何禁用
Core Control ports, locations, error pages, aliases, and other essentials. --without-http
Access Allow/deny based on IP address. --without-http_access_module
Auth Basic Basic HTTP authentication. --without-http_auth_basic_module
Auto Index Generates automatic directory listings. --without-http_autoindex_module
Browser Interpret "User-Agent" string. 0.4.3 --without-http_browser_module
Charset Recode web pages. --without-http_charset_module
Empty GIF Serve a 1x1 image from memory. 0.3.10 --without-http_empty_gif_module
FastCGI FastCGI Support. --without-http_fastcgi_module
Geo Set config variables using key/value pairs of IP addresses. 0.1.17 --without-http_geo_module
Gzip Gzip responses. --without-http_gzip_module
Headers Set arbitrary HTTP response headers.
Index Controls which files are to be used as index.
Limit Requests Limit frequency of connections from a client. 0.7.20 --without-http_limit_req_module
Limit Zone Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. 0.5.6 --without-http_limit_zone_module
Limit Conn Limit concurrent connections based on a variable. --without-http_limit_conn_module
Log Customize access logs.
Map Set config variables using arbitrary key/value pairs. 0.3.16 --without-http_map_module
Memcached Memcached support. --without-http_memcached_module
Proxy Proxy to upstream servers. --without-http_proxy_module
Referer Filter requests based on Referer header. --without-http_referer_module
Rewrite Request rewriting using regular expressions. --without-http_rewrite_module
SCGI SCGI protocol support. 0.8.42 --without-http_scgi_module
Split Clients Splits clients based on some conditions 0.8.37 --without-http_split_clients_module
SSI Server-side includes. --without-http_ssi_module
Upstream For load-balancing. --without-http_upstream_ip_hash_module (ip_hash directive only)
User ID Issue identifying cookies. --without-http_userid_module
uWSGI uWSGI protocol support. 0.8.40 --without-http_uwsgi_module
X-Accel X-Sendfile-like module.
proxy模块中常用的指令时proxy_pass和proxy_cache.
nginx的web缓存功能的主要是由proxy_cache、fastcgi_cache指令集和相关指令集完成,proxy_cache指令负责反向代理缓存后端服务器的静态内容,fastcgi_cache主要用来处理FastCGI动态进程缓存(这里我不是很清楚这两个指令的区别,好像功能上都差不多,尤其后面这句话的意思,是我翻译过来的)。
确认proxy模块安装好后,下面对nginx的配置文件进行设置,重点部分如标红字体所示。
这是我的nginx.conf配置文件。
user www-data;
worker_processes 1;
#error_log logs/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" "$host"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip
#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end
## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}
## Start www.quancha.cn ##
server {
listen 80;
server_name *.quancha.cn;
access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;
## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200;
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##
}
配置文件中以proxy_开头的指令我们大都可以字面意思得到理解。请务必注意一点proxy_cache_path和proxy_temp_path设置的目录需要在同一分区,因为它们之间是硬链接的关系。
最后启动nginx,来迎接着激动人心的时刻吧。我已经迫不及待了。如果文章哪里有问题或者你遇到了什么麻烦,可以留言让我知道。
2012年12月5日mood发表评论阅读评论
2014年放假安排 手机号码吉凶测试 智能计算器
为什么要做web cache,我想大家最主要的是解决流量的压力。随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器,可以参看这里配置nginx反向代理配置
前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。
要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:
root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7
nginx的所有模块必须在编译的时候添加,不能再运行的时候动态加载,默认的编译选项下包含的模块,如果你不是显示的用参数关闭它。
nginx默认安装的模块如下
模块名称 描述 版本 如何禁用
Core Control ports, locations, error pages, aliases, and other essentials. --without-http
Access Allow/deny based on IP address. --without-http_access_module
Auth Basic Basic HTTP authentication. --without-http_auth_basic_module
Auto Index Generates automatic directory listings. --without-http_autoindex_module
Browser Interpret "User-Agent" string. 0.4.3 --without-http_browser_module
Charset Recode web pages. --without-http_charset_module
Empty GIF Serve a 1x1 image from memory. 0.3.10 --without-http_empty_gif_module
FastCGI FastCGI Support. --without-http_fastcgi_module
Geo Set config variables using key/value pairs of IP addresses. 0.1.17 --without-http_geo_module
Gzip Gzip responses. --without-http_gzip_module
Headers Set arbitrary HTTP response headers.
Index Controls which files are to be used as index.
Limit Requests Limit frequency of connections from a client. 0.7.20 --without-http_limit_req_module
Limit Zone Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. 0.5.6 --without-http_limit_zone_module
Limit Conn Limit concurrent connections based on a variable. --without-http_limit_conn_module
Log Customize access logs.
Map Set config variables using arbitrary key/value pairs. 0.3.16 --without-http_map_module
Memcached Memcached support. --without-http_memcached_module
Proxy Proxy to upstream servers. --without-http_proxy_module
Referer Filter requests based on Referer header. --without-http_referer_module
Rewrite Request rewriting using regular expressions. --without-http_rewrite_module
SCGI SCGI protocol support. 0.8.42 --without-http_scgi_module
Split Clients Splits clients based on some conditions 0.8.37 --without-http_split_clients_module
SSI Server-side includes. --without-http_ssi_module
Upstream For load-balancing. --without-http_upstream_ip_hash_module (ip_hash directive only)
User ID Issue identifying cookies. --without-http_userid_module
uWSGI uWSGI protocol support. 0.8.40 --without-http_uwsgi_module
X-Accel X-Sendfile-like module.
proxy模块中常用的指令时proxy_pass和proxy_cache.
nginx的web缓存功能的主要是由proxy_cache、fastcgi_cache指令集和相关指令集完成,proxy_cache指令负责反向代理缓存后端服务器的静态内容,fastcgi_cache主要用来处理FastCGI动态进程缓存(这里我不是很清楚这两个指令的区别,好像功能上都差不多,尤其后面这句话的意思,是我翻译过来的)。
确认proxy模块安装好后,下面对nginx的配置文件进行设置,重点部分如标红字体所示。
这是我的nginx.conf配置文件。
user www-data;
worker_processes 1;
#error_log logs/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" "$host"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip
#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end
## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}
## Start www.quancha.cn ##
server {
listen 80;
server_name *.quancha.cn;
access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;
## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200;
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##
}
配置文件中以proxy_开头的指令我们大都可以字面意思得到理解。请务必注意一点proxy_cache_path和proxy_temp_path设置的目录需要在同一分区,因为它们之间是硬链接的关系。
最后启动nginx,来迎接着激动人心的时刻吧。我已经迫不及待了。如果文章哪里有问题或者你遇到了什么麻烦,可以留言让我知道。
发表评论
-
Nginx模块fastcgi_cache的几个注意点
2014-09-17 11:01 570原文地址:http://www.cnxct ... -
[nginx] nginx缓存cache的几种方式
2014-09-15 13:57 778原文地址:http://bbs.linuxtone.org/t ... -
NGINX的流媒体插件 nginx-rtmp-module
2014-09-15 12:03 621http://www.oschina.net/p/nginx- ... -
nginx 自定义 header
2014-09-15 11:56 752$http_HEADER The value of the H ... -
nginx反向代理proxy_set_header自定义header头无效
2014-09-15 11:52 1317原文地址:http://www.ttlsa.com/nginx ... -
nginx 自定义协议 扩展模块开发
2014-09-15 09:33 960原文地址:http://blog.chin ... -
Nginx模块开发入门
2014-09-14 10:05 589原文地址:http://kb.cnblogs.com/page ... -
nginx log_format 记录自定义header信息
2014-09-12 17:53 2161原文地址:http://notelifes.com/2013/ ... -
在Nginx中记录自定义Header
2014-09-12 17:48 811原文地址:http://gunner.me ... -
nginx log 记录请求的头信息
2014-09-12 14:44 1046记录访问的log,为了在 ... -
nginx官网文档地址
2014-09-10 10:33 532原文地址:http://wiki.nginx.org/Ngin ... -
Nginx配置反向代理时cache缓存的使用方法
2014-09-10 10:32 864原文地址:http://www.server110.com/n ... -
Windows下Nginx+PHP5(FastCgi)安装配置详解
2014-08-25 16:46 829源文地址:http://www.china ... -
nginx 多域名虚拟主机配置 (nginx如何绑定多个域名)
2014-08-25 14:33 361原文地址:http://wenku.baidu.com/lin ... -
实例讲解Nginx下的rewrite规则
2014-06-17 14:44 750一.正则表达式匹配, ... -
nginx cache静态化+tmpfs 高性能cdn方案 原创-胡志广
2014-02-25 18:04 814nginx cache静态化+tmpfs 高性能cdn方案 原 ...
相关推荐
本篇文章主要介绍了Nginx启用proxy_cache缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ngx_cache_purge 是 nginx 模块,此模块可以清理 nginx 的 FastCGI、proxy、 SCGI 和 uWSGI 的缓存。配置指令(相同位置语法)fastcgi_cache_purgesyntax: fastcgi_cache_purge on|off|<method> [from all|<ip> [.....
前言:之前使用官方的nginx proxy_cache作为CDN静态缓存发现该模块清理缓存是一个大问题,只能一条一条url的清除,为此写了一个批量清除缓存的脚本 主要功能包括: 1.按文件类型清理 2.按具体的文件名清理 3.按网站...
碰到问题:移动用户访问web服务器www.osyunwei.com很慢解决办法:1、在移动机房放置一台nginx反向代理服务器2、通过域名DNS智能解析,所有移动用户访问www.osyunwei.com时解析到nginx反向代理服务器3、nginx反向代理...
很多情况是php生成的动态内容proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。proxy_cache缓存设置复制代码 代码...
利用Nginx的proxy_cache搭建缓存服务器一:编译ngx_cache_purge 1、Nginx的Proxy_cache是根据Key值md5哈希存储缓存,支持任意的Key,例如你可以根据”域名、URI、参数”组合成key,也支持非200状态码,如404/302等...
很多情况是php生成的动态内容proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。proxy_cache缓存设置#注:proxy_...
proxy_cache_path /data/nginx/cache/one levels=1:2 keys_zone=one:10m max_size=10g; proxy_cache_key $host$request_uri; server { server_name www.jb51.net jb51.net; root /home/www.jb51.net/web; index ...
nginx使用proxy_cache将用户的请求缓存到本地一个目录。下一个相同请求可以直接调取缓存文件,就不用去请求服务器了。 毕竟,IO密集型服务的处理是nginx的强项。 二、如何进行设置 先上个栗子: http{ proxy_...
项目的页面需要加载很多数据,也不是经常变化的,不涉及个性化定制,为每次请求去动态生成数据,性能比不上根据请求路由和参数缓存一下结果,使用 Nginx 缓存将大幅度提升请求速度。 基础 只需要配置 proxy_cache_...
proxy_cache_valid 200 304 4h; expires 4h; } proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; location /app/ { # 动态页面,交给tomcat处理 proxy_pass ...
主要介绍了Nginx服务器上搭建图片缓存服务的基本配置解析,分别介绍了通过proxy_store模块和proxy_cache模块两种方式的配置,需要的朋友可以参考下
Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。 Nginx 0.8.32...
19.Nginx+keepalived+proxy_cache配置高可用Nginx群集和高速缓冲 20.Nginx最难一战 优化指南 21.确保Nginx安全的10大关键技巧 Nginx变量详解 Nginx模块与实践案例 提供了目前所有互联网公司都在用的51个案例 常见...
proxy_cache_path /nginx/cache/path levels=1:2 keys_zone=cache_test:2048m inactive=7d max_size=10g; ...... location ~ .(gif|jpg|jgep|png)$ { proxy_pass http://upstreams; proxy_ignore_headers X-...
第1部分 Nginx服务器 第1章 Nginx的功能 ...第53章 缓存技术――proxy_cache 第54章 缓存技术――proxy_store 第55章 缓存技术――Memcached 第56章 缓存技术――NCACHE 第57章 缓存技术――Varnish
第1部分 Nginx服务器 第1章 Nginx的功能 ...第53章 缓存技术――proxy_cache 第54章 缓存技术――proxy_store 第55章 缓存技术――Memcached 第56章 缓存技术――NCACHE 第57章 缓存技术――Varnish
第1部分 Nginx服务器 第1章 Nginx的功能 ...第53章 缓存技术――proxy_cache 第54章 缓存技术――proxy_store 第55章 缓存技术――Memcached 第56章 缓存技术――NCACHE 第57章 缓存技术――Varnish