Apache Tomcat Vs Nginx Comparison
Apache Tomcat and Nginx server, were created for two different tasks. NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server and Apache Tomcat is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. The Java Servlet, JavaServer Pages.
Table Of Contents
1. The tools
- Apache Tomcat 8
- Nginx server 1.10.2
2. Introduction
In this article We are going to compare Tomcat and Nginx servers and we are going to see where it is better to use one instead of the other.
3. Prerequisites
- Tomcat 8 installed and running
4. Nginx server
Nginx server is a web server, which used to serve static content like HTTP pages and serve dynamic content using FastCGI or as a reverse proxy.
Nginx server is a high-performance HTTP server and reverse proxy, ideal to use as a front end of another web servers. It is possible to use Nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with Nginx.
Nginx can be deployed to serve dynamic HTTP content on the network using FastCGI, SCGI handlers for scripts, WSGI application servers or Phusion Passenger modules, and it can serve as a software load balancer. Nginx uses an asynchronous event-driven approach to handling requests. Nginx modular event-driven architecture can provide more predictable performance under high loads.
4.1 Nginx request/response
Static content is meant to use a request/response scheme that is a message exchange pattern. The client makes a request to the server and the server sends a response.
In the case of the Nginx server it’s main purpose is to serve static HTML files that are text files formated with the HTML markup language. Other static content could be served as well, like XML, JSON, images, video files, sound files, etc. Also Nginx server could be used as a reverse proxy and a load balancer to serve as a frontend of other servers.
4.2 Download Nginx
Go to the URL http://nginx.org/en/download.html and download the Nginx server for Windows.
4.3 Install Nginx
Nginx comes in a zip compressed folder, all you need to do is uncompress the folder on your hard disk and you have a working Nginx on windows. Choose a folder and unzip Nginx.
4.4 Nginx serve a simple HTML file
Let’s create a simple HTML file to show the static content.
HTML static content
<DOCTYPE! HTML> <html> <head> <title>This is the title</title> </head> <body> <div> <p>This is static content</p> </div> </body> </html>
Nginx serve its static documents from the html
directory.
Save the HTML file as example.html
and copy the file in the html
directory.
4.5 Start Nginx
Open a terminal window and type start nginx
Now open the browser to see the static content. Go to the URI http://localhost/example.html and you see the following page.
The browser, in this case Firefox, makes a request to the Nginx server and then the server as a response sends the page we created before.
4.6 Stop Nginx
To stop the nginx server type nginx -s stop
5. Tomcat server
Tomcat server is a servlet container, which is designed to serve Java servlets. Tomcat is a server that is meant to run applications that were written in Java.
The servlets are Java classes that are used to serve dynamic web content. You can use Tomcat to serve static content as well. Recent versions of Tomcat have an improved performance to serve static content.
5.1 Example servlet
5.1.1 Create the project
We are going to create an example servlet to see how this works.
Open eclipse.
Click File->New Project.
Choose Dynamic Web Project
Click next and then write the name of the project
Press Finish.
5.1.2 Create the servlet
Right click on the project folder and press New->Servlet
Write the name of the servlet and press next.
Look at the URL Mapping automatically created by eclipse and then press next. This URL Mapping is the relative URI where you can find the servlet.
Choose doGet
, for this simple servlet we are only going to create the GET
request/response. As you can see a Servlet could have a much rich interface than a static web page to process the requests.
Inside the doGet
method, write the following code.
doGet Method
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try (PrintWriter writer = response.getWriter()) { writer.println(""); writer.println(""); writer.println(""); writer.println("Example Servlet"); writer.println(""); writer.println(""); writer.println("<h1>Sample Servlet</h1>"); writer.println(""); writer.println("Sample Servlet."); writer.println(""); writer.println(""); writer.println(""); } }
With this code we are doing the following.
response.setContentType("text/html");
Sets the response to be HTML.
response.setCharacterEncoding("UTF-8");
Sets the character encoding
PrintWriter writer = response.getWriter()
Gets the writer to write in the standard servlet output.
Then we write each line of html in the writer to create our response html page.
using writer.println
5.1.3 Run the servlet
Right click on the project and press Run as -> Run on Server
Choose Tomcat server and press finish.
Open the browser on the link http://localhost:8080/SampleServlet/Sampleservlet
And we get the following output:
5.2 Tomcat request/response
Tomcat is a servlet container, you can have many servlets in a Tomcat instance. All JSP pages are compiled into a servlet.
When you use Tomcat and the user request a resource in the server, the servlet container process the request, then it chooses what to do with the request. If the request has a valid URI, Tomcat gets the resource and sends the response to the client.
6. Complete Source code
example.html
<DOCTYPE! HTML> <html> <head> <title>This is the title</title> </head> <body> <div> <p>This is static content</p> </div> </body> </html>
SampleServlet.java
package com.myservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/SampleServlet") public class SampleServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SampleServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try (PrintWriter writer = response.getWriter()) { writer.println(""); writer.println(""); writer.println(""); writer.println("Example Servlet"); writer.println(""); writer.println(""); writer.println("<h1>Sample Servlet</h1>"); writer.println(""); writer.println("Sample Servlet."); writer.println(""); writer.println(""); writer.println(""); } } }
7. Conclusion
As you can see Nginx and Tomcat serve different purposes and you can combine them to make a good solution to serve web content, you can use tomcat to serve your dynamic content using servlets and use Nginx as a frontend reverse proxy and load balancer to serve a Tomcat cluster.
8. Download the Source Code
This was an example of: Apache Tomcat Vs Nginx Comparison.
It looks like there may be some truly valuable and interesting content in here, but the web page is so inundated with ads that it is impossible to ferret out the useful information. I understand the need for ad revenue, etc. but this is so extreme as to render your site unusable.