Nginx
Nginx是一个高性能的HTTP和反向代理服务器,同时支持IMAP/POP3邮件代理功能。相比Apache,Nginx使用更少资源,支持更高并发。
提示
Nginx高性能Web服务器与反向代理
功能特性
HTTP基础功能
- 处理静态文件、索引文件及自动索引
- 反向代理加速(无缓存)、负载均衡和容错
- FastCGI支持、SSL/TLS SNI支持
- 模块化结构,支持gzipping、byte ranges、chunked responses、SSI-filter
- IMAP/POP3代理服务,支持外部HTTP认证服务器重定向
支持的操作系统
- FreeBSD 3.x-6.x (i386)
- Linux 2.2/2.4/2.6 (i386/amd64)
- Solaris 8-10 (i386/sun4u)
- MacOS X (10.4) PPC
架构特性
- 主进程+多个工作进程模型
- 事件驱动:kqueue (FreeBSD)、epoll (Linux)、select/poll等
- sendfile支持,最小化数据拷贝
- 10,000非活动HTTP keep-alive连接仅需2.5M内存
安装
编译环境准备
Ubuntu:
apt-get install build-essential
apt-get install libtoolCentOS:
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++依赖库安装
# 安装PCRE(重写模块)
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
tar -zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure && make && make install
# 安装zlib(压缩模块)
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure && make && make install编译安装Nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.34 \
--with-zlib=/usr/local/src/zlib-1.2.8
make && make install启动Nginx
# 确保80端口未被占用
netstat -ano|grep 80
# 启动
sudo /usr/local/nginx/nginx运行和控制
命令行参数
| 参数 | 说明 |
|---|---|
| -c | 指定配置文件路径 |
| -t | 测试配置文件语法 |
| -v | 显示版本 |
| -V | 显示版本和编译参数 |
信号控制
主进程支持信号:
| 信号 | 说明 |
|---|---|
| TERM/INT | 快速关闭 |
| QUIT | 从容关闭 |
| HUP | 重载配置 |
| USR1 | 重新打开日志 |
| USR2 | 平滑升级 |
| WINCH | 从容关闭工作进程 |
常用命令
# 启动
sudo /usr/local/nginx/nginx
# 快速停止
kill -TERM $(cat /usr/local/nginx/nginx.pid)
# 从容停止
kill -QUIT $(cat /usr/local/nginx/nginx.pid)
# 重载配置
kill -HUP $(cat /usr/local/nginx/nginx.pid)
# 重新打开日志
kill -USR1 $(cat /usr/local/nginx/nginx.pid)
# 平滑升级
kill -USR2 $(cat /usr/local/nginx/nginx.pid)配置符号
时间单位
| 符号 | 说明 |
|---|---|
| ms | 毫秒 |
| s | 秒 |
| m | 分钟 |
| h | 小时 |
| d | 日 |
| w | 周 |
| M | 月(30天) |
| y | 年(365天) |
容量单位
| 符号 | 说明 |
|---|---|
| k/K | 千字节 |
| m/M | 兆字节 |
主模块配置
基本指令
# 进程用户
user www users;
# 工作进程数
worker_processes 5;
# CPU绑定
worker_cpu_affinity 0001 0010 0100 1000;
# 进程优先级
worker_priority -10;
# PID文件
pid /var/log/nginx.pid;
# 错误日志
error_log /var/log/nginx/errors.log;worker_processes计算
# 最大客户端数
max_clients = worker_processes * worker_connectionsevents模块
events {
# 连接处理方法
use epoll;
# 接受互斥锁
accept_mutex on;
# 多accept
multi_accept on;
# 单个工作进程最大连接数
worker_connections 1024;
}HTTP核心模块
客户端请求相关
# 请求体缓冲区大小
client_body_buffer_size 8k/16k;
# 请求体临时文件路径
client_body_temp_path /spool/nginx/client_temp 1 2;
# 请求超时时间
client_body_timeout 60;
client_header_timeout 60;
# 客户端header缓冲区
client_header_buffer_size 1k;
# 最大请求体大小
client_max_body_size 8m;静态文件处理
location /i/ {
# alias与root的区别:alias会替换路径
alias /spool/w3/images/;
}
location / {
root /var/www/html;
index index.html index.htm;
}日志配置
# 访问日志
access_log /var/log/nginx/access.log;
# 错误日志
error_log /var/log/nginx/error.log;其他核心指令
| 指令 | 说明 |
|---|---|
| keepalive_timeout | keep-alive超时 |
| sendfile | 启用sendfile |
| tcp_nopush | FreeBSD优化 |
| tcp_nodelay | 禁用Nagle算法 |
| gzip | 启用压缩 |
常用模块
HttpIndex模块
index index.html index.htm;HttpAccess模块
# 访问控制
allow 192.168.1.0/24;
deny 10.0.0.0/8;HttpAuthBasic模块
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;HttpGzip模块
gzip on;
gzip_types text/plain text/css application/json;
gzip_comp_level 6;HttpProxy模块
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}HttpRewrite模块
location / {
rewrite ^/images/(.*)$ /media/images/$1 break;
rewrite ^/blog/(.*)$ /news/$1 permanent;
}rewrite指令标志:
- last:重新搜索location
- break:停止rewrite
- redirect:302临时重定向
- permanent:301永久重定向
HttpSSL模块
server {
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
}HttpLimitReq模块
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
location / {
limit_req zone=one burst=5;
}HttpLog模块
log_format main '$remote_addr - $request_time';
access_log /var/log/nginx/access.log main;配置示例
完整示例
worker_processes 4;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name example.com;
root /var/www/html;
location / {
index index.html index.htm;
}
location /images/ {
alias /data/images/;
}
error_page 404 /404.html;
}
}虚拟主机
server {
listen 80;
server_name www.example.com;
root /var/www/example.com;
}
server {
listen 80;
server_name www.example.org;
root /var/www/example.org;
}负载均衡
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}反向代理
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}防盗链
location ~* \.(jpg|gif|png)$ {
valid_referers none blocked example.com;
if ($invalid_referer) {
return 403;
}
}优化建议
Hash表优化
Nginx使用hash表处理请求,针对server_names等设置:
server_names_hash_max_size 512;
server_names_hash_bucket_size 64;事件模型选择
| 平台 | 推荐事件模型 |
|---|---|
| Linux 2.6+ | epoll |
| FreeBSD | kqueue |
| Solaris | /dev/poll |
| 其他 | select/poll |
连接数计算
# 最大客户端数
max_clients = worker_processes * worker_connections
# 作为反向代理
max_clients = worker_processes * worker_connections / 4常见问题
为什么选择Nginx
- 高并发:支持高达50,000并发连接
- 低资源:相比Apache使用更少内存
- 高性能:基于epoll/kqueue事件驱动
- 配置简洁:配置文件简洁易读
- 稳定性:可7x24不间断运行
调试
使用debug_connection仅调试特定客户端:
events {
debug_connection 192.168.1.1;
}FAQ
- URL重写不工作:检查错误日志和配置语法
- 负载均衡算法:目前使用简单轮询
- 无法关闭代理缓存:暂无此功能
模块列表
| 模块 | 说明 |
|---|---|
| ngx_http_core_module | HTTP核心模块 |
| ngx_http_access_module | 访问控制 |
| ngx_http_auth_basic_module | 基本认证 |
| ngx_http_gzip_module | gzip压缩 |
| ngx_http_proxy_module | 反向代理 |
| ngx_http_rewrite_module | URL重写 |
| ngx_http_ssl_module | SSL/TLS |
| ngx_http_stub_status_module | 状态页 |
| ngx_mail_core_module | 邮件核心 |
| ngx_mail_auth_module | 邮件认证 |
| ngx_mail_proxy_module | 邮件代理 |
| ngx_mail_ssl_module | 邮件SSL |