Nginx (phát âm là "engine X") là một máy chủ web mã nguồn mở, miễn phí, được phát triển bởi Igor Sysoev từ năm 2002 cho công ty Rambler của Nga. Phiên bản đầu tiên được công bố vào ngày 4 tháng 10 năm 2004 (phiên bản 0.1.0). Hiện nay, Nginx được sử dụng rộng rãi nhờ khả năng xử lý hàng chục nghìn kết nối đồng thời (C10K) với mức tiêu thụ tài nguyên cực thấp.
Tính năng nổi bật
- Kiến trúc module hóa, dễ mở rộng
- Độ tin cậy cao, hỗ trợ nâng cấp nóng mà không cần dừng dịch vụ
- Tiêu thụ bộ nhớ thấp: chỉ khoảng 2.5MB cho 10.000 kết nối keep-alive không hoạt động
- Hỗ trợ I/O bất đồng bộ (aio), mmap, sendfile và mô hình sự kiện (event-driven)
Chức năng chính
- Máy chủ web phục vụ nội dung tĩnh
- Reverse proxy cho HTTP, HTTPS, POP3, IMAP, SMTP
- Hỗ trợ tích hợp với FastCGI, uWSGI, SCGI
- Các module mở rộng như nén Gzip, SSL/TLS, streaming media
Kiến trúc Master/Worker
Nginx vận hành theo mô hình đa tiến trình:
- Master process: Quản lý cấu hình, khởi tạo và giám sát các worker
- Worker processes: Xử lý trực tiếp yêu cầu từ client
- Cache loader & cache manager: Quản lý hệ thống cache
Phân loại module
- Core modules: Xử lý cấu hình, log, quản lý tiến trình
- HTTP modules: Xử lý giao thức HTTP (ngx_http_*)
- Mail modules: Hỗ trợ proxy mail (ngx_mail_*)
- Stream modules: Proxy TCP/UDP (ngx_stream_*)
- Third-party modules: Mở rộng bởi cộng đồng (Lua, JSON...)
Cài đặt Nginx
Có thể cài đặt qua kho chính thức hoặc biên dịch từ mã nguồn:
yum install gcc pcre-devel openssl-devel zlib-devel
useradd -r -s /sbin/nologin nginx
./configure \
--prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-pcre
make && make install
Cấu hình tối ưu hiệu năng
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
worker_priority -5;
worker_rlimit_nofile 65536;
events {
worker_connections 2048;
use epoll;
accept_mutex on;
multi_accept on;
}
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
Cấu hình HTTP cơ bản
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
server_tokens off;
gzip on;
gzip_types text/plain text/css application/json;
include /etc/nginx/sites-enabled/*;
}
Virtual host và routing
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
location /static/ {
alias /data/static/;
expires 30d;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
}
error_page 404 /custom_404.html;
}
Bảo mật và kiểm soát truy cập
location /admin {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
allow 192.168.1.0/24;
deny all;
}
valid_referers none blocked server_names *.google.com;
if ($invalid_referer) {
return 403;
}
Ghi log dạng JSON
log_format json_log escape=json
'{'
'"timestamp":"$time_iso8601",'
'"client":"$remote_addr",'
'"method":"$request_method",'
'"uri":"$request_uri",'
'"status":$status,'
'"size":$body_bytes_sent,'
'"agent":"$http_user_agent"'
'}';
access_log /var/log/nginx/access.json json_log;
Rewrite URL linh hoạt
location /old-path {
rewrite ^/old-path/(.*)$ /new-path/$1 permanent;
}
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php last;
}
Cấu hình SSL/TLS
server {
listen 443 ssl http2;
server_name secure.example.com;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}