Nginx has revolutionized the way websites and web applications are served, offering impressive performance, scalability, and a lightweight footprint. Tengine builds upon Nginx’s strengths, adding innovative features and optimizations.
Tengine: A Brief Overview
Tengine is an open-source web server developed by Alibaba Group. It builds upon the Nginx core, enhancing it with additional features and optimizations. Here’s why Tengine stands out:
- SO_REUSEPORT: Tengine supports the
SO_REUSEPORT
option, which allows multiple worker processes to listen on the same port simultaneously. This feature significantly improves connection handling and load balancing1. - Performance: Tengine demonstrates impressive performance gains compared to vanilla Nginx. Let’s look at a benchmark comparison:
- Tengine with SO_REUSEPORT enabled: Achieved a 200% performance improvement over Nginx with accept lock (the default setting).
- Tengine vs. Nginx without accept lock: Tengine outperformed Nginx by 60%1.
Benchmark Results
In a simple benchmark test, Tengine exhibited remarkable performance enhancements. Here are the details:
- Test Setup:
- Hardware: Intel Xeon E5-2650v2 (32 cores), 128GB RAM, Intel 10-Gigabit NIC
- Software: Linux kernel 3.17.2, Tengine 2.1.0, Nginx 1.6.2, ApacheBench 2.3
- Test Cases: Varying concurrency (100 to 1000)
- Results:
- Tengine (SO_REUSEPORT): Best performance
- Nginx (accept lock): Default setting
- Nginx (without accept lock): Lower performance
Various benchmarks show Nginx and Tengine consistently outperforming Apache in terms of:
- Requests per second: Nginx and Tengine handle significantly more requests than Apache under similar conditions.
- Latency: The time it takes to serve a request is lower with Nginx and Tengine.
- Memory usage: Nginx and Tengine consume less memory, especially under heavy load.
Why Choose Nginx or Tengine Over Apache?
Event-Driven Architecture: Nginx and Tengine excel under heavy traffic loads because of their event-driven architecture. They handle multiple connections efficiently without spawning a new process or thread for each request, resulting in minimal resource consumption.
- HTTP/3 Support (QUIC v1 and draft-29): Tengine embraces the latest in web protocols, enabling faster and more secure communication between clients and servers.
High Concurrency: Nginx and Tengine are designed to handle thousands of simultaneous connections effortlessly. This makes them ideal for high-traffic websites, APIs, and applications where Apache’s process-based model might struggle.
- Dynamic Reconfiguration Based on Headers and Weights: Customize routing dynamically based on standard and custom HTTP headers, header values, and weights. This fine-grained control enhances load balancing and optimization
Reverse Proxy and Load Balancing: Nginx and Tengine are exceptional reverse proxies. They can distribute traffic across multiple backend servers, improving performance, fault tolerance, and scalability.
Lightweight and Fast: Compared to Apache, Nginx and Tengine have a smaller memory footprint and faster response times. They’re optimized for delivering static content and are well-suited for resource-constrained environments.
Flexibility and Extensibility: Nginx and Tengine’s modular architecture allows you to customize and extend their functionality easily. A vast ecosystem of modules exists for tasks like caching, security, and application-specific optimizations.
- More Load Balancing Methods: Tengine offers consistent hashing, session persistence, and dynamic resolution of upstream domain names on the fly2
- Lua Scripting Support: Extend Tengine’s core functionalities efficiently using Lua scripts. It’s a powerful way to enhance your server’s behavior
- High-Speed UDP Transmission with Kernel Bypass: By bypassing the kernel for UDP transmission, Tengine achieves impressive performance gains, especially in scenarios with high concurrency.
Tengine: Taking Nginx Further
Additional features:
- Dynamic Module Loading: Add or remove modules on the fly without restarting the server.
- Enhanced Load Balancing Algorithms: More sophisticated algorithms for distributing traffic.
- Web Application Firewall (WAF) Module: Integrated security to protect against common web attacks.
Installation and Configuration on Different Linux Distributions
a) CentOS/RHEL:
1. Add the Tengine repository:
```bash
sudo yum install epel-release
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
```
2. Install Tengine:
sudo yum install tengine
3. Start and enable Tengine:
sudo systemctl start tengine
sudo systemctl enable tengine
b) Ubuntu/Debian:
1. Add the Tengine repository:
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:openresty/ppa
sudo apt-get update
2. Install Tengine:
sudo apt-get install tengine
3. Start and enable Tengine:
sudo systemctl start tengine
sudo systemctl enable tengine
c) Basic Configuration:
The main configuration file for Tengine is typically located at `/etc/tengine/tengine.conf`. Here’s a basic configuration example:
nginx
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
}
This configuration sets up a basic web server listening on port 80 and serving files from `/var/www/html`.
Detailed comparison table of Nginx, Tengine, and Apache web servers:
Feature | Nginx | Tengine | Apache |
---|---|---|---|
Architecture | Event-driven | Event-driven | Process-based |
Concurrency Handling | Excellent | Excellent | Good |
Performance | Very high | Very high | Moderate |
Memory Footprint | Low | Low | Moderate |
Configuration | Flexible | Flexible | Flexible |
Scalability | High | High | Moderate |
Load Balancing | Built-in | Built-in & Enhanced | Requires modules |
Reverse Proxy | Built-in | Built-in | Requires modules |
Static Content Serving | Excellent | Excellent | Good |
Dynamic Content Serving | Requires modules/FastCGI | Requires modules/FastCGI | Built-in |
Modules | Extensive | Extensive | Extensive |
Community | Large | Growing | Very large |
Security | Good | Good with WAF Module | Good |
Learning Curve | Moderate | Moderate | Moderate |
Use Cases | High-traffic, API, reverse proxy | High-traffic, API, reverse proxy | General purpose, legacy systems |
Key Points:
- Architecture: Nginx and Tengine’s event-driven architecture allows them to excel in handling a large number of concurrent connections with minimal resource usage. Apache’s process-based model can be less efficient under heavy load.
- Performance & Scalability: Nginx and Tengine generally outperform Apache in terms of raw performance and scalability, particularly under high traffic conditions.
- Modules: All three servers have a vast ecosystem of modules, providing flexibility and extensibility.
- Dynamic Content: Apache has built-in support for processing dynamic content (PHP, Perl, etc.). Nginx and Tengine typically handle dynamic content through FastCGI or by proxying requests to other servers (like Apache or application servers).
- Tengine Advantage: Tengine offers enhanced load balancing algorithms and a Web Application Firewall (WAF) module, which can be significant advantages in certain scenarios.
- Apache Advantage: Apache’s extensive documentation and larger community can be helpful for beginners and for situations where specific modules or configurations are needed.
Choosing the Right Server:
The best choice depends on your specific needs:
- High-traffic Websites/APIs: Nginx or Tengine are ideal due to their superior performance and scalability.
- Legacy Systems: Apache might be preferred if you have existing applications that rely heavily on Apache-specific modules or configurations.
- Specific Features: Tengine’s WAF and enhanced load balancing could be decisive factors for some projects.
Tips for Switching from Apache
- Evaluate Your Needs: If your primary concern is performance and scalability, Nginx or Tengine are excellent choices.
- .htaccess Migration: Nginx doesn’t use
.htaccess
files. You’ll need to convert any Apache rewrite rules or configurations to Nginx format. - Module Compatibility: Make sure any essential Apache modules have equivalents for Nginx or Tengine.
- Gradual Transition: Consider running both Apache and Nginx in parallel initially to ensure a smooth transition.
Conclusion
Nginx Tengine offers a powerful, flexible, and high-performance alternative to both standard Nginx and Apache. Its enhanced features, excellent performance, and ease of configuration make it an attractive option for websites and applications of all sizes. While the switch from Apache may require some adjustments, many users find the improved performance and resource efficiency well worth the effort.