在Nginx上缓存Symfony 2

在Nginx上缓存Symfony 2,symfony,caching,nginx,Symfony,Caching,Nginx,我从Apache2+Varnish的设置转移到了Nginx,我一直在思考如何在这个设置中设置/使用ESI以及fastcgi_缓存 首先,ESI的思想是在服务器前面设置一个反向代理层来缓存页面的可缓存部分,然后使用ESI检索动态部分。在我之前的设置中,Varnish充当反向代理,Apache只在必要时处理esi请求 我的问题是,现在Nginx作为这里唯一的服务器,我如何让它工作?我需要设置另一个作为反向代理服务器运行的Nginx实例吗?我找不到这方面的任何文件 第二个问题是关于fastcgi_缓存

我从Apache2+Varnish的设置转移到了Nginx,我一直在思考如何在这个设置中设置/使用ESI以及fastcgi_缓存

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

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

第二个问题是关于fastcgi_缓存的。我已经按如下所述进行了设置,但缓存似乎对我不起作用,没有缓存文件填充,而且我总是得到“未命中”。我想知道这是否是因为我需要在每个控制器中设置最大使用年限/共享最大使用年限,以便每个控制器正常工作

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;
}

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

Cache-Control: no-cache
如果希望nginx缓存页面,则必须更改这些标题

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


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

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

Cache-Control: no-cache
如果希望nginx缓存页面,则必须更改这些标题

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

最简单的解决方案是使用(如果使用SF2标准版,则已经有了),并在控制器和/或操作上使用注释来指定缓存头。你可以找到更多关于这种方法的信息