I have a website with just a single page with bootstrap, some javascript, text and images.
I was thinking of making my own webserver that would send everything over to the client then close the connection to save resources.
I'm wondering how would browsers behave. Would everything remain functional even with a closed connection, given it's all already been transferred?
Thank you!
CodePudding user response:
This isn't an optimization that would be helpful. Existing web servers typically close connections to clients within a few seconds of them not being used. Closing connections immediately is unlikely to make much difference. Having a few open but unused connections doesn't take many server resources or prevent other connections from working.
Building your own web server from scratch isn't easy. Your own server isn't going to perform as well as existing servers like Nginx or Apache. You are likely to create more security vulnerabilities than using an existing server. Your website is going to have fewer features available to it. A web server just to serve your simple site would probably need to have:
- Support for HTTP/1.0, HTTP/1.1, and HTTP/2.0
- Support for HTTPS with TLS encryption negotiation
- Support for transfer encoding (like gzip) for compression
- Memory caching of your website's files for performance
As your site grows, there are likely to be even more advanced features of web servers of which you would be able to take advantage.
Furthermore, running your own web server precludes hosting your site on shared hosting, which is the most cost efficient option for small new sites.
If you really want to close connections when you know they are complete, you are better off configuring existing servers to do that. This question's answers have several ways of telling Apache to close connections: Apache: Get rid of Keep-Alive entry in the headers list
Eventually you may want to add a content delivery network (CDN) to your site to push your content out to edge nodes close to your users around the globe. Since a CDN works as a reverse proxy server, users would no longer even be hitting your web server directly and wouldn't get your close optimization.