user root; # 按 CPU 核数设置,超过 8 个收益很小;需要时可直接改成具体数字,如 4 worker_processes auto; # 每个 worker 打开的文件描述符上限,为高并发场景预留 worker_rlimit_nofile 65535; # 错误日志级别 warn,避免大量冗余 info 日志;容器内输出到 stderr 方便收集 error_log stderr warn; events { # 高并发场景用 epoll;worker_connections 与 worker_processes 相乘即最大并发连接数 use epoll; worker_connections 10240; multi_accept on; } http { include mime.types; default_type application/octet-stream; log_format main '"$http_x_forwarded_for" $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"'; # 容器环境下日志输出到 stdout/stderr,交给 docker 日志驱动统一处理 access_log /dev/stdout main; # 只为低版本浏览器不支持的格式做 gzip,现代浏览器直接用 gzip_static 提供的 .gz 文件 gzip on; gzip_vary on; gzip_comp_level 5; gzip_min_length 1k; gzip_proxied any; gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; # 客户端请求头/请求体超时,防止慢连接长期占用 worker client_header_timeout 15s; client_body_timeout 15s; send_timeout 15s; # 调优: 32的倍数 server_names_hash_max_size 1024; server_names_hash_bucket_size 128; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; client_max_body_size 1200m; # 大文件上传时先把请求体落到磁盘,避免占用内存 client_body_buffer_size 128k; # 文件缓存,减少磁盘 stat 次数;大小可视容器内打开文件数调整 open_file_cache max=10000 inactive=30s; open_file_cache_valid 60s; open_file_cache_min_uses 2; open_file_cache_errors on; include /usr/local/nginx/conf/conf.d/*.conf; }