Seems I might have found what causes the issue but I cannot be really sure, so I will report again later. Basically, I could reproduce it pretty consistently:
- Create a Flarum tag that has the "view forum" permission set to members (and not to everyone)
- The above means that users that are not logged will not be able to see discussions with that tag and will receive the “The page you requested could not be found” error which is in fact 404 in the error console. That is expected.
- Now if you log in with a user and try to open the same discussion URL, you might sometimes see the same problem. No matter how much you refresh it, it would give you that error for some time. After that it will fix itself. Or, if you modify slightly the URL by e.g. adding / at the end, or change the post number in the URL, the problem will disappear
That made me think it's some caching problem. I use CloudFlare but couldn't find anything that can be reconfigured there and I don't have any custom caching edge rules, etc.
Then I moved on to mine SiteGround hosting and I found that there are three types of caching there:
- Nginx Direct Delivery. It was turned on. It's a bit weird, since my Flarum is hosted by the Apache, so it seems SiteGround use an Nginx facade that they have configured to do some caching before the Apache, or at least that's my understanding. I disabled it.
- Dynamic cache - this is really fishy and I don't understand how exactly it works but they claim it caches dynamic pages, e.g. PHP, so that they cache the response and then the next user requesting the same PHP would get the cached response. Either I get it wrong, or it's a wrong idea in the first place but I really don't see how that may work at all. You must not cache a dynamic content generated through PHP... I don't know, maybe they also check whether the request writes in the DB and then assume that if the DB is unchanged, then they can cache the PHP result, or who knows what?! Anyway, it cannot be disabled. So, I searched if I can disable completely the caching and found out that they have a buried info in their FAQ that in order to disable caching, you have to put the following in your
.htaccess
file:
<IfModule mod_headers.c>
Header set Cache-Control "private"
</IfModule>
However the above would make any Flarum response to be marked as private cache-control, which in turn will prevent CDN-s such as CloudFlare to cache static resource, it has to be applied only to non-assets.
Besides, with the standard installation of Flarum there's already a mod_header.c
directive that fixes some proxy stuff, so to combine both and not apply private cache control on assets, the section becomes:
# Fix for https://httpoxy.org vulnerability
# Disable SiteGround cache for non-asset resource
<IfModule mod_headers.c>
RequestHeader unset Proxy
<If "%{REQUEST_URI} !~ /assets/">
Header set Cache-Control "private"
</If>
</IfModule>
- Memcached which is disabled by default and I'm not sure if it can be used by Flarum.
I think by disabling 1 and 2 I may have solved the problem. I don't see the original issue anymore, at least in the last two hours.
BTW, it would be great if Flarum would not show 404 when anonymous users open these discussions. A 401/403 is the proper error code with a corresponding error page that would redirect to the Login page.