【问题标题】:how caching via nginx result of prerender.io如何通过 prerender.io 的 nginx 结果缓存
【发布时间】:2016-06-15 07:10:28
【问题描述】:

我们有一个带有 Angular 前端的网站,需要为搜索机器人和其他应用程序(如 Skype)进行渲染,这些应用程序可以预览页面。 我们使用 nginx,它设置用于代理从机器人到 prerender 的请求,它安装在我们的服务器上。但在这种情况下,预渲染一个页面大约需要 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|

代理缓存.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/

【问题讨论】:

    标签: angularjs caching nginx prerender


    【解决方案1】:

    我解决了这个问题。它需要添加另一个忽略标题 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;
                }
    

    【讨论】:

      【解决方案2】:

      向预渲染服务器本身添加缓存相当容易,s3 和内存缓存可以工作out of the box

      如果您需要 nginx 来处理缓存,我认为如果您将其放在问题标题中,您将获得更好的答案。

      【讨论】:

        猜你喜欢
        • 2018-04-08
        • 2014-02-18
        • 2017-04-09
        • 2019-02-10
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多