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

时间:2018-08-17 11:45:18

标签: php wordpress nginx woocommerce

我试图将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;

}

1 个答案:

答案 0 :(得分:3)

我们已经修复。

问题在于缓存文件包含一些标头,包括“ Set-Cookie”,因此,如果您检查NGINX缓存文件,则可能会在其中找到var name1 = $('#firstname').val(); cookie。

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

wc_session_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

相关问题