大网数据广告活动
服务器运维问答

Handling caching with NGINX when cookies are being set by the server

· 52CE 我爱测

I've had very good performance results using NGINX microcaching . However, I'm still not sure how to best handle server responses that have the Set-Cookie header. Can responses from the origin server with Set-Cookie header be cac…

问题原文

原帖链接 · luqo33 · CC BY-SA 3.0

I've had very good performance results using NGINX microcaching.

However, I'm still not sure how to best handle server responses that have the Set-Cookie header.

  • Can responses from the origin server with Set-Cookie header be cached by NGINX at all?

If the answer is negative and Set-Cookie responses should always by-pass the caching layer, this would have very negative consequences on performance.

For example, when you visit an avarege e-commerce site running WooCommerce, theses cookies would be set upon your first visti:

Set-Cookie:PHPSESSID=xxyy

Set-Cookie:wp_woocommerce_session_xx=yy

Should Set-Cookie responses be excluded from caching this would mean that cached content would never be served for a first visit in any e-commerce store.

Also, when browsing products on a WooCommerce store, the plugin would set woocommerce_recently_viewed=xxxx cookie. woocommerce_recently_viewed cookie would be updated on each product view so all subsequent requests to other products would cause the server to include the Set-Cookie header.

  • Can caching be enabled in such scenario at all?

Some default caching configuration that I'm using with my NGINX includes this:

  if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
  }

If I include woocommerce_recently_viewed cookie in the list, the cache would be by-passed most of the time.

  • What's the best practice handling caching and cookies in a scenario like this?

已采纳解答

原帖链接 · DerfK · CC BY-SA 3.0

In general caching works poorly with dynamic content, especially with content that contains things like "recently viewed" that will change every singe page load. If you really want to use caching then the first step is to disable that plug in so that there's a theoretical chance that someone might be served the same page twice. The second step is to only start a session when someone does something that requires one: logs in, puts an item in their cart, etc. From that point on, you will have to stop caching once the session cookie has been created (otherwise, if the customer only hits the cache for a while, the session will expire on both the customer and the server sides).

I think for content like this caching is best done in the application itself. The application should understand which parts of the webpage are dynamic and need to be regenerated every page load, and can assemble the webpage from new and used parts to give the user the correct response.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。