SoFunction
Updated on 2025-04-04

The relationship between Nginx and Tomcat and description

NginxandTomcatThey are all commonly used components in modern web application architectures. They are often used in high-performance web services, load balancing, and reverse proxy scenarios. Although their capabilities overlap, they each have different core responsibilities and can work closely together to optimize the performance and scalability of the system.

The role of

Nginx (Engine-X) is a high-performanceWeb ServerandReverse proxy server, often used to handle HTTP requests, load balancing, and static resource services.

  • Reverse proxy:Nginx can be used asReverse proxyThe server, receives client requests and forwards them to the backend application (such as Tomcat). This makes Nginx an intermediary between the client and the application server, enhancing the security, scalability and stability of the system.
  • Load balancing:Nginx provides built-in load balancing capabilities that can distribute requests to multiple Tomcat servers. Commonly used load balancing algorithms include polling, IP hashing, minimum connection, etc. Through load balancing, multiple Tomcat instances can be shared to jointly bear the load, improving the system's throughput and fault tolerance.
  • Static resource services:Nginx handles static files very efficiently (such as pictures, CSS, JS files). It can respond quickly to static resource requests and relieve stress on Tomcat servers. Requests for static resources can be processed directly by Nginx without going through Tomcat, thereby improving performance.
  • SSL terminal:Nginx can handle SSL encryption and decryption work, reducing the encryption burden of Tomcat, making Tomcat focus on handling business logic.
  • Request cache:Nginx supports request caching function, which can cache some content and reduce the load on the backend Tomcat.
  • Reverse proxy + Web layer security: Nginx can hide the real address of Tomcat as a reverse proxy to improve security. And it can effectively limit certain HTTP requests and prevent malicious attacks (such as DDoS attacks).

Nginx configuration load balancing

http {
    upstream tomcat_backend {
        server 127.0.0.1:8080;  # Tomcat Server 1        server 127.0.0.1:8081;  # Tomcat Server 2        server 127.0.0.1:8082;  # Tomcat Server 3    }

    server {
        listen 80;
        server_name ;

        location / {
            proxy_pass http://tomcat_backend;  # Use load balancing            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
        }
    }
}
  • upstream tomcat_backend: Define a name calledtomcat_backendbackend server pool containing multiple Tomcat instances (different ports or machines).
  • proxy_pass http://tomcat_backend;:Nginx forwards the request to the defined load balancing pool and automatically selects a Tomcat instance to handle the request.

Nginx connection pool

Configuring Nginx to reduce the connection overhead with Tomcat can improve performance.

http {
    upstream tomcat_backend {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
        keepalive 32;  # Configure persistent connections    }
}

Nginx reverse proxy cache

For some dynamic content, Nginx can also be cached, reducing Tomcat's processing pressure.

location /api/ {
    proxy_cache my_cache;
    proxy_cache_valid 200 1h;  #Cache for 1 hour    proxy_pass http://tomcat_backend;
}

The role of

Tomcat is aJava Servlet ContainerandWeb Server, mainly responsible for handling dynamic requests, especially Java Web applications (such as applications based on Servlet and JSP technologies).

Tomcat's main responsibilities are:

  • Process dynamic requests: Tomcat is responsible for parsing, processing and responding to client dynamic requests. These requests are usually generated by Java Web applications, such as Java Servlets or JSP pages. Tomcat runs Java applications, executes the backend's business logic, and generates dynamic web content.
  • Supports Java EE standards: Tomcat is an implementation of the Java EE specification (Servlet and JSP) that can execute web applications based on Java technology (such as applications written by frameworks such as Spring MVC and Struts).
  • Servlet container: Tomcat implements the functions of Servlet containers, supporting Servlet life cycle management, request distribution, etc. It is the core server component of Java Web applications.
  • JSP Support: Tomcat also supports JSP (Java Server Pages), allowing Java Web developers to dynamically generate web pages through the combination of Java code and HTML code.
  • Session Management: Tomcat provides HTTP session management functions to handle the creation, management and destruction of user sessions.

Tomcat acts as a container for Java Web applications, handling dynamic requests (such as Servlets or JSPs). Here is a basic example of Tomcat configuration, assuming we are using Tomcat as the backend server to handle dynamic Java requests.

Usually, Tomcat's configuration file is, you can configure the port and other settings for Tomcat listening. By default, Tomcat listens to port 8080, but can be adjusted as needed.

<Server port="8005" shutdown="SHUTDOWN">
    <Service name="Catalina">
        <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000"
                   redirectPort="8443" />
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
                <!-- Configure the application -->
            </Host>
        </Engine>
    </Service>
</Server>
  • port="8080": Configure Tomcat to listen on port 8080 by default.
  • connectionTimeout="20000": Set the connection timeout to 20 seconds.
  • redirectPort="8443": If HTTPS is enabled, HTTP requests are redirected to port 8443.

The role of Tomcat

In common web application architectures, Nginx and Tomcat are collaborative components of the front-end and back-end, and together form the two cores of the web system.

Their partnerships are generally as follows:

  • Client requests to Nginx: When the client (browser) sends a request, it first passes through Nginx. Nginx is responsible for parsing requests and doing load balancing, SSL terminal processing, static resource processing, etc.
  • Nginx reverse proxy request to Tomcat: For dynamic requests (such as accessing Java web applications), Nginx forwards the request to Tomcat. Nginx acts as a reverse proxy server, and Tomcat handles requests as a backend application server and generates response data.
  • Tomcat returns the response to Nginx: After Tomcat has processed the request, it returns the response result (such as HTML, JSON, or other formats) to Nginx.
  • Nginx returns the response to the client:Nginx finally forwards the response returned by Tomcat to the client to complete the entire request-response process.

This architecture enables the system to have high availability, scalability and stability in high concurrency and high traffic conditions.

As a front-end reverse proxy and load balancer, Nginx can effectively divert requests and accelerate the loading of static content, while Tomcat focuses on handling complex Java dynamic requests and business logic.

4. Common front-end and back-end architectures

(1)Single Tomcat backend: Suitable for small systems or development stages.

Nginx receives the request as the front-end and forwards it to Tomcat, which directly processes and returns the response.

[Client] --> [Nginx] --> [Tomcat]

(2)Multiple Tomcat backends (load balancing): Suitable for high concurrency and high availability systems.

Nginx serves as a load balancer to distribute requests to multiple Tomcats, and traffic can be allocated using load balancing algorithms.

[Client] --> [Nginx (Load balancing)] --> [Tomcat 1]
                               --> [Tomcat 2]
                               --> [Tomcat 3]

(3)Separation of static resources and dynamic requests: Nginx handles static resource requests directly, while dynamic requests (such as JSP or Servlets) are handled by Tomcat.

By configuring Nginx, static files (such as pictures, CSS, JavaScript) can be directly provided by Nginx, while dynamic content (such as user login information, data query, etc.) is forwarded to Tomcat through Nginx.

[Client] --> [Nginx (Static files)] --> [Tomcat (Dynamic Request)]

Summarize

  • Nginx: Suitable for high concurrency and high performance scenarios, it is responsible for handling HTTP requests, reverse proxy, load balancing, SSL encryption and decryption, static resource services, etc.
  • Tomcat: Focus on running Java Web applications and handling dynamic requests (such as Servlets, JSPs, etc.), it is a container and server for Java Web applications.

Use Nginx and Tomcat togethercan:

  • Improve system throughput and concurrent processing capabilities.
  • Improve system stability and availability through load balancing and reverse proxy.
  • Reduce the pressure on database and backend services and optimize response time.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.