In daily Web development, the topic of Server is always inseparable: Nginx, Tomcat, Apache Http Server, Liunx and so on. So, what are the types of servers? What do they do?
Servers are generally divided into two parts:
-
Hardware: A computer (usually Linux, aliyun we rent, etc.) that runs at least one server software and can handle some specialized requests.
-
Software: A computer program that receives, processes requests and then returns a response. Several Web servers commonly used in daily development :Apache Http Server, IIS, Nginx.
What does the server do
A Web server is a repository of Web resources. When a user accesses it through a browser, the server reads the stored resources and sends the data to the visitor. With Web server, developers only need to worry about how Web resources are written, and do not need to care about how resources are sent to the client, thus greatly reducing the workload of developers.
Why Tomcat was introduced
Web servers are usually only capable of handling requests for static resources, which is far from sufficient in normal development. So servlets were introduced to extend it. Servlets are server-side Java programs inside a Web server that capture and process requests sent by the browser. Web servers invoke servlets to handle requests for dynamic resources, such as access to a database.
But the Servlet itself cannot be used directly; it must be instantiated by the container and its methods called to produce a response. The response is then returned by the Servlet container to the Web server, which wraps the response and returns it to the browser (client) as HTTP.
Tomcat is an excellent Servlet container implementation.
Deployment of servers in the project
If the request is a static Web page, the Web Server processes it and returns the result. If the request is dynamic, the Web Server forwards the resolution to Tomcat, which then returns the result through the Web Server. In this way, division of labor can be achieved, load balancing can be achieved, and system performance can be improved.
The most commonly used Web server is Nginx.
It is worth noting that Tomcat itself can also handle static resources, so it plays both roles in some small Web applications. But because this efficiency is not high enough, in slightly larger projects will be static separation.