bcremer / shopware-with-nginx

Running shopware using NGINX
http://shopware.com
BSD 2-Clause "Simplified" License
95 stars 46 forks source link

enable shopware cache invalidation via http BAN method #48

Closed wbob closed 5 years ago

wbob commented 5 years ago

Nginx won't handle BAN (or POST) on static resources and return a 405 Method Not Allowed. In general, invalidation still works with invalidateWithStore(), but not via invalidateWithBANRequest(). See invalidate() in the HttpCache Plugin when each is used.

Users will see a Reverse proxy returned invalid status code in their logs.

$ curl -sI -X BAN http://example.com/
HTTP/1.1 405 Not Allowed
Server: nginx

To forward the invalidation to PHP, a redirect to shopware.php is used, ending up being handled by fastcgi. According to if-is-evil, the use of rewrite .. last within if is declared "100% safe".

last will stop rewrite processing within the block and search for a new matching location, being \.php$.

if ($request_method = BAN) {
    rewrite ^ /shopware.php last;
}

PHP will handle the request and Nginx will return a 200

$ curl -sI -X BAN http://example.com/
HTTP/1.1 200 OK
Server: nginx

Invalidation now resets Age, prompts cache to re-fetch

$ curl -sI 'http://example.com/product-123'
HTTP/1.1 200 OK
...
Age: 5
X-Symfony-Cache: HEAD /product-123: fresh
...
$ curl --header 'x-shopware-invalidates: a123' -X BAN http://example.com/
$ curl -sI 'http://example.com/product-123'
HTTP/1.1 200 OK
...
X-Shopware-Cache-Id: ;a123;
...
Age: 0
X-Symfony-Cache: HEAD /product-123: miss, store

the example dump in the head of shopware.php verifies the internal nginx rewrite will not alter the method

print_r($_SERVER); die();
...
$ curl -s -X BAN 'http://example.com/' | grep BAN
    [REQUEST_METHOD] => BAN

Will very likely solve these issues: forum.shopware.com#35300 forum.shopware.com#51742 forum.shopware.com#56593 forum.shopware.com#56445

I commented extensively to avoid configuration errors, if you prefer to have it terse I'll remove the bullet points.

bcremer commented 5 years ago

HI @wbob, thanks for this PR.

I'm currently not running a Shopware Instance so I have to depend on external testing by the Shopware community to verify this change. If someone from @FriendsOfShopware @shopware has time to test this PR, that would be greatly appreciated.

shyim commented 5 years ago

I will test this tomorrow :) I had this situation already often in Development Support, and we suggested the users to set A alternative Proxy Url to the shopware.php and it solved it.

bcremer commented 5 years ago

Thanks @shyim for stepping in :+1:

shyim commented 5 years ago

@bcremer tested looks good for me :)

bcremer commented 5 years ago

@wbob Thanks for this PR. @shyim Thanks for testing.

Merged it right away.

wbob commented 5 years ago

thanks for looking into it. Btw, nice to have shopware/shopware#1908 in >= 5.5.5 :+1: A schema redirect will be less likely if no proxyurl is configured explicitly.