nginx cache. How to define list of urls to cache only? - caching

I have big list of urls to cache, for example:
http://example.com?a=1&b=1
http://example.com?a=2&b=1
etc...
There are no rule to describe these, only by direct listing.
I want my nginx to cache only urls I defined in this list and ignore caching for other GET parameters. Is there a way?

Related

How do I setup PageRules to bypass Caching against an entire image directory?

I'm working with Cloudflare and one of its benefits is caching. There are certain areas of my site that shouldn't be cached as the results need to be seen by the user. I run an online store and images for products are stored in directories which are also designated by the product Id, this means there are quite a few directories and I don't want the images to be cached.
Here are a few example URLs of product images on my website.
www.mysite.co.uk/images/products/87/image300.jpg
www.mysite.co.uk/images/products/88/image300.jpg
www.mysite.co.uk/images/products/94/image300.jpg
www.mysite.co.uk/images/products/27/image300.jpg
To get around the caching of the above directories I tried adding page rules within the Cloudflare dashboard, despite my best efforts I cannot get Cloudflare to stop caching all the images within the products directory.
My first attempt was to use a wildcard and prevent caching across all pages on my site, the pagerule I used was:
mysite.co.uk/*
However, the above rule didn't seem to do anything. I then attempt to get more granular with it and opted for a rule like this:
mysite.co.uk/images/products/*
This rule didn't seem to work either. I then looked at more advanced wildcard use but I fear I got out of my depth:
mysite.co.uk/images/products/*/$1.jpg
Needless to say, the above rule didn't work either. So, my question is, what rule should I use to prevent caching of my product images?
Assuming your site is published at www.mysite.co.uk and the images you don't want to cache are under www.mysite.co.uk/images/products/... then, you would create a page rule such as:
This rule will tell Cloudflare to not store the resources matching the expression on the CDN. You can also change the rule to match www.mysite.co.uk/images/products/*.jpg if you only want to match jpg images under that folder.
Finally: if there are more page rules defines, it is recommended ordering them from most to least specific, as only one rule is matched for every request.

Check if two urls are for the same website

I'm looking for a way to compare two urls. I can do:
URI('http://www.test.com/blabla').host
to have the base name, but this not reliable. For example:
URI('http://www.test.com/blabla').host == URI('http://test.com/blabla').host
returns false, but they can be the same site. To have the IP address is not reliable too because if I do:
IPSocket.getaddress(URI('http://hello.herokuapp.com').host) ==
IPSocket.getaddress(URI('http://test.herokuapp.com').host)
It returns true, but they are not the same site. Is there a more reliable way?
The site under http://foo.com can be the same as under http://www.foo.com, but it can be a totally different site, due to web server configuration. It depends on the DNS config too, which IP points to www and which one to without www.
If you want compare two sites, you need to fetch the content, and compare key parts (using nokogiri for example) about similarities.
Nowadays due to sidebars and news, two consequent request to the same url, gives slight different html responses.

Can Sling mappings be restricted to requests with host header

I would like to selectively apply Sling mappings defined in sling:Mapping nodes under /etc/map.publish and can't get the behaviour I would like.
Essentially, I would like the mapping rule to trigger only when the host header matches the request.
I am currently using sling:Mapping nodes under /etc/map.publish to map resource paths to short URLs in the response.
So under /etc/map.publish/http/myapp I would have the following node:
<jcr:root ...>
jcr:primaryType="sling:Mapping"
sling:internalRedirect="/content/company/app/en"
sling:match="app.company.com
</jcr:root>
What I would like is that when a user requests:
http://app.company.com/content/company/app/en/page.html
The urls in the response (when mapped) will return in the form:
http://app.company.com/page.html
The reason for this difference in inbound and outbound urls is because I have Apache rewriting URLs for different device types.
However, when a request with a different host header arrives, such as:
http://localhost:4502/content/company/app/en/page.html
I do not want the URLs to be mapped according to that rule. Right now, it is being mapped to
http://app.company.com/page.html
It seems as though the mapping is strictly resolves the resource using considering the host/port. Then when mapping urls during output a "best match" is found and used. I would like the map() to behave like the resolve() if possible.
There are two mechanisms based on /etc/map:
URL resolver using resolver.resolve() responsible for transforming URLs like http://app.company.com/page.html into content path, eg. /content/company/app/en/page.html
Link rewriter using resolver.map() method which transforms the content and shortens all links from /content/company/app/en/page.html form in <a>, <img>, etc. to full URL. It will work only if you don't have any regular expressions in apropriate sling:match property.
You can use domain name to map/resolve content and eg. create multidomain environment, so http://app.company.com/page.html will hit one resource and http://app.company2.com/page.html will hit another.
However, you can't disable or enable link rewriter depending on the current request host. Eg. if configure mappings as above, the /content/company/app/en/page.html content path will always be shortened to http://app.company.com/page.html, no matter what host header you have in your request.
If you want to make sure your inbound request is resolved, just add a second mapping to it.
Your mapping would look like this:
<jcr:root ...>
jcr:primaryType="sling:Mapping"
sling:internalRedirect="[/content/company/app/en,/content,/]"
sling:match="app.company.com
</jcr:root>
Outbound mappings, s.a. resolver.map(), will use the first applying rule.

Clear Magento cache based on key pattern

Is there a way in magento to clear specific parts of the cache whihc begin with a specific cache key.
I would like to clear everything in my module that have a key that begin with:
mynamespace_mymodule_
so the following will be clear from cache:
mynamespace_mymodule_asdaqasd
mynamespace_mymodule_qeqweq
mynamespace_mymodule_poipoi
etc
etc
Magento's caching system allows you to assign both cache key (unique id) and cache tags while saving your cache. Look at Mage_Core_Model_Cache::save method - 3d argument is cache tags array.
By assigning cache tags you'll have ability to remove all cache which is tagged with your cache tag at once. Mage_Core_Model_Cache::clean methid will take array of cache tags as an argument.

Caching options

As I see Smarty uses caching ‘by template’.
Can I somehow provide an URL to it, so it would cache pages by the URL given?
Can I get is_cached() to work with that given URL?
And compare last_mtime of the cached file with some of my data and then say «it’s time to update the cache»?
As default Smarty uses file based caching while storing php-like files in the $cache_dir.
You can implement a custom cache function and register it, but that depends on your desire how deep you want to dive into Smarty.
A way easier approach would be to just add a bit of the urls name to the template filename, so in your template directory for example might look like.
/your/templates/url1.index.tpl
/your/templates/url2.index.tpl
...and then use Smartys caching according to your needs.

Resources