Skip to content

nginx.conf配置文件详解

nginx.conf是Nginx的主配置文件,理解其结构和配置项对于正确配置Nginx至关重要。

配置文件结构

nginx.conf采用层次化的块结构,主要包含以下几个部分:

nginx
# 全局配置
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

# Events模块
events {
    worker_connections  1024;
}

# HTTP模块
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;

    sendfile        on;
    keepalive_timeout  65;

    # Server块
    server {
        listen       80;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

全局配置

user指令

指定Nginx运行用户:

nginx
user nginx;

说明:

  • 指定Worker进程运行用户
  • 通常设置为nginx或www-data
  • 需要确保用户对相关目录有访问权限

worker_processes指令

设置Worker进程数量:

nginx
worker_processes auto;

说明:

  • auto:自动设置为CPU核心数
  • 数字:指定具体进程数
  • 通常设置为CPU核心数或核心数的2倍

error_log指令

设置错误日志路径和级别:

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

日志级别:

  • debug:调试信息
  • info:一般信息
  • notice:通知信息
  • warn:警告信息
  • error:错误信息
  • crit:严重错误
  • alert:警报
  • emerg:紧急情况

pid指令

设置PID文件路径:

nginx
pid /var/run/nginx.pid;

说明:

  • 存储Master进程的PID
  • 用于进程管理和信号控制

Events模块

Events模块配置影响Nginx处理连接的方式。

worker_connections指令

设置每个Worker进程的最大连接数:

nginx
events {
    worker_connections 1024;
}

说明:

  • 单个Worker进程的最大并发连接数
  • 总连接数 = worker_processes × worker_connections
  • 根据服务器性能调整

use指令

指定事件处理模型:

nginx
events {
    use epoll;
}

可用模型:

  • epoll:Linux推荐
  • kqueue:BSD系统推荐
  • select:通用但性能较差
  • poll:通用但性能较差

multi_accept指令

设置是否同时接受所有连接:

nginx
events {
    multi_accept on;
}

说明:

  • on:尽可能多地接受连接
  • off:逐个接受连接(默认)

HTTP模块

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

include指令

包含其他配置文件:

nginx
include /etc/nginx/mime.types;

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;

说明:

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

client_max_body_size指令

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

nginx
client_max_body_size 20m;

说明:

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

client_body_buffer_size指令

设置请求体缓冲区大小:

nginx
client_body_buffer_size 128k;

日志配置

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;

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;

说明:

  • 小于该值的文件不压缩
  • 单位:字节

完整配置示例

nginx
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}

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;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
    keepalive_requests 100;

    client_max_body_size 20m;
    client_body_buffer_size 128k;

    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;

    include /etc/nginx/conf.d/*.conf;
}

配置验证

测试配置文件

bash
nginx -t

查看编译模块

bash
nginx -V

总结

nginx.conf配置文件的关键点:

  • 全局配置:设置运行用户、进程数、日志等
  • Events模块:配置连接处理方式
  • HTTP模块:配置HTTP服务相关参数
  • Server块:配置虚拟主机
  • Location块:配置URL匹配规则

理解nginx.conf的结构和配置项,是掌握Nginx配置的基础。