2

我从 Apache 2 + Varnish 的设置转移到单独的 Nginx,我有点坚持在这个设置中我应该如何设置/使用 ESI 以及 fastcgi_cache。

首先,ESI 的想法是我们在服务器前面设置一个反向代理层来缓存页面的可缓存部分,然后使用 esi 检索动态部分。在我之前的设置中,Varnish 充当反向代理,Apache 仅在必要时处理 esi 请求。

我的问题是,现在 Nginx 作为这里的唯一服务器,我该如何让它工作?我需要设置另一个作为反向代理服务器运行的 Nginx 实例吗?我找不到这方面的任何文件。

第二个问题是关于 fastcgi_cache。我已按如下所述进行设置,但缓存似乎对我不起作用,没有填充缓存文件,我总是得到“MISS”。我想知道是不是因为我需要在每个控制器中设置 max-age/shared-max-age 才能使每个控制器工作?

fastcgi_cache_path /run levels=1:2 keys_zone=www_mysite_com:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/mysite.com/w/w/w/www/web;
        index index.php index.html index.htm;

        # Make site accessible from http://www.mysite.com
        server_name www.mysite.com;

        # Specify a character set
        charset utf-8;

        # strip app.php/ prefix if it is present
        rewrite ^/app\.php/?(.*)$ /$1 permanent;

        # h5bp nginx configs
        # include conf/h5bp.conf;

        location / {
                index app.php;
                try_files $uri @rewriteapp;
        }

        location @rewriteapp {
                rewrite ^(.*)$ /app.php/$1 last;
        }

        # Deny access to .htaccess
        location ~ /\.ht {
            deny all;
        }

         # Don't log robots.txt or favicon.ico files
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        # 404 errors handled by our application, for instance Symfony
        error_page 404 /app.php;

        # pass the PHP scripts to FastCGI server from upstream phpfcgi
        location ~ ^/(app|app_dev|backend/app|backend/app_dev|config)\.php(/|$) {
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;

                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME web/$fastcgi_script_name;
                fastcgi_param  HTTPS off;
                fastcgi_cache www_mysite_com;
                fastcgi_cache_valid 200 60m;
        }

        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
}
4

1 回答 1

4

默认情况下,来自 Symfony 2 应用程序的响应有一个禁用缓存的缓存控制标头:

Cache-Control: no-cache

如果您希望 nginx 缓存页面,则必须更改这些标头。

您可以在文档中找到有关缓存的一般信息

最简单的解决方案是使用SymfonyFrameworkExtraBundle(如果您使用 SF2 标准版,您已经拥有它)并在您的控制器和/或操作上使用注释来指定缓存头。您可以在 @Cache 注释的文档中找到有关此方法的更多信息。

于 2014-03-26T12:52:19.547 回答