首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Nginx上缓存Symfony 2

在Nginx上缓存Symfony 2
EN

Stack Overflow用户
提问于 2013-11-08 22:56:49
回答 1查看 2.7K关注 0票数 2

我从Apache2+ Varnish的设置迁移到了Nginx,我有点纠结于在这个设置中我应该如何设置/使用ESI和fastcgi_cache。

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

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

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-26 20:52:20

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

代码语言:javascript
复制
Cache-Control: no-cache

如果你想让nginx缓存页面,你必须修改那些头文件。

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19862001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档