Skip to content

HTTP模块配置

HTTP模块是Nginx最核心的模块,包含HTTP服务的所有配置。

HTTP模块位置

HTTP模块位于Events模块之后:

nginx
events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

基本配置指令

include指令

包含其他配置文件。

nginx
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;

default_type指令

设置默认MIME类型。

nginx
default_type application/octet-stream;

sendfile指令

启用高效文件传输。

nginx
sendfile on;

说明:

  • 使用sendfile系统调用
  • 零拷贝传输,提高性能
  • 推荐开启

tcp_nopush指令

优化数据包发送。

nginx
tcp_nopush on;

说明:

  • 配合sendfile使用
  • 减少数据包数量
  • 提高传输效率

tcp_nodelay指令

禁用Nagle算法。

nginx
tcp_nodelay on;

说明:

  • 减少延迟
  • 适合实时性要求高的应用

连接配置

keepalive_timeout指令

设置长连接超时时间。

nginx
keepalive_timeout 65;

说明:

  • 单位:秒
  • 0表示禁用长连接
  • 根据应用特点调整

keepalive_requests指令

设置长连接最大请求数。

nginx
keepalive_requests 100;

说明:

  • 单个长连接的最大请求数
  • 超过后关闭连接

keepalive_disable指令

禁用特定浏览器的长连接。

nginx
keepalive_disable msie6;

客户端配置

client_max_body_size指令

设置客户端请求体最大大小。

nginx
client_max_body_size 20m;

说明:

  • 单位:k、m、g
  • 默认:1m
  • 上传文件时需要调整

client_body_buffer_size指令

设置请求体缓冲区大小。

nginx
client_body_buffer_size 128k;

client_header_buffer_size指令

设置请求头缓冲区大小。

nginx
client_header_buffer_size 1k;

large_client_header_buffers指令

设置大请求头缓冲区。

nginx
large_client_header_buffers 4 8k;

说明:

  • 第一个参数:缓冲区数量
  • 第二个参数:每个缓冲区大小

client_body_timeout指令

设置请求体读取超时时间。

nginx
client_body_timeout 60;

client_header_timeout指令

设置请求头读取超时时间。

nginx
client_header_timeout 60;

日志配置

log_format指令

自定义日志格式。

nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

常用变量:

  • $remote_addr:客户端IP
  • $remote_user:认证用户名
  • $time_local:访问时间
  • $request:请求行
  • $status:状态码
  • $body_bytes_sent:发送字节数
  • $http_referer:来源页面
  • $http_user_agent:用户代理
  • $http_x_forwarded_for:真实IP

access_log指令

设置访问日志。

nginx
access_log /var/log/nginx/access.log main;

关闭访问日志:

nginx
access_log off;

error_log指令

设置错误日志。

nginx
error_log /var/log/nginx/error.log warn;

Gzip配置

gzip指令

启用Gzip压缩。

nginx
gzip on;

gzip_types指令

设置压缩的MIME类型。

nginx
gzip_types text/plain text/css application/json application/javascript;

gzip_comp_level指令

设置压缩级别。

nginx
gzip_comp_level 6;

说明:

  • 范围:1-9
  • 数字越大压缩率越高,但CPU消耗越大
  • 推荐值:4-6

gzip_min_length指令

设置最小压缩文件大小。

nginx
gzip_min_length 1000;

gzip_vary指令

添加Vary响应头。

nginx
gzip_vary on;

gzip_disable指令

禁用特定浏览器的压缩。

nginx
gzip_disable "msie6";

性能配置

sendfile指令

启用高效文件传输。

nginx
sendfile on;

sendfile_max_chunk指令

设置sendfile最大块大小。

nginx
sendfile_max_chunk 512k;

aio指令

启用异步I/O。

nginx
aio on;

directio指令

启用直接I/O。

nginx
directio 4m;

缓存配置

open_file_cache指令

启用文件缓存。

nginx
open_file_cache max=1000 inactive=20s;

说明:

  • max:最大缓存数
  • inactive:非活动时间

open_file_cache_valid指令

设置缓存验证时间。

nginx
open_file_cache_valid 30s;

open_file_cache_min_uses指令

设置最小使用次数。

nginx
open_file_cache_min_uses 2;

open_file_cache_errors指令

缓存错误信息。

nginx
open_file_cache_errors on;

限制配置

limit_rate指令

限制响应速率。

nginx
limit_rate 100k;

limit_req_zone指令

定义请求限制区域。

nginx
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

limit_req指令

应用请求限制。

nginx
limit_req zone=one burst=5 nodelay;

完整配置示例

nginx
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 日志配置
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log warn;

    # 性能配置
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    sendfile_max_chunk 512k;

    # 连接配置
    keepalive_timeout  65;
    keepalive_requests 100;

    # 客户端配置
    client_max_body_size 20m;
    client_body_buffer_size 128k;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    client_body_timeout 60;
    client_header_timeout 60;

    # Gzip配置
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;
    gzip_disable "msie6";

    # 文件缓存
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    # 包含其他配置
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

性能优化建议

高并发场景

nginx
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 1000;
    client_max_body_size 10m;
    gzip on;
    gzip_comp_level 4;
    open_file_cache max=2000 inactive=30s;
}

静态资源服务器

nginx
http {
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 300;
    open_file_cache max=10000 inactive=60s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;
}

API服务器

nginx
http {
    sendfile on;
    tcp_nodelay on;
    keepalive_timeout 60;
    keepalive_requests 100;
    client_max_body_size 50m;
    gzip on;
    gzip_types application/json application/javascript;
}

总结

HTTP模块配置的关键点:

  • 性能优化:sendfile、tcp_nopush、tcp_nodelay
  • 连接管理:keepalive_timeout、keepalive_requests
  • 客户端限制:client_max_body_size、client_body_buffer_size
  • 日志配置:log_format、access_log、error_log
  • Gzip压缩:gzip、gzip_types、gzip_comp_level
  • 文件缓存:open_file_cache系列指令

合理配置HTTP模块,可以显著提升Nginx的性能和稳定性。