网站首页 > 精选教程 正文
高并发网站架构的核心原则其实就一句话"把所有的用户访问请求都尽量往前推",即:能缓存在用户电脑本地的,就不要让他去访问CDN。 能缓存CDN服务器上的,就不要让CDN去访问源(静态服务器)了。能访问静态服务器的,就不要去访问动态服务器。以此类推:能不访问数据库和存储就一定不要去访问数据库和存储。
在主配置文件(nginx.conf)中添加缓存域
astcgi_cache_path /data/nginx/fcgicache levels=1:2 keys_zone=fcache:10m max_size=2g inactive=1d;
fastcgi_cache_path:缓存文件的路径,/dev/shm/为tmfs缓存文件系统,
实际储存在内存中,所以读写IO性能更高。
levels:缓存目录的结构层次,例如1:2,缓存文件会就生成在指定目录的再下两层目录中。
keys_zone:缓存域名称,在vhost内进行缓存时需要调用。
inactive:缓存不活动时间,若缓存内容在指定时间内未被访问将会被清理出缓存域。
2. 站点配置(然后在配置中的server的location中加入)
location /archives/ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /data/webroot/$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache fcache;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_key $request_method$host$request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Cache “$upstream_cache_status”;
}
fastcgi_cache:指定缓存域
fastcgi_cache_methods:指定缓存的请求方式
fastcgi_cache_key:指定缓存文件的标识,这个标识会MD5转码存储在缓存域的目录下
fastcgi_cache_valid:指定缓存状态,例如上文中只缓存响应状态码为200和302的请求所产生的返回页面10分钟
fastcgi_ignore_headers:默认情况下fastcgi_cache会忽略有特殊header的请求,并不进行缓存,官网说明。但当我们添加这个参数后,这些限制将不在存在。
add_header 将会在返回请求的response的header中添加一个X-Cache字段表示是否进行了缓存。如果需要也可以在nginx日志中通过log_format添加$upstream_cache_status字段
·MISS 未命中,请求被传送到后端
·HIT 缓存命中
·EXPIRED 缓存已经过期请求被传送到后端
·UPDATING 正在更新缓存,将使用旧的应答
·STALE 后端将得到过期的应答
fastcgi_keep_conn on; 开启保持连接,性能也有一定的加速作用.
使用ab工具压测网站,1:可以测试再没有缓存之前的访问速度,2:看看缓存是否生效以及生效后的访问速度是否有提升。
Ab压测后已经又缓存数据了
Nginx内置FastCgi缓存是不支持自动清除缓存更新缓存,如果是WordPress程序的可以使用Nginx Helper插件,专为wordpress的nginx fastcgi_cache缓存更新而开发的插件。
WordPress程序安装使用Nginx Helper插件自动清除更新缓存
安装Nginx ngx_cache_purge模块
1:安装Nginx ngx_cache_purge模块
查看ngx_cache_purge是否安装
nginx -V 2>&1 | grep -o ngx_cache_purge
显示ngx_cache_purge表示已经安装
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.9.10.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
tar xzf nginx-1.9.10.tar.gz
cd nginx-1.9.10
nginx -V #查看nginx编译参数,最后加上--add-module=../ngx_cache_purge-2.3
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \
--with-ipv6 --with-http_gzip_static_module --with-http_realip_module \
--with-http_flv_module --with-ld-opt=-ljemalloc \
--add-module=../ngx_cache_purge-2.3
make #编译
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #备份nginx
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 显示ngx_cache_purge表示已经安装成功
不出意外会出现ngx_cache_purge ,表示已经成功在LNMP环境下添加了ngx_cache_purge组件
2:Nginx配置
建议将fastcgi_cache_path设置tmpfs内存中,操作系统不同tmpfs路径也不同,如下:
CentOS:/dev/shm
Ubuntu和Debian:/run/shm
修改nginx虚拟主机配置文件/usr/local/nginx/conf/vhost/linuxeye.com.conf:
fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/vhost/linuxeye_blog.crt;
ssl_certificate_key /usr/local/nginx/conf/vhost/linuxeye_blog.key;
ssl_ciphers "CHACHA20:GCM:HIGH:!DH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
#ssl_stapling on;
#ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
server_name linuxeye.com;
access_log /home/wwwlogs/blog_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/blog;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
3:使nginx配置生效
service nginx reload
重启系统后shm内存中nginx-cache文件夹会丢失,为了使重启生效(自动创建文件夹),修改/etc/init.d/nginx的make_dirs下一行(大概52行)添加:
[ ! -d '/dev/shm/nginx-cache' ] && { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; }
- 上一篇: 如何解决服务器缓存过高
- 下一篇: Nginx系列:图片过滤处理
猜你喜欢
- 2024-09-24 nginx配置Proxy Cache缓存的12个指令
- 2024-09-24 万字长文聊缓存(上)- http缓存
- 2024-09-24 Web缓存投毒实战(二)
- 2024-09-24 架构师成长之路:高层架构设计中如何确定缓存架构(经验干货)
- 2024-09-24 Nginx系列:数据压缩
- 2024-09-24 nginx缓存不起作用问题解决方法
- 2024-09-24 缓存架构系统就谈多级缓存,可我就知Redis!nginx缓存代理助提升
- 2024-09-24 除了负载均衡,Nginx还可以做很多,限流、缓存、黑白名单等
- 2024-09-24 利用nginx设置浏览器协商缓存
- 2024-09-24 NGINX最佳缓存配置策略
你 发表评论:
欢迎- 最近发表
-
- java 服务之间调用(rpc)(java实现服务器)
- Java机器学习库(Java ML)(二、聚类)
- 「Java库」如何使用优秀的加密库Jasypt来保护你的敏感信息?
- GitHub精选 | 轻量级Android和Java日志库
- Java面试官:MySQL binlog 有什么作用?主从延迟的了解么?
- Excel函数核武器库:50个高频场景公式——第二弹
- Excel函数核武器库:50个高频场景公式——第一弹
- Spring Cache高性能缓存库 - Caffeine简介
- Java通过Kafka Streams库来实现数据流处理
- 一连问了好几个大佬,竟然都不知道Redis为什么默认16个数据库?
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)