php - WooCommerce NGINX 缓存和 wc-session cookie 添加到购物车问题

标签 php wordpress nginx woocommerce

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

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

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;

}

最佳答案

我们已经修复了。

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

我们解决这个问题的方法是用这段代码排除 cookie header :

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

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

希望您会发现它有用。

您可以在这里找到资源:

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

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

关于php - WooCommerce NGINX 缓存和 wc-session cookie 添加到购物车问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51894747/

相关文章:

css - Rails 图像 Assets 未加载

php - 我可以使用 phpQuery 找到完全匹配吗?

php - 另一个有 mysqli 问题的初学者

php - WordPress:尝试使用 "get_categories()"获取特定类别 url

node.js - 未处理的拒绝 SequelizeConnectionError : connect ENOENT

c - C中一个奇怪的字符串复制函数

php - 获取 wordpress 中最旧帖子的永久链接

php - 通过 post 传递变量

php - Wordpress 菜单问题

php - 取决于一行中有多少帖子有条件 HTML