# pattern user user [group]; # default user nobody nobody; # example user www-data;
设置 user www-data
后, Nginx 的主(master)进程仍然以 root 用户运行(只有 root 进程可以监听小于 1024 的端口),工作(worker)进程将以 www-data 用户运行。
# pattern worker_processes number | auto; worker_cpu_affinity cpumask ...; pid file; daemon on | off; # default worker_processes 1; pid nginx.pid; daemon on; # example worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; pid /var/run/nginx.pid; daemon on;
设置 Nginx 的工作进程数并将每个工作进程绑定到特定 CPU 核心。当 worker_processes
指令为 auto 时, Nginx 将自动启动与 CPU 核心数相同的工作进程。有时 Nginx 会将所有工作进程绑定到一个处理器核心,通过设置 worker_cpu_affinity
指令可以避免这种情况。通常情况,工作进程数与 CPU 核心数相等,只需将一个工作进程绑定一个 CPU 核心即可。设置 pid
指定一个文件存储主进程的ID ,通过 daemon off
可关闭 Nginx 后台守护进程,默认开启。
查看服务器的 CPU 核心数。
cat /proc/cpuinfo |grep "processor" | wc -l
通过 pid
文件停止或重启 Nginx 服务器。
# Graceful Shutdown kill -QUIT `cat /var/run/nginx.pid` # Configuration Reload kill -HUP `cat /var/run/nginx.pid`
events { # pattern use method; accept_mutex on | off; worker_connections number; # default accept_mutex on; worker_connections 512; # example use epoll; accept_mutex on; worker_connections 1024; }
Nginx 支持多种 连接处理方法 ,而哪些处理方法可用则取决于使用的平台。如果平台支持多种方法,那么 Nginx 一般会自动选择最高效的方法。如果需要,也可以使用 use
指令明确指定连接处理方法。默认 accept_mutex
开启,当请求数较大时应该关闭该配置项,具体原因可参见 闲扯 Nginx 的 accept_mutex 配置 这篇文章。
worker_connections
指令可设置单个工作进程开启的最大连接数。一个工作进程通常对应一个 CPU 核心,那么单个 CPU 性能越好,连接数越大。此外,服务器总连接数不能大于系统可同时打开文件的最大数目(可通过 worker_rlimit_nofile
指令修改),最大数目与服务器内存有关,内存越大,可同时打开文件越多。由此可见,该设置项与 CPU 性能和内存大小均有关系。
# pattern error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; # default error_log logs/error.log error; # example error_log /var/log/nginx/error.log warn; events { # pattern debug_connection address | CIDR | unix:; # example debug_connection 127.0.0.1; debug_connection localhost; debug_connection 192.0.2.0/24; debug_connection ::1; debug_connection 2001:0db8::/32; } http { # pattern log_format name string ...; access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; access_log off; # default log_format combined "..."; access_log logs/access.log combined; # example log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$gzip_ratio"'; access_log /var/log/nginx/access.log main gzip; }
通过 error_log
指令设置错误日志的输出和级别,输出可选文件、标准输出(stderr)和 syslog 三种,级别可选 [debug | info | notice | warn | error | alert | emerg ] 七种;通过 access_log
指令设置访问日志的输出、格式和条件。访问日志的输出可选文件和 syslog 两种,或者关闭。格式和条件均可自定义,通过 log_format
指令定义访问日志格式,支持很多变量,查看 Module ngx_http_log_module 了解详情。日志切割、监控及条件日志内容不在本文范围内,不做介绍。
debug_connection
指令设置一个 IP 或者 IP 段(支持 IPv4 和 IPv6 地址),来自这些地址的连接将会自动启用调试日志(debug logging),其他连接参考 error_log
指令的设置。在编译时添加 --with-debug
参数才能让 Nginx 支持调试日志(debug logging)特性,具体可参考 A debugging log 这篇文章。
http { # pattern gzip on | off; gzip_vary on | off; gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; gzip_http_version 1.0 | 1.1; gzip_comp_level level; gzip_min_length length; gzip_buffers number size; gzip_disable regex ...; gzip_types mime-type ...; # default gzip off; gzip_vary off; gzip_proxied off; gzip_http_version 1.1; gzip_comp_level 1; gzip_min_length 20; gzip_buffers 32 4k|16 8k; gzip_types text/html; # example gzip on; gzip_vary on; gzip_proxied any; gzip_http_version 1.1; gzip_comp_level 5; gzip_min_length 1024; gzip_buffers 16 8k; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_types text/plain text/html text/css text/javascript text/xml application/json application/x-javascript application/javascript application/xml application/xhtml+xml application/atom+xml application/xml+rss image/png image/jpeg image/gif image/x-icon image/bmp image/svg+xml audio/mpeg audio/ogg video/mp4 video/quicktime video/ogg video/webm; }
gzip on
开启服务器压缩功能; gzip_vary on
给响应头加上 Vary: Accept-Encoding
; gzip_proxied any
为所有的代理的响应头启用压缩; gzip_http_version 1.1
指定压缩支持的 HTTP 协议版本; gzip_comp_level 5
指定压缩级别[1-9],数字越小,压缩比越小,压缩速度越快; gzip_min_length
设置启用压缩的最小文件; gzip_buffers 16 8k
设置缓冲区的个数和大小; gzip_disable
对于特定浏览器禁用压缩功能;最后通过 gzip_types
设定启用压缩的文件类型。