如何通过 prerender.io 的 nginx 结果进行缓存
how caching via nginx result of prerender.io
我们的网站在 angular 上有前端,需要为搜索机器人和其他应用程序(如 Skype)呈现页面预览。
我们使用 nginx,它设置用于代理来自机器人的请求以预呈现,它安装在我们的服务器上。但在那种情况下,预呈现其中一页大约需要 15 秒。
所以,问题是如何设置预渲染的缓存结果?
我已经试过了:
将缓存设置放入 frontend.conf
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m max_size=1G;
proxy_temp_path /var/lib/nginx/proxy 1 2;
server {
location @prerender {
.....................
proxy_cache STATIC;
proxy_cache_valid 1d;
if ($prerender = 1) {
rewrite .* /$scheme://$host:$server_port$request_uri? break;
proxy_pass http://10.0.2.2:3000;
}}}
其中 10.0.2.2 服务器正常工作 prerender.io
我尝试通过另一个 nginx 来完成此操作,它的设置类似于缓存代理。在 frotnend.conf 我把所有的缓存设置注释掉,放到其他的nginx中。但是我还是有同样的问题,页面渲染需要15秒,而且nginx没有做任何缓存。
UDP.
我尝试了nginx的另一种配置,但仍然有问题。
架构看起来像
web-browser(http://myapp.local) > |AppServer(frontend) is a virtual Server|(proxy_pass) > to > |nginx with proxy cache| > to > |prerender|
代理-cache.conf
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m max_size=1G;
proxy_temp_path /var/lib/nginx/proxy 1 2;
server {
..............................
location / {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_ignore_headers X-Accel-Expires;
proxy_ignore_headers Expires;
proxy_cache_methods GET;
proxy_cache_valid any 1d; # 200
#proxy_cache_key $host$uri$is_args$args;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_pass http://127.0.0.1:3000;
}}
并且我为缓存配置了日志记录。当我在日志中向服务器发出请求时,我得到:
GET /http://myweb.local:80/ HTTP/1.0 "302" 20 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "0.388" "0.389"
GET /http://myweb.local:80/en/ HTTP/1.0 "200" 14685 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "1.261" "1.263"
GET /http://myweb.local:80/ HTTP/1.0 "302" 20 "-" "10.0.2.2" "skype" "-" "HIT" "-" "-" "0.001"
GET /http://myweb.local:80/en/ HTTP/1.0 "200" 14689 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "1.249" "1.251"
在预渲染日志中:
2016-06-15T14:05:57.880Z getting http://myweb.local:80/en/
2016-06-15T14:05:59.131Z got 200 in 1251ms for http://myweb.local:80/en/
2016-06-15T14:06:00.332Z getting http://myweb.local:80/en/
2016-06-15T14:06:01.885Z got 200 in 1553ms for http://myweb.local:80/en/
向预渲染服务器本身添加缓存相当容易,s3 和内存缓存工作 out of the box。
如果您需要 nginx 来处理缓存,我认为如果将其放在问题标题中,您获得答案的机会会更大。
我解决了这个问题。它需要添加另一个忽略 headers proxy_ignore_headers Set-Cookie; 。因此通过 nginx 进行缓存预呈现的配置将是:
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=2g;
server {
.......................
location / {
try_files $uri @prerender;
}
location @prerender {
set $prerender 0;
.......................
proxy_cache STATIC;
proxy_cache_valid any 1d;
proxy_ignore_headers Cache-Control;
proxy_ignore_headers X-Accel-Expires;
proxy_ignore_headers Set-Cookie;
proxy_ignore_headers Expires;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
if ($prerender = 1) {
rewrite .* /$scheme://$host:$server_port$request_uri? break;
proxy_pass http://10.0.2.2:3000;
}
我们的网站在 angular 上有前端,需要为搜索机器人和其他应用程序(如 Skype)呈现页面预览。 我们使用 nginx,它设置用于代理来自机器人的请求以预呈现,它安装在我们的服务器上。但在那种情况下,预呈现其中一页大约需要 15 秒。
所以,问题是如何设置预渲染的缓存结果?
我已经试过了: 将缓存设置放入 frontend.conf
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m max_size=1G;
proxy_temp_path /var/lib/nginx/proxy 1 2;
server {
location @prerender {
.....................
proxy_cache STATIC;
proxy_cache_valid 1d;
if ($prerender = 1) {
rewrite .* /$scheme://$host:$server_port$request_uri? break;
proxy_pass http://10.0.2.2:3000;
}}}
其中 10.0.2.2 服务器正常工作 prerender.io
我尝试通过另一个 nginx 来完成此操作,它的设置类似于缓存代理。在 frotnend.conf 我把所有的缓存设置注释掉,放到其他的nginx中。但是我还是有同样的问题,页面渲染需要15秒,而且nginx没有做任何缓存。
UDP.
我尝试了nginx的另一种配置,但仍然有问题。 架构看起来像
web-browser(http://myapp.local) > |AppServer(frontend) is a virtual Server|(proxy_pass) > to > |nginx with proxy cache| > to > |prerender|
代理-cache.conf
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m max_size=1G;
proxy_temp_path /var/lib/nginx/proxy 1 2;
server {
..............................
location / {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_ignore_headers X-Accel-Expires;
proxy_ignore_headers Expires;
proxy_cache_methods GET;
proxy_cache_valid any 1d; # 200
#proxy_cache_key $host$uri$is_args$args;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_pass http://127.0.0.1:3000;
}}
并且我为缓存配置了日志记录。当我在日志中向服务器发出请求时,我得到:
GET /http://myweb.local:80/ HTTP/1.0 "302" 20 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "0.388" "0.389"
GET /http://myweb.local:80/en/ HTTP/1.0 "200" 14685 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "1.261" "1.263"
GET /http://myweb.local:80/ HTTP/1.0 "302" 20 "-" "10.0.2.2" "skype" "-" "HIT" "-" "-" "0.001"
GET /http://myweb.local:80/en/ HTTP/1.0 "200" 14689 "-" "10.0.2.2" "skype" "-" "MISS" "127.0.0.1:3000" "1.249" "1.251"
在预渲染日志中:
2016-06-15T14:05:57.880Z getting http://myweb.local:80/en/
2016-06-15T14:05:59.131Z got 200 in 1251ms for http://myweb.local:80/en/
2016-06-15T14:06:00.332Z getting http://myweb.local:80/en/
2016-06-15T14:06:01.885Z got 200 in 1553ms for http://myweb.local:80/en/
向预渲染服务器本身添加缓存相当容易,s3 和内存缓存工作 out of the box。
如果您需要 nginx 来处理缓存,我认为如果将其放在问题标题中,您获得答案的机会会更大。
我解决了这个问题。它需要添加另一个忽略 headers proxy_ignore_headers Set-Cookie; 。因此通过 nginx 进行缓存预呈现的配置将是:
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=2g;
server {
.......................
location / {
try_files $uri @prerender;
}
location @prerender {
set $prerender 0;
.......................
proxy_cache STATIC;
proxy_cache_valid any 1d;
proxy_ignore_headers Cache-Control;
proxy_ignore_headers X-Accel-Expires;
proxy_ignore_headers Set-Cookie;
proxy_ignore_headers Expires;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
if ($prerender = 1) {
rewrite .* /$scheme://$host:$server_port$request_uri? break;
proxy_pass http://10.0.2.2:3000;
}