malayter at gmail.com
Tue Feb 2 17:43:11 MSK 2010
On Mon, Feb 1, 2010 at 11:35 AM, Igor Sysoev <igor at sysoev.ru> wrote:
>> All pages on this site have a login option so I guess caching our site isn't
>> a good idea.
> Yes, even if you cache them properly in nginx, they will have poor hit ratio.
> However, you may try to cache non-personalized pages.
You can cache pages for "public" users that aren't logged in, and then
serve the pages directly without caching to users who are logged in.
This can be done simply by simply changing the URI in your application
For example, anonymous users would hit "http://example.com/index.php",
and the application could return a "Cache-Control:
public,max-age=3600". After a user logs in, you can simply redirect
them to "http://example.com/loggedin/index.php". This virtual diectory
can run the same code, but instead it sets a "Cache-Control: private,
max-age=0" header. So logged in users would not received cached pages.
Another option in nginx would be to use $http_cookie (or a variable
based on it) as part of the proxy_cache_key. Users with an empty
cookie would get the cached page for public users, while users that
have logged in and have a session cookie would be proxied directly to
the back-end. Again, you should set appropriate "Cache-Control"
headers in the application for anonymous versus logged-in users.
Finally, all of your static images, .js files, and CSS files should
have appropriate cache-control headers, so they can be cached for both
logged-in and anonymous users. As Igor points out, this only makes
sense if those files are not already "local" to the nginx server, but
are instead being served from back-end via proxy. You should set
Cache-Control headers on these files even if you aren't using nginx
proxy_cache at all, since you still want to take advantage of any
caches "downstream" from you (such as at the user's ISP, or in their
corporate datacenter). These shared caches are actually more common
than people think, and many are transparent to end-users. When we
first implemented cache-control headers on static files many years
ago, our bandwidth bill dropped by 75% in one month.
More information about the nginx