This was a question in a recent interview I took. I answered, that to my awareness there is no such way. But I am not sure. Is there a way to stop Apache web server from answering an HTTP request? I used PHP as the language of choice, if it makes a difference.
1 Answer
To better understand this answer you need to understand the networking "OSI 7 Layer Networking Model". (More details here: https://en.wikipedia.org/wiki/OSI_model )
What this demonstrates is that Apache will "handle" a request before PHP code even begins to execute. (Apache operates at layers 5,6, and 7, where PHP Typically Operates on top of apache).
Therefore PHP code is going to be ill equipped to "stop" apache from Answering an HTTP request. Instead, you will want to look at blocking access entirely (A Firewall [Either hardware of software based], operating at layer 3/4) or you will need apache to have logic to not handle the request.
This is actually more of a security question, than a programming question. Typically for this use case you would want an apache module to make a determination inside apache about handling a request. A few good modules for this are: mod_evasive, mod_security
- Mod_Evasive:
- https://www.digitalocean.com/community/tutorials/how-to-protect-against-dos-and-ddos-with-mod_evasive-for-apache-on-centos-7
- Mod_Security
- https://geekflare.com/apache-web-server-hardening-security/#6-Mod-Security
Please let me know if you have any questions, Thanks!