การปรับจูนประสิทธิภาพ Nginx
ปรับกระบวนการทำงานของ Nginx ขีดจำกัดการเชื่อมต่อ และขนาดบัฟเฟอร์ เพื่อให้รองรับปริมาณงานสูงสุดและมีเวลาแฝงต่ำ
การปรับจูนประสิทธิภาพ Nginx เป็นบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Boost Nginx Performance
Why tune Nginx? To handle more traffic, reduce latency, and ensure your web applications are fast and responsive. It's about getting the most out of your server resources.
Understanding Worker Processes
Nginx uses a master-worker architecture. The master process handles reading configuration and managing worker processes. Worker processes do the actual work of handling client requests.
Configure Worker Processes
The worker_processes directive sets the number of worker processes. A common recommendation is to set it to auto (Nginx >= 1.9.1) or to the number of CPU cores.
worker_processes auto; # Or '4' for 4 CPU cores
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
}
}
}Max Worker Connections
Each worker process can handle a limited number of simultaneous connections. The worker_connections directive sets this limit per worker process. This includes both client and proxy connections.
Adjusting Worker Connections
A higher worker_connections value allows a single worker to handle more clients, which is good for high-traffic sites. However, setting it too high can consume excessive memory. Use a value appropriate for your server's resources.
worker_processes auto;
events {
# Each worker can handle up to 4096 connections
worker_connections 4096;
use epoll; # For Linux systems, highly efficient
}
http {
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
}
}
}Nginx Buffers Explained
Nginx uses buffers to temporarily store data from clients or backend servers. Properly configuring buffer sizes prevents slow clients from tying up resources and improves overall performance.
Client Request Buffers
These buffers manage incoming data from clients:
client_body_buffer_size: Buffer for client request bodies (e.g., file uploads).client_header_buffer_size: Buffer for client request headers.
Small requests fit in memory. Larger ones can be written to disk if configured.
http {
client_body_buffer_size 16k; # Default is 8k/16k depending on OS
client_header_buffer_size 1k; # Default is 1k
large_client_header_buffers 4 8k; # 4 buffers of 8k each for larger headers
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
}
}
}Proxy Buffers for Backends
When Nginx acts as a reverse proxy, it buffers responses from backend servers to improve delivery speed and reduce backend load:
proxy_buffer_size: Size of the first buffer for the response header.proxy_buffers: Number and size of buffers for the response body.
http {
proxy_buffer_size 128k; # First buffer for header
proxy_buffers 4 256k; # 4 buffers, each 256k, for response body
proxy_busy_buffers_size 256k; # Max size of buffers that can be busy
server {
listen 80;
server_name example.com;
location /api/ {
proxy_pass http://backend_app;
}
}
}Persistent Connections (Keepalive)
Keepalive connections allow a client to send multiple requests over a single TCP connection. This reduces connection overhead and improves perceived latency.
keepalive_timeout: How long an idle keepalive connection stays open.keepalive_requests: Max requests a client can make over a keepalive connection.
http {
keepalive_timeout 65; # Default is 75s; set to optimize for client behavior
keepalive_requests 1000; # Default is 100; higher for persistent clients
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
}
}
}Tuning Directives
Consider the core Nginx tuning directives we've discussed. Which of the following statements about them is FALSE?
Nginx Tuning Summary
We've covered key Nginx performance tuning areas:
- Worker Processes: Optimize CPU utilization.
- Worker Connections: Manage simultaneous client connections.
- Buffer Sizes: Efficiently handle client and proxy data.
- Keepalive: Reduce connection overhead.
These settings are crucial for achieving high throughput and low latency, ensuring a smooth user experience.
คำถามที่พบบ่อย
บทเรียน “การปรับจูนประสิทธิภาพ Nginx” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับจูนประสิทธิภาพ Nginx” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับจูนประสิทธิภาพ Nginx”
ปรับกระบวนการทำงานของ Nginx ขีดจำกัดการเชื่อมต่อ และขนาดบัฟเฟอร์ เพื่อให้รองรับปริมาณงานสูงสุดและมีเวลาแฝงต่ำ คุณปฏิบัติ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับจูนประสิทธิภาพ Nginx” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) นี้ได้ไหม
ได้ บทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บันทึกการทำงานและตัวชี้วัดของ Nginx
- การปรับจูนประสิทธิภาพ Nginx
- Nginx ในสภาพแวดล้อมแบบคอนเทนเนอร์
- การโหลดใหม่โดยไม่หยุดทำงานและการทดสอบการตั้งค่า