配置文件

文件位置 Linux:/etc/nginx/nginx.conf

macOS:/usr/local/etc/nginx/nginx.conf

windows:<安装目录>/conf/nginx.conf

文件层级: 全局块、events块、http块(全局块+server块(server全局块+location块))

全局块

# 运行 Nginx 的用户和用户组
user nginx nginx;
user <user> <group>;
只写一个时,Nginx 会把用户组设为该用户的主组
 
# 工作进程数(通常设置为 CPU 核心数)
worker_processes auto;
 
# 错误日志路径和级别
error_log /var/log/nginx/error.log warn;
记录 warn、error、crit、alert、emerg;debug/info 级别不会记) # 进程 PID 文件位置 pid
/var/run/nginx.pid; # 最大打开文件数 worker_rlimit_nofile 65535; 这个值还受系统 ulimit -n、systemd 的 LimitNOFILE 等限制影响

events块

Events 块主要影响 Nginx 服务器与用户的网络连接。

events {
    # 单个工作进程的最大连接数
    worker_connections 10240;
    
    # 使用 epoll 事件驱动模型(Linux 推荐)
    use epoll;
    在现代 Linux 上通常即使不写,Nginx 也会自动选 epoll
    # 尽可能多地接受连接
    multi_accept on;
    让 worker 在一次事件循环里 尽可能多地 accept 新连接,而不是一次只收一个。
}

http块

基础配置

http {
    # 引入 MIME 类型定义
    include mime.types;
    (扩展名 → Content-Type),比如 .html → text/html
    default_type application/octet-stream;
    如果某个文件扩展名在 mime.types 里找不到,就用这个默认类型(通常表示“二进制流”)
    # 字符集
    charset utf-8;
    设置响应的默认字符集为 UTF-8
    # 日志格式定义
    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;
    配合 sendfile,尽量把响应头和数据包凑大再发
    tcp_nodelay on;
    对 keepalive 连接减少 Nagle 算法带来的延迟
    # 连接超时时间
    keepalive_timeout 65;
    
    # 客户端请求体大小限制
    client_max_body_size 20m;
    
    # Gzip 压缩
    gzip on;
    gzip_vary on;
    添加 Vary: Accept-Encoding 头,便于 CDN/缓存正确区分是否压缩版本。
    gzip_comp_level 6;
    压缩等级 1~9,越大压得越小但更耗 CPU。6 属于常见折中。
    gzip_types text/plain text/css text/xml text/javascript 
               application/json application/javascript application/xml+rss;
    指定哪些 MIME 类型进行 gzip 压缩(文本、css、js、json、xml 等)。
}    

性能优化配置

http {
    # 隐藏 Nginx 版本号(安全)
    server_tokens off;
    默认情况下,响应头里可能会有 Server: nginx/1.xx.x,错误页也可能显示版本。
    设为 off 后只显示 Server: nginx(或更少信息),减少被针对版本漏洞扫描的风险。
    # 文件缓存
    open_file_cache max=10000 inactive=20s;
    最多缓存 10000 个文件条目
    某个缓存条目 20 秒内没有被访问,就会被移出缓存
    open_file_cache_valid 30s;
    每隔 30 秒检查缓存是否仍然有效(文件是否被修改/删除等)
    open_file_cache_min_uses 2;
    一个文件至少被访问 2 次才会进入缓存
    
    # 客户端缓冲区大小
    client_body_buffer_size 128k;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 16k;
    
    # FastCGI 缓存(用于 PHP 等)
    fastcgi_cache_path /var/cache/nginx levels=1:2 
                       keys_zone=my_cache:10m max_size=1g 
                       inactive=60m use_temp_path=off;
    /var/cache/nginx:缓存文件存储目录
    levels=1:2:目录分层规则(避免一个目录下文件太多)
    keys_zone=my_cache:10m:定义缓存区名字 my_cache,并用 10MB 内存存 key/元数据
    (10MB 大概能存几万条缓存 key,具体取决于 key 长度)
    max_size=1g:磁盘缓存最大 1GB
    inactive=60m:60 分钟没被访问的缓存会被清理
    use_temp_path=off:尽量直接写入缓存目录而不是临时目录(减少一次移动/IO
}

Server 块

Server 块定义了一个虚拟主机,一个 HTTP 块可以包含多个 Server 块。

server {
    # 监听端口
    listen 80;
    listen [::]:80;  # IPv6
    
    # 服务器域名
    server_name example.com www.example.com;
    
    # 网站根目录
    root /var/www/example.com;
    例如访问 http://example.com/a/b.html,Nginx 会去找:
    /var/www/example.com/a/b.html
    
    # 默认首页
    index index.html index.htm index.php;
    访问目录时默认找哪些首页文件。找到第一个存在的就返回。
    
    # 访问日志(可覆盖 http 块设置)
    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;
}
server {
    listen 443 ssl http2;
    server_name example.com;
    监听 443 端口,启用 SSL/TLS(HTTPS),并开启 HTTP/2
    # SSL 证书
    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    
    # SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    只允许 TLS 1.21.3
    ssl_ciphers HIGH:!aNULL:!MD5;
    指定可用的加密套件(这里排除匿名和 MD5)
    ssl_prefer_server_ciphers on;
    优先使用服务器选择的套件
    # SSL 会话缓存
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    缓存 TLS 会话,缓存区名 SSL,大小 10MB,超时 10 分钟
    # HSTS(强制 HTTPS)
    add_header Strict-Transport-Security "max-age=31536000" always;
    未来 31536000 秒(1 年)内,这个域名只用 HTTPS 访问
}
 
# HTTP 自动跳转 HTTPS
server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

Location 块配置详解

 

posted @ 2026-01-05 16:09  蘑菇味的花魂  阅读(11)  评论(0)    收藏  举报