网站内容方案(凡客诚品:浏览量大,数据存储的特点及解决方法)
优采云 发布时间: 2022-02-07 18:29网站内容方案(凡客诚品:浏览量大,数据存储的特点及解决方法)
一旦掌握了这个方案,就可以轻松实现“京东商城”、“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 headers控制缓存时间,一般使用max-age / s-maxage / Last-Modified 来确定缓存时间
我更喜欢最后一种,通常我们同时使用 max-age 和 s-maxage,这样我就可以根据自己的意图决定何时缓存文件。
3. www 服务器
下面给出一个简化的配置示例
如果文件不存在,则连接后端cms服务器生成文件,显示,并添加缓存。生成的文件将从 cms 同步到 www 服务器。
您可以使用
rsync 同步方案 nfs/samba/gluster 共享方案 iSCSI 共享存储方案 分布式文件系统方案
另请阅读:分布式文件系统、Netkiller Linux 存储说明
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 添加版本控制,例如: