1. 性能相关配置
1 2 | worker_processes number | auto; worker进程的数量;通常应该为当前主机的cpu的物理核心数 |
1 2 3 4 | worker_cpu_affinity auto [cpumask] #将work进程绑定在固定cpu上提高缓存命中率 例: worker_cpu_affinity 0001 0010 0100 1000; worker_cpu_affinity 0101 1010; |
1 2 | worker_priority number 指定worker进程的 nice 值,设定worker进程优先级: [-20,20] |
1 2 | worker_rlimit_nofile number worker进程所能够打开的文件数量上限,默认较小,生产中需要调大如65535 |
2. 时间驱动events相关的配置
1 2 3 | worker_connections number 每个worker进程所能够打开的最大并发连接数数量,如10240 总最大并发数: worker_processes * worker_connections |
1 2 3 | use method 指明并发连接请求的处理方法,默认自动选择最优方法不用调整 如:use epoll; |
1 2 3 4 | accept_mutex on | off 互斥; 处理新的连接请求的方法; on指由各个worker轮流处理新请求 , Off指每个新请求的到达都会通知(唤醒)所有的worker进程,但 只有一个进程可获得连接,造成“惊群”,影响性能,默认on |
3. http核心模块相关配置ngx_http_core_module
3.1web服务模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | server { ... } 配置一个虚拟主机 server { listen address[:PORT]|PORT; server_name SERVER_NAME; root /PATH/TO/DOCUMENT_ROOT ; } 注意: (1) 基于port; listen PORT; 指令监听在不同的端口 (2) 基于ip的虚拟主机 listen IP:PORT; IP 地址不同 (3) 基于 hostname server_name fqdn; 指令指向不同的主机名 |
3.2套接字相关配置
1 2 3 4 5 6 7 | listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size] default_server 设定为默认虚拟主机 ssl 限制仅能够通过ssl连接提供服务 backlog=number 超过并发连接数后,新请求进入后援队列的长度 rcvbuf=size 接收缓冲区大小 sndbuf=size 发送缓冲区大小 |
3.3 server_name
1 2 3 4 5 6 7 8 9 10 11 | server_name name ...; 支持*通配任意长度的任意字符 server_name *.magedu.com www.magedu.* 支持~起始的字符做正则表达式模式匹配,性能原因慎用 server_name ~^www\d+\.magedu\.com$ #\d 表示 [0-9] 匹配优先级机制从高到低: (1) 首先是字符串精确匹配 如: www.magedu.com (2) 左侧*通配符 如: *.magedu.com (3) 右侧*通配符 如: www.magedu.* (4) 正则表达式 如: ~^.*\.magedu\.com$ (5) default_server |
3.4 延迟发送选项
1 2 3 4 5 | tcp_nodelay on | off; 在keepalived模式下的连接是否启用TCP_NODELAY选项 当为off时,延迟发送,合并多个请求后再发送 默认On时,不延迟发送 可用于: http, server, location |
3.5 sendfile
1 2 3 | sendfile on | off; 是否启用sendfile功能,在内核中封装报文直接发送 默认Off |
3.6 隐藏版本信息
1 2 | server_tokens on | off | build | string 是否在响应报文的Server首部显示nginx版本 |
3.7 location匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } 在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射; ngnix会根据 用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置 示例: server {... server_name www.magedu.com; location /images/ { root /data/imgs/ ; } } http: //www .magedu.com /images/logo .jpg --> /data/imgs/images/logo .jpg =:对URI做精确匹配; ^~:对URI的最左边部分做匹配检查,不区分字符大小写 ~:对URI做正则表达式模式匹配,区分字符大小写 ~*:对URI做正则表达式模式匹配,不区分字符大小写 不带符号:匹配起始于此uri的所有的uri 匹配优先级从高到低: =, ^~, ~/~*, 不带符号 |
3.7 路径别名alias path
1 2 3 4 5 6 7 8 9 10 11 | 示例: http: //www .magedu.com /bbs/index .php location /bbs/ { alias /web/forum/ ; } --> /web/forum/index .html location /bbs/ { root /web/forum/ ; } --> /web/forum/bbs/index .html 注意: location中使用root指令和 alias 指令的意义不同 (a) root,相当于追加在root目录后面 (b) alias ,相当于对location中的url进行替换 |
3.8 错误页面显示
1 2 3 4 5 6 | error_page code ... [=[response]] uri; 模块: ngx_http_core_module 定义错误页, 以指定的响应状态码进行响应 可用位置: http, server, location, if in location error_page 404 /404 .html error_page 404 =200 /404 .html #防止404页面被劫持 |
3.9 长连接相关配置
1 2 3 4 5 6 7 8 9 | keepalive_timeout timeout [header_timeout]; 设定保持连接超时时长, 0表示禁止长连接, 默认为75s keepalive_requests number; 在一次长连接上所允许请求的资源的最大数量,默认为100 keepalive_disable none | browser ... 对哪种浏览器禁用长连接 send_timeout time ; 向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非 整个响应过程的传输时长 |
3.10 请求报文缓存
1 2 3 4 5 6 7 8 9 10 | client_body_buffer_size size; 用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时, 其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置 client_body_temp_path path [level1 [level2 [level3]]]; 设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量 目录名为16进制的数字; client_body_temp_path /var/tmp/client_body 1 2 2 1 1级目录占1位16进制,即2^4=16个目录 0-f 2 2级目录占2位16进制,即2^8=256个目录 00-ff 2 3级目录占2位16进制, 即2^8=256个目录 00-ff |
3.11 对客户端进行限制相关配置
1 2 3 4 5 6 7 8 9 10 11 12 | limit_rate rate; 限制响应给客户端的传输速率,单位是bytes /second 默认值0表示无限制 limit_except method ... { ... },仅用于location 限制客户端使用除了指定的请求方法之外的其它方法 method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH 例: limit_except GET { allow 192.168.1.0 /24 ; deny all; } 除了GET和HEAD 之外其它方法仅允许192.168.1.0 /24 网段主机使用 |
4. 访问控制模块ngx_http_access_module
实现基于ip的访问控制功能
1 2 3 4 5 6 7 8 9 10 11 12 | allow address | CIDR | unix: | all; deny address | CIDR | unix: | all; http, server, location, limit_except 自上而下检查,一旦匹配,将生效,条件严格的置前 示例: location / { deny 192.168.1.1; allow 192.168.1.0 /24 ; allow 10.1.1.0 /16 ; allow 2001:0db8:: /32 ; deny all; } |
5. 用户认证模块ngx_http_auth_basic_module
实现基于用户的访问控制,使用basic机制进行用户认证
1 2 3 4 5 6 7 8 9 | auth_basic string | off; auth_basic_user_file file ; location /admin/ { auth_basic "Admin Area" ; auth_basic_user_file /etc/nginx/ .ngxpasswd; } 用户口令: 1、明文文本:格式name:password:comment 2、加密文本:由htpasswd命令实现 httpd-tools所提供 |
6. 状态查看模块ngx_http_stub_status_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 用于输出nginx的基本状态信息 Active connections:当前状态,活动状态的连接数 accepts:统计总值,已经接受的客户端请求的总数 handled:统计总值,已经处理完成的客户端请求的总数 requests:统计总值,客户端发来的总的请求数 Reading:当前状态,正在读取客户端请求报文首部的连接的连接数 Writing:当前状态,正在向客户端发送响应报文过程中的连接数 Waiting:当前状态,正在等待客户端发出请求的空闲连接数 示例: location /status { stub_status; allow 172.16.0.0 /16 ; deny all; } |
7. 日志记录模块ngx_http_log_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1、 log_format name string ...; string可以使用nginx核心模块及其它模块内嵌的变量 2、 access_log path [ format [buffer=size] [ gzip [=level]] [flush= time ] [ if =condition]]; access_log off; 访问日志文件路径,格式及相关的缓冲的配置 buffer=size flush= time 示例 log_format compression '$remote_addr-$remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"' ; access_log /spool/logs/nginx-access .log compression buffer=32k; 3、 open_log_file_cache max=N [inactive= time ] [min_uses=N] [valid= time ]; open_log_file_cache off; 缓存各日志文件相关的元数据信息 max:缓存的最大文件描述符数量 min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项 inactive:非活动时长 valid:验正缓存中各缓存项是否为活动项的时间间隔 |
8. 压缩相关选项ngx_http_gzip_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1、 gzip on | off; #启用或禁用gzip压缩 2、gzip_comp_level level; #压缩比由低到高: 1 到 9 默认: 1 3、gzip_disable regex ...; #匹配到客户端浏览器不执行压缩 4、gzip_min_length length; #启用压缩功能的响应报文大小阈值 5、gzip_http_version 1.0 | 1.1; #设定启用压缩功能时,协议的最小版本 默认: 1.1 6、gzip_buffers number size; 支持实现压缩功能时缓冲区数量及每个缓存区的大小 默认: 32 4k 或 16 8k 7、gzip_types mime- type ...; 指明仅对哪些类型的资源执行压缩操作;即压缩过滤器 默认包含有text /html ,不用显示指定,否则出错 8、gzip_vary on | off; 如果启用压缩,是否在响应报文首部插入“Vary: AcceptEncoding 9、 gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; nginx对于代理服务器请求的响应报文,在何种条件下启 用压缩功能 off:对被代理的请求不启用压缩 expired,no-cache, no-store, private:对代理服务器 请求的响应报文首部Cache-Control值任何一个,启用压缩功能 示例: gzip on; gzip_comp_level 6; gzip_min_length 64; gzip_proxied any; gzip_types text /xml text /css application /javascript ; |
9. https模块ngx_http_ssl_module模块:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1、 ssl on | off; 为指定虚拟机启用HTTPS protocol, 建议用listen指令代替 2、 ssl_certificate file ; 当前虚拟主机使用PEM格式的证书文件 3、 ssl_certificate_key file ; 当前虚拟主机上与其证书匹配的私钥文件 4、 ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; 支持ssl协议版本,默认为后三个 5、 ssl_session_cache off | none | [ builtin [:size]] [shared:name:size]; builtin [:size]:使用OpenSSL内建缓存,为每worker进程私有 [shared:name:size]:在各worker之间使用一个共享的缓存 6、 ssl_session_timeout time ; 客户端连接可以复用ssl session cache中缓存的ssl参数的有 效时长,默认5m 示例: server { listen 443 ssl; server_name www.magedu.com; root /vhosts/ssl/htdocs ; ssl on; ssl_certificate /etc/nginx/ssl/nginx .crt; ssl_certificate_key /etc/nginx/ssl/nginx .key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; } |
联系客服
微信登录中...
请勿关闭此页面