【问题标题】:WooCommerce NGINX caching and wc-session cookie add to cart issueWooCommerce NGINX 缓存和 wc-session cookie 添加到购物车问题
【发布时间】:2019-01-24 11:00:18
【问题描述】:

我正在尝试将我的 WooCommerce v3.4 站点安装到使用 Ubuntu 18.04、PHP7.1 和 NGINX 构建的新 VPS 服务器上。我对 WooCommerce 会话和 cookie 的问题。 cookie 不会针对不同的用户分开。如果有人添加产品或更新它为所有用户添加的购物车。

这是我的服务器配置文件,请帮我解决这个问题。

server {
        root /var/www/mydomain.com/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name mydomain.com www.mydomain.com;
        client_max_body_size 64m;


        location / {
             try_files $uri $uri/ /index.php$is_args$args;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }



        set $skip_cache 0;
        if ($request_method = POST) {
            set $skip_cache 1;
        }
        if ($query_string != "") {
            set $skip_cache 1;
        }

        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }

        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
        if ($request_uri ~* "(/shop.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
            set $skip_cache 1;
        }
        if ( $arg_add-to-cart != "" ) { 
          set $skip_cache 1;
        }

        if ( $cookie_woocommerce_items_in_cart ) {
            set $skip_cache 1;
        }   


        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;
            fastcgi_cache cachezone;
            include fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_connect_timeout 60s;
            fastcgi_send_timeout 60s;
            fastcgi_read_timeout 60s;
            fastcgi_buffers 256 16k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;

            set $rt_session "";
            if ($http_cookie ~* "wc_session_cookie_[^=]*=([^%]+)%7C") {
                set $rt_session wc_session_cookie_$1;
            }

            if ($skip_cache = 0 ) {
                more_clear_headers "Set-Cookie*";
                set $rt_session "";
            }
        }




        location ~ /\.ht {
                deny all;
        }

        location /phpmyadmin {
            root /usr/share/;
            index index.php;
            try_files $uri $uri/ =404;
            location ~ ^/phpmyadmin/(doc|sql|setup)/ {
                deny all;
            }

        location ~ /phpmyadmin/(.+\.php)$ {
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            include snippets/fastcgi-php.conf;
          }
        }


        gzip on;
        gzip_min_length  1100;
        gzip_buffers  4 32k;
        gzip_types    text/plain application/x-javascript text/xml text/css;
        gzip_vary on;

}

【问题讨论】:

  • 您好,您解决了这个问题吗?如何解决的?我也遇到了同样的问题。
  • 不!未能解决这就是为什么我使用 Apache 而不是 NGINX

标签: php wordpress nginx woocommerce


【解决方案1】:

我们已经修好了。

问题是缓存文件包含一些标头,包括“Set-Cookie”,因此如果您检查 NGINX 缓存文件,您可能会在那里找到 wc_session_cookie_ cookie。

我们解决这个问题的方法是使用以下代码排除 cookie 标头:

if( $skip_cache = 0 ) {
   more_clear_headers "Set-Cookie";
}

所以现在每个用户会话都是独立的,但现在我们面临另一个问题,即 WC 优惠券创建重复会话,但我们正在使用 JS 修复它。

希望你会发现这很有用。

你可以在这里找到资源:

https://github.com/openresty/headers-more-nginx-module#more_clear_headers

https://github.com/openresty/headers-more-nginx-module#more_clear_input_headers

【讨论】:

  • 您好@RoelMagdaleno,感谢您的回答,我遇到了完全相同的问题。因此,某些用户将获得其他用户界面/授权,因为他们正在接收缓存的标头/cookie。你用过“more_clear_input_headers”吗?我想知道我是否应该使用它,因为 Fastcgi 实际上只缓存了 GET/HEAD。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2018-08-10
  • 2015-08-18
  • 2014-02-25
  • 1970-01-01
相关资源
最近更新 更多