-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HEAD request returns 404 #21299
Comments
Hi @kolucciy. Thank you for your report.
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
For more details, please, review the Magento Contributor Assistant documentation. @kolucciy do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?
|
Hi @engcom-backlog-nazar. Thank you for working on this issue.
|
✅ Confirmed by @engcom-backlog-nazar Issue Available: @engcom-backlog-nazar, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself. |
- CMS index action is not handling head requests
This is an issue with the Magento built-in full page cache. Add this plugin in a di.xml somewhere
And create the following class in the path defined by the di.xml file
Alternatively you can disable the fullpage cache, but that is not advisable since it affects performance. Better solution is to put the store behind a Varnish cache, since that seems to be unaffected by this. |
…404 responses to HEAD requests.
HEAD requests should be supported automatically (on the framework level) for all GET requests, just w/o body. Caching should take this into account (GET should have separate cache to avoid empty body). Looks like also supported by @Vinai |
Yes, HEAD requests should be cached, and fixing only the index action is bandaid. |
@buskamuza I'm wondering if there really needs to be a separate cache for HEAD requests? Since the same controller is returning the result for both HEAD and GET requests (and I think it's infeasible to make the controller return different results based on if the request is HEAD or GET), I don't think there's a need for a separate cache. The framework should just send the HEAD part of the result back, instead of the whole result (which it might already do anyway?). @Vinai IMO there's no need for a plugin. I think we could simply map the HEAD requests to the GET action interface in app/etc/di.xml (the arguments for Magento\Framework\App\Request\HttpMethodMap) and completely remove the Magento\Framework\App\Action\HttpHeadActionInterface interface. Unless there's some specific reason to have the HEAD interface existing separately, I feel that removing that interface completely and defining the GET interface to be the action for both is a cleaner solution. The framework would then handle the two different request types as I outlined above. Thoughts on this suggestion? |
@mattijv The change you describe introduces a pretty big backward incompatible change. Also it doesn’t address the the issue of the response body, which should be empty for propper HTTP HEAD request handling. |
@Vinai I'm not sure what the backwards incompatible change would be? I assume you mean removing the HttpHeadActionInterface? It is true that if some third party developer has implemented that Interface in their code, removing it would break their code. And I guess if they have chosen to implement a route that only responds to HEAD requests, that would be broken by the method mapping change. The first issue could be avoided by leaving the interface in the codebase. The latter is something that would be hard to reconcile with my proposed changes, but is likely a rare usecase and an improper way to handle HTTP requests anyway... Is there some other backwards incompatibility the change would cause that I'm missing? Since the action interfaces were introduced in M2.3, I feel like the real world impact of changing the mappings at this stage would be small. I would rather have the codebase be stripped from unnecessary parts, if possible. I see now what you intented the plugin to do. I was under the assumption that the zend framework would handle stripping the body from the response, but that was a mistake on my part. Apologies. Yes, a plugin like that is needed to handle the HEAD responses correctly. |
Yes, removing an interface is a backward incompatible change. |
@Vinai @buskamuza After some digging around in the parts of Magento internals that I wasn't too familiar with, it seems there are two different ways the HEAD requests could be handled.
Option 1 seems to be easier to implement and would not introduce internal complexity to the core framework, but possibly has some overhead tradeoffs. To me option 1 feels like it better follows the philosophy of GET and HEAD requests, but I might be mistaken. Option 2 would be a more complex change, but would potentially result in faster responses to HEAD requests (unless there is a possibility of rendering a result affecting the headers of said result, in which case the rendering is anyway necessary?). Anyway, this decision is above my paygrade, so to speak. And I might very well have overlooked some issues. |
@Vinai In M2.3 I can find any class that implements it currently and it isn't present in earlier releases, which is why I think removing it at this stage shouldn't be a major problem. But then again I don't have to support thousands of 3rd party developers... IMO implementing it as a separate interface feels like a mistake and this would be the best time to fix the issue as M2.3 has been out for a fairly short time and the use case for that interface seems rare anyway. |
The response content has to be generated for HEAD requests so the Content-Length HTTP header can be correctly set, which is an important use case for HEAD requests. |
@Vinai Of course, silly of me to overlook that. In that case I would suggest changes similar to the option 1 I outlined above. If you feel that removing the interface is too major of a change, I would still propose changing the interface mappings so that both HEAD and GET map to the same interface, and leaving the HttpHeadActionInterface for backwards compatibility (even if it is unused). |
On further inscpection the I also noticed that at the moment all static resources also return the full body for HEAD requests. Perhaps the Magento\Framework\App\StaticResouce could emit a similar event for the same purpose? |
According to the Magento Technical Guidelines an event (including its arguments) must not be modified, a plugin must be used instead (see paragraph 14 at https://devdocs.magento.com/guides/v2.3/coding-standards/technical-guidelines.html) I’m not saying I agree with that, but any solution in the core should probably take that into account. |
@robfico Yes, this affects pretty much all cached controllers. Before this is fixed in Magento, you can either use some other full page cache, like Varnish, or use a temporary fix like #21299 (comment) to prevent the built-in cache from caching 404 HEAD requests. |
I don’t think it should take a lot to fix the built in page cache to distinguish the GET and HEAD requests and cache them separately. |
Have a production mode 2.3 site with Nginx that sometimes serves homepage as a 404 think this might be why. Suspect head requests sent by a "down notifier" service and then the 404 is cached and served up by full page cache. Confirm 404 when doing Head request, flushing FPC cleans it out.
Someone made a plugin module to work around this in the meantime, same as mattijv's earlier post. |
… interface and add HEAD request handling
… interface and add HEAD request handling
… interface and add HEAD request handling
…action interface and add HEAD request handling #21378 - Merge Pull Request #21378 from mattijv/magento2:2.3-develop - Merged commits: 1. 46191d8 2. d8eb160 3. 72630cd 4. 4f7db3a 5. e198914 6. 22b9e56 7. 69bd2dd 8. ce2e4d3 9. 56d4cc2 10. 2b58789 11. 5b79a30 12. 6bac208 13. 880404c 14. bc3ef7a 15. be77120 16. fecce53 17. 3d19c09 18. 2a337f9 19. b5683e8 20. 740e4bd
…action interface and add HEAD request handling #21378
Any idea when 2.3.2 will be released? Could do with a fix for this being available. Thanks |
@SteveEdson While waiting for the next release you can do a couple interim fixes:
and/or
|
@mattijv thanks for your work on that. Option 2 does not work for me on Magento 2.3.1. I tried to apply your changes from the pull request via
May I re-use this issue to test this on a new 2.3-develop instance or should I create a new issue? |
@magento give me 2.3-develop instance |
Hi @sprankhub. Thank you for your request. I'm working on Magento 2.3-develop instance for you |
Hi @sprankhub, here is your Magento instance. |
Indeed, this works on 2.3-develop. So either option 1 or 2 is correct. Then I will need to wait for 2.3.2... |
@sprankhub In practice only the Make sure that your change persists and that you apply the change before running the deployment commands. If the problem persists despite that, maybe you are running into some other (related?) issue? |
Got it. I only patched |
I am experiencing exactly the same with the 404 error that shows randomly after a while. I did the same as sprankhub, but am also experiencing 429 errors on my robots and sitemap. Could this also be related to this issue? Hope this is resolved soon. |
I changed the di.xml, but it is still occuring. Can you arrange a fix for me? |
@HugoGerritsen are you talking about 404 errors or 429 errors (probably a different issue)? Did you patch both |
@sprankhub I’m talking about the 404 errors. I updates both di.xml files and the error still comes up. |
Note that it is possible to fix this in affected versions using a custom module (or even just a line added to I didn't assemble it into a full-blown Git repo, but these are the files for such a module: |
Preconditions (*)
Steps to reproduce (*)
curl -X HEAD -i https://demo1-m2.mage.direct/
orcurl -I https://demo1-m2.mage.direct/
Expected result (*)
Actual result (*)
The text was updated successfully, but these errors were encountered: