这篇文章主要讲解了“Nginx的基本属性配置”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Nginx的基本属性配置”吧!
1. Nginx服务的基本配置
1.1 用于调试进程和定位问题的配置项
是否以守护进程的方式运行nginx
# 默认on
daemon on|off;
是否以master/worker方式工作
# 默认on,指定了是否以master-worker进程的方式运行,如果设置为off,那么所有的请求将只会由master进程处理
master_process on|off;
error日志的设置
# 指定了error日志的目录和日志级别,第二个参数用于指定目录,第三个参数用于指定日志级别,总共有:debug、info、notice、warn、error、crit、alert、emerg,这些日志级别中,从左往右优先级依次增大,默认为info
error_log logs/error.log error;
是否处理几个特殊的调试点
# 指定了调试点
debug_points stop|abort
仅对指定的客户端输出debug级别的日志
debug_connection IP|CIDR
该参数主要用于events模块中,针对指定的ip或者网段记录debug日志:
events {
debug_connection 10.224.66.14;
debug_connection 10.224.57.0/24;
}
需要注意的是,在使用该参数时,必须要确保在进行configure时已经加入了--with-debug
参数,否则不会生效;
限制coredump核心转储文件的大小
worker_rlimit_core size;
在Linux操作系统中,如果一个进程由于错误或者收到信号而终止时,会将进程执行时的内存内容写入一个文件(core文件),以作为调试之用,这就是所谓的核心转储。在nginx进程宕机时,其就会产生核心转储文件,而且该文件一般都有几个G,因而如果不限制该文件的大小,那么很有可能会把服务器磁盘占满。该参数的作用就是限制核心转储文件的大小的。
指定coredump文件生成目录
working_directory path;
该参数指定了在生成核心转储文件时,将该文件存放的目录。
1.2 正常运行的配置项
定义环境变量
env TESTPATH=/tmp/
这个配置项可以让用户直接设置操作系统上的环境变量。
嵌入其他配置文件
include /path/file
用于将其他的配置文件引入进来,该路径可以是绝对路径,也可以是相对路径,如果是相对路径,则是基于nginx的配置目录而指定的。
pid文件的路径
pid path/file
用于指定存储nginx的master进程运行所使用的进程id的文件的路径。
Nginx的worker进程运行的用户及用户组
user username [groupName]
用于指定worker进程运行时所基于的用户和用户组,默认都为nobody,这里如果不指定groupName,那么组名就与用户名一致。
指定nginx的worker进程可以打开的最大句柄描述符个数
worker_rlimit_nofile limit;
设置一个worker进程能够打开的最大句柄描述符个数。
限制信号队列
worker_rlimit_sigpending limit;
设置了每个用户能够发往nginx的信号队列的大小,如果信号队列已满,那么新发送的信号将会被丢弃。
1.3 优化性能的配置项
nginx的worker进程的个数
worker_processes 1;
用于指定nginx运行时worker进程的个数,在nginx运行时,每个worker进程都是单线程运行的,这里需要判断worker进程是否进行了阻塞性操作,如果有这样的操作,那么稍微多配置一些worker进程比较好,如果没有,那么将worker进程数量设置得与CPU数量一样能够得到更好的性能。
绑定nginx的worker进程到指定的CPU内核
worker_cpu_affinity cpumask [cpumask...]
将worker进程与指定的CPU进行绑定,这样能够防止多个worker进程抢占同一个CPU,从而避免出现同步问题。如下是一个4核CPU的配置方式:
worker_processes 4;
worker_cpu_affinity 1000 0100 0010 0001;
需要注意的是,worker_cpu_affinity
仅对于Linux系统有效。
SSL硬件加速
ssl_engine device;
如果服务器上有SSL硬件加速设备,那么就可以进行配置以加快SSL协议的处理速度。用户可以使用OpenSSL提供的命令来查看是否有SSL硬件加速设备:
openssl engine -t
系统调用gettimeofday的执行频率
timer_resolution -t
默认情况下,每次内核的事件调用返回时,都会执行一次gettimeofday,在早期的Linux版本中,获取系统时间都会有一次从内核态到用户态的数据复制,其代价比较高,但是在最新的x86-64体系架构中,gettimeofday仅仅只是一次vsyscall,其仅仅只是对共享内存页中的数据的访问,代价不大。
nginx的worker进程优先级
worker_priority nice;
用于设置nginx的worker进程的优先级,其中,nice的默认值为0。在Linux操作系统中,当有多个进程在竞争CPU执行资源时,其就会根据每个进程设置的优先级来优先分配执行权限,并且所分配的时间片也要高一些。优先级的值在-20~+19
之间,数值越低优先级越高,一般建议将nginx的优先级设置得更低一些,这样才能保证其执行的权限,但是建议不要设置得比内核的进程优先级(其值为-5)还要低。
1.4 事件类配置项
是否打开accept锁
accept_mutex [on|off]
accept_mutex
参数用于控制是否启用负载均衡锁,其默认值为on,该锁会保证各个worker轮流的、序列化的与新客户端建立连接,并且当某个worker的连接数达到了worker_connections
配置的最大连接数的7/8时,该锁会降低该worker将要新建立的连接数,从而保证各个worker的负载均衡。
lock文件的路径
lock_file path/file;
该参数指定了lock文件的路径。nginx会使用操作系统提供的锁功能,但如果操作系统不支持原子锁,此时才会使用文件锁来实现lock。如果accept_mutex
参数设置为off,那么该参数将不会生效。
使用accept锁后到真正建立连接之间的延迟时间
accept_mutex_delay Nms;
如果一个worker进程尝试获取锁失败了,那么其就会等待该参数指定的时间段之后再次尝试获取锁,该值默认为500ms。
批量建立新连接
multi_accept [on|off];
当事件模型通知此次有新的连接建立请求时,尽可能的对本次调度中客户端发起的的所有TCP请求都建立连接,该值默认为off。
选择事件模型
use [kqueue|rtsig|epoll|/dev/poll|select|poll|eventport];
nginx所选用的事件模型,其会自动使用最适合的模型。在Linux操作系统下支持poll、select和epoll三种,其中epoll的性能是最高的。
每个worker的最大连接数
worker_connections number;
指定了每个worker进程能够建立的最大连接数。
2. http核心模块配置
2.1 监听端口
listen address:port[default(deprecated)|default_server|[backlog=num|rcvbuf=size|sndbuf=size|accept_filter=filter|deferred|bind|ipv6only=[on|off]|ssl]]
2.2 主机名称
语法:server_name name [...];
默认:server_name "";
配置块:server
2.3 server_names_hash_bucket_size
语法:server_names_hash_bucket_size size;
默认:server_names_hash_bucket_size 32|64|128;
配置块:http、server、location
2.4 server_names_hash_max_size
语法:server_names_hash_max_size size;
默认:server_names_hash_max_size 512;
配置块:http、server、location
2.5 重定向主机名称的处理
语法:server_name_in_redirect on|off;
默认:server_name_in_redirect on;
配置块:http、server或者location
2.6 location
语法:location [=|~|~*|^~|@] /uri/{...}
配置块:server
3. 文件路径的定义
3.1 以root方式设置资源路径
语法:root path;
默认:root html;
配置块:http、server、locationo、if
3.2 以alias方式设置资源文件
语法:alias path;
配置块:location;
与root
一样,alias也是配置资源文件路径的,但是alias是location后的路径以别名的方式替换目标路径的指定部分,比如如下配置:
location /conf {
alias /usr/local/nginx/conf;
}
此时如果一个请求为/conf/index.html,那么其前缀/conf将会与当前location匹配,并且会将alias参数替换请求uri中匹配的部分,也就是转换后的uri为/usr/local/nginx/conf/index.html。
3.3 访问首页
语法:index file...;
默认:index index.html;
配置块:http、server、location
该配置块的主要作用是将用户访问的某个地址映射到首页,在进行首页查找时,会按照顺序查询index参数后的文件,如果存在,则将其返回,如果不存在,则继续查找下一个。比如如下示例:
location / {
root path;
index /index.html /html/index.php /index.php
}
当接收到用户的/
请求后,其首先会查询/path/index.html
文件是否存在,如果不存在,则查询下一个/path/html/index.php
是否存在,如果存在,则直接返回,依此类推。
3.4 根据http返回码重定向页面
语法:error_page code[code...][=|=answer-code]uri|@named_location
配置块:http、server、location、if
该配置的主要作用是,如果当前请求返回了指定的状态码,那么就将其重定向到后面的错误页面。如:
error_page 404 /404.html
error_page 502 503 504 /50x.html
error_page 403 http://example.com/forbidden.html
error_page 404 = @fetch;
需要注意的是,即使重定向了URI,返回的HTTP状态码还是原来的状态码,如果需要修改状态码,可以使用=
来修改原来的状态码,如:
error_page 404 =200 /empty.gif;
error_page 404 =403 /forbidden.gif;
也可以不指定修改后的状态码,而是由重定向后的请求决定其返回的状态码:
error_page 404 = /empty.gif;
在重定向后,也可以不修改URI,而是将这个请求重定向到另一个location中进行处理,比如:
location / {
error_page 404 @fallback;
}
location @fallback {
proxy_pass http://backend;
}
3.5 是否支持递归的使用error_page
语法:recursive_error_pages [on|off];
默认:recursive_error_pages off;
配置块:http、server、location;
3.6 try_files
语法:try_files path2[path3]uri;
配置块:server、location
该参数的主要作用是在用户请求到达之后,会依次尝试其后指定的各个path路径,如果匹配上了,那么就将该路径的值直接返回。如果都没有匹配上,那么就会使用最后的uri作为默认处理路径。示例如:
try_files /system/maintenance.html $uri $uri/index.html $uri.html @other
location @other {
proxy_pass http://backend;
}
4. 内存及磁盘资源的分配
4.1 http包体只存储到磁盘文件中
语法:client_body_in_file_only on|clean|off;
默认:client_body_in_file_only off;
配置块:http、server、location
4.2 http包体尽量写入到一个内存buffer中
语法:client_body_in_single_buffer on|off;
默认:client_body_in_single_buffer off;
配置块:http、server、location
4.3 存储http头部的内存buffer大小
语法:client_header_buffer_size size;
默认:client_header_buffer_size 1k;
配置块:http、server
4.4 存储超大http头部的内存buffer大小
语法:large_client_header_buffers number size;
默认:large_client_header_buffers 48k;
配置块:http、server
4.5 存储http包体的内存buffer的大小
语法:client_body_buffer_size size;
默认:client_body_buffer_size 8k/16k;
配置块:http、server、location
4.6 http包体的临时存放目录
语法:client_body_temp_path dir-path[level1[level2[level3]]]
默认:client_body_temp_path client_body_temp;
配置块:http、server、location
该参数的主要作用是指定了存储http包体的磁盘目录,后面的level表示可以有几级子目录,这是因为如果请求比较多,那么生成的文件就会比较多,频繁的访问同一个目录可能会降低性能,因而可以设置多级子目录用于文件的存放;
需要注意的是,上述level参数表示的是所生成的目录名占有目标文件名字符的个数,比如生成的目标文件名为00000123456
,而上述参数按如下配置:
client_body_temp_path /opt/nginx/client_temp 1 2;
那么nginx就会截取目标文件名的最后1个字符作为一级目录,倒数第二个和第三个总共两个字符作为二级目录,最终文件将会存储在如下目录:
/opt/nginx/client_temp/6/45/00000123456
nginx在生成目标文件时,其文件名是以顺序递增的整数进行命名的。
4.7 connection_pool_size
语法:connection_pool_size size;
默认:connection_pool_size 256;
配置块:http、server
4.8 request_pool_size
语法:request_pool_size size;
默认:request_pool_size 4k;
配置块:http、server
5. 网络连接的设置
5.1 读取http头部的超时时间
语法:client_header_timeout time(默认单位:秒);
默认:client_header_timeout 60;
配置块:http、server、location
5.2 读取http包体的超时时间
语法:client_body_timeout time(默认单位:秒);
默认:client_body_timeout 60;
配置块:http、server、location
5.3 发送响应的超时时间
语法:send_timeout time;
默认:send_timeout 60;
配置块:http、server、location
5.4 reset_timeout_connection
语法:reset_timeout_connection on|off;
默认:reset_timeout_connection off;
配置块:http、server、location
5.5 lingering_close
语法:lingering_close off|on|always;
默认:lingering_close on;
配置块:http、server、location
5.6 lingering_time
语法:lingering_time time;
默认:lingering_time 30s;
配置块:http、server、location
5.7 lingering_timeout
语法:lingering_timeout time;
默认:lingering_timeout 5s;
配置块:http、server、location
5.8 对某些浏览器禁用keepalive功能
语法:keepalive_disable [msie6|safari|none]...
默认:keepalive_disablemsie6 safari
配置块:http、server、location
5.9 keepalive超时时间
语法:keepalive_timeout time(默认单位:秒);
默认:keepalive_timeout 75;
配置块:http、server、location
5.10 keepalive长连接上能够承载的最大请求数
语法:keepalive_requests n;
默认:keepalive_requests 100;
配置块:http、server、location
5.11 tcp_nodelay
语法:tcp_nodelay on|off;
默认:tcp_nodelay on;
配置块:http、server、location
5.12 tcp_nopush
语法:tcp_nopush on|off;
默认:tcp_nopush off;
配置块:http、server、location
6. MIME类型的设置
6.1 MIME type与文件扩展的映射
语法:type {...};
配置块:http、server、location
6.2 默认MIME type
语法:default_type MIME-type;
默认:default_type text/plain;
配置块:http、server、location
6.3 types_hash_bucket_size
语法:types_hash_max_size size;
默认:types_hash_max_size 1024;
配置块:http、server、location
6.4 types_hash_max_size
语法:types_hash_max_size size;
默认:types_hash_max_size 1024;
配置块:http、server、location
7. 对客户端请求的限制
7.1 按http方法名限制用户请求
语法:limit_except method...{...}
配置块:location
该配置项的主要作用是限制某些方法的请求的访问,后面的参数可取GET、HEAD、POST、DELETE、MKCOL、COPY、MOVE、OPTIONS、PROPFIND、PROPPATCH、LOCK、UNLOCK或者PATCH。示例如:
limit_except GET {
allow 192.168.1.0/32;
deny all;
}
上述配置将会限制所有的GET请求的访问,而允许其他方法的请求。
7.2 http请求包体的最大值
语法:client_max_body_size size;
默认:client_max_body_size 1m;
配置块:http、server、location
7.3 对请求的限速
语法:limit_rate speed;
默认:limit_rate 0;
配置块:http、server、location、if
该配置主要是对客户端的请求进行每秒传输的字节大小进行限速,默认为0,表示不限速;
针对不同的客户端,可以使用$limit_rate
参数执行不同的限速策略。如:
server {
if ($slow) {
set $limit_rate 4k;
}
}
7.4 limit_rate_after
语法:limit_rate_after time;
默认:limit_rate_after 1m;
配置块:http、server、location、if
8. 文件操作的优化
8.1 sendfile系统调用
语法:sendfile on|off;
默认:sendfile off;
配置块:http、server、location
8.2 AIO系统调用
语法:aio on|off;
默认:aio off;
配置块:http、server、location
8.3 directio
语法:directio size|off;
默认:directio off;
配置块:http、server、location
8.4 directio_alignment
语法:directio_alignment size;
默认:directio_alignment 512;
配置块:http、server、location
8.5 打开文件缓存
语法:open_file_cache max=N[inactive=time]|off;
默认:open_file_cache off;
配置块:http、server、location
8.6 是否缓存打开文件错误的信息
语法:open_file_cache_errors on|off;
默认:open_file_cache_errors off;
配置块:http、server、location
8.7 不被淘汰的最小访问次数
语法:open_file_cache_min_uses number;
默认:open_file_cache_min_uses 1;
配置块:http、server、location
8.8 检验缓存中元素有效性的频率
语法:open_file_cache_valid time;
默认:open_file_cahce_valid 60s;
配置块:http、server、location
9. 对客户端请求的特殊处理
9.1 忽略不合法的http头部
语法:ignore_invalid_headers on|off;
默认:ignore_invalid_headers on;
配置块:http、server
9.2 http头部是否允许下划线
语法:underscores_in_headers on|off;
默认:underscores_in_headers off;
配置块:http、server
9.3 对If-Modified-Since头部的处理策略
语法:if_modified_since [off|exact|before];
默认:if_modified_since exact;
配置块:http、server、location
9.4 文件未找到时是否返回error日志
语法:log_not_found on|off;
默认:log_not_found on;
配置块:http、server、location
9.5 merge_slashes
语法:merge_slashes on|off;
默认:merge_slashes on;
配置块:http、server、location
9.6 DNS解析地址
语法:resolver address...;
配置块:http、server、location
9.7 DNS解析的超时时间
语法:resolver_timeout time;
默认:resolver_timeout 30s;
配置块:http、server、location
9.8 返回错误页面时是否在server中注明nginx版本
语法:server_tokens on|off;
默认:server_tokens on;
配置块:http、server、location
感谢各位的阅读,以上就是“Nginx的基本属性配置”的内容了,经过本文的学习后,相信大家对Nginx的基本属性配置这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!