网站内容方案(凡客诚品:浏览量大,数据存储的特点及解决方法)
优采云 发布时间: 2021-11-03 20:15网站内容方案(凡客诚品:浏览量大,数据存储的特点及解决方法)
一旦掌握了这个方案,就可以轻松实现“京东商城”、“VANCL诚品”、“走秀网”等网站
这些网站的特点是:浏览量大,数据存储主要是图片,登录/注册和购物车,用户中心访问量仅占5%,使用负责任的平衡很容易解决。
静态网站不能避免使用ajax进行局部更新,ajax请求也要考虑缓存问题
首次访问服务器
访问www服务器nginx判断文件是否存在,如果文件不存在就去cms服务器查找,如果存在则返回www服务器显示。如果cms上不存在文件,如果存在,cms服务器使用rewrite生成文件,同时将内容返回给www服务器,www将内容缓存在它自己的服务器并显示内容
第二次访问
访问www服务器nginx判断文件是否存在,如果文件不存在就去cms服务器查找,如果存在则返回www服务器显示。如果cms上不存在文件,如果存在,cms服务器使用rewrite生成文件,同时将内容返回给www服务器,www将内容缓存在其上自己的服务器,并显示内容2. cdn
如何使用cdn缓存你的网站内容
有几种方法可以在 cdn 节点上缓存您的网页:
让cdn的客服帮你配置缓存规则。他们喜欢一刀切。比如所有的html在其管理后台缓存2小时,使用常规配置缓存时间。他们一般不提供这个,有些公司的CDN会提供这个功能。很方便。通过HTTP头自己控制缓存时间,一般使用max-age / s-maxage / Last-Modified来确定缓存时间
我更喜欢最后一种,通常我们同时使用max-age和s-maxage,这样我就可以根据我的意图决定何时缓存文件。
3. www 服务器
下面给出了一个简化的配置示例
如果文件不存在,则连接后端cms服务器生成文件,显示,并添加缓存。生成的文件会从cms同步到www服务器。
您可以使用
rsync 同步解决方案 nfs/samba/gluster 共享解决方案 iscsi 共享存储解决方案 分布式文件系统解决方案
参考阅读:分布式文件系统、Netkiller Linux Storage Letters
upstream cms.mydomain.com {
server 192.168.2.11 weight=5 max_fails=3 fail_timeout=30s;
server 192.168.2.21 weight=5 max_fails=3 fail_timeout=30s;
server 192.168.2.23 backup;
server 192.168.2.23 down;
}
server {
listen 80;
server_name www.mydomain.com;
charset utf-8;
access_log /var/log/nginx/www.mydomain.com.access.log main;
location / {
root /www/mydomain.com/www.mydomain.com;
index index.html index.htm;
if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png|html)$") {
expires 1d;
}
if ($request_uri ~* "\.(xml|json)$") {
expires 1m;
}
valid_referers none blocked *.mydomain.com;
if ($invalid_referer) {
#rewrite ^(.*)$ http://www.mydomain.com/cn/$1;
return 403;
}
proxy_intercept_errors on;
if (!-f $request_filename) {
proxy_pass http://cms.mydomain.com;
break;
}
}
#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 /usr/share/nginx/html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
4. cms 服务器
cms 内容管理系统的主要功能
内容分类管理内容模板管理内容编辑发布内容生成
服务应该被执行
当发现目录中的文件不存在时,通过rewrite生成html,可以根据需要生成html页面。当页面更新时,应该通过api刷新cdn缓存。图片的版本要增加一个,把页面分成多个模块,通过SSI组装页面,避免大改版时全站生成HTML。避免使用seesion技术,这样可以在负载均衡时使用最小连接数算法
例如:
rewrite ^/product/(phone|notebook)/(\d+).html /product/$1.php?id=$2 last;
URL 是唯一的,URL 设计要考虑唯一性。不要有相同的 URL 来处理两个任务。例如,在下面的例子中,每个用户的*敏*感*词*都有一个 URL,当它被访问时,它可以缓存在 CDN 或用户浏览器上。
http://www.mydomain.com/profile/neo.html
http://www.mydomain.com/profile/jam.html
server {
listen 80;
server_name www.mydomain.com;
#charset koi8-r;
access_log /var/log/nginx/www.mydomain.com.access.log main;
location / {
root /www/mydomain.com/www.mydomain.com;
index index.html;
}
}
server {
listen 80;
server_name cms.mydomain.com;
charset utf-8;
access_log /var/log/nginx/cms.mydomain.com.access.log main;
location / {
root /www/mydomain.com/cms.mydomain.com;
index index.html index.php;
}
location ~ ^/(cn|tw)/(comment|affiche)/.*\.html {
root /www/mydomain.com/www.mydomain.com;
if (!-f $request_filename) {
rewrite ^/(cn|tw)/(comment|affiche)/(\d+).html /publish/$2.php?id=$3&lang=$1 last;
}
}
location /xml/ {
root /www/mydomain.com/www.mydomain.com/xml;
}
location ~ ^/(config|include|crontab)/ {
deny all;
break;
}
#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 /usr/share/nginx/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 /www/mydomain.com/cms.mydomain.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/mydomain.com/cms.mydomain.com$fastcgi_script_name;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT /www/mydomain.com/cms.mydomain.com;
fastcgi_param HOSTNAME cms.mydomain.com;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
5. img
server {
listen 80;
server_name img.mydomain.com;
charset utf-8;
access_log /var/log/nginx/img.mydomain.com.access.log main;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 7d;
}
location ~ .*\.(js|css)$
{
expires 1d;
}
location ~ .*\.(html|htm)$
{
expires 15m;
}
location / {
root /img/mydomain.com/img.mydomain.com;
index index.html;
rewrite "/theme/([0-9] {4})([0-9] {2})([0-9] {2})/(.+)\.(.+)\.(.+)" /theme/$1/$2/$3/$4.$6;
rewrite "/news/([0-9] {4})([0-9] {2})([0-9] {2})/(.+)\.(.+)\.(.+)" /news/$1/$2/$3/$4.$6;
rewrite "/product/([0-9] {4})([0-9] {2})([0-9] {2})/(.+)\.(.+)\.(.+)" /product/$1/$2/$3/$4.$6;
}
}
/theme/2012/08/15/images.1.jpg 实际上是 /theme/2012/08/15/images.jpg 文件
/theme/2012/08/15/images.2.jpg 也是 /theme/2012/08/15/images.jpg
/theme/2012/08/15/images.3.jpg 也是 /theme/2012/08/15/images.jpg
但是CDN和您的浏览器每次都会下载新文件,所以您只需要更新CDN中的html页面即可。不要关注图片,你的浏览器会下载新地址的图片。这就解决了麻烦的刷新工作。
6. Ajax 部分更新和缓存
例如,对于我的新闻评论页面,我需要使用ajax技术来显示用户的回复。Ajax 加载 json 数据,然后在本地更新。我缓存了 1 分钟。
if ($request_uri ~* "\.(xml|json)$") {
expires 1m;
}
如果有新的提交,我们可以给json添加版本控制。例如: