通过解析大神配置的nginx.conf,在实践中学习nginx配置。
1、worker_process/worker_connections
user tengine;
worker_processes 1;
events {
worker_connections 1500;
}
worker_processes:worker进程的数量; 如果没有特别大的性能消耗,通常设为1就足够了。
worker_connections:每个worker进程的连接数。
通过worker_processes和worker_connnections可以计算出nginx的max_clients。
默认情况下,max_clients=worker_processes * worker_connection。
作反向代理时,max_clients=worker_processes * worker_connections/4。
2、动态加载模块dso
这个模块主要是用来运行时动态加载模块,而不用每次都要重新编译Tengine。
- 动态加载模块的个数限制为128个
- 只有http模块支持动态加载模块。
dso {
# load ngx_http_rewrite_module.so;
load ngx_http_reqstat_module.so;
load ngx_http_access_module.so;
load ngx_http_concat_module.so;
load ngx_http_limit_conn_module.so;
load ngx_http_limit_req_module.so;
load ngx_http_sysguard_module.so;
load ngx_http_upstream_session_sticky_module.so;
load ngx_http_trim_filter_module.so;
}
动态加载的模块解析:
- ngx_http_rewrite_module 通过正则表达式重定向。
- ngx_http_reqstat_module 通过定义的变量来统计nginx的运行情况。
- ngx_http_access_module 通过deny和allow来限制客户端IP ,检查规则按照第一次匹配的IP。
- ngx_http_concat_module 合并多个文件在一个响应报文中。
- ngx_http_limit_conn_module 可以按照定义的键限定每个键值的连接数,也可以设定单一 IP 来源的连接数。只有那些正在被处理的请求所在的连接才会被计数。
- ngx_http_limit_req_module 可以按照定义的键限定每个键值的请求频率,也可以设定单一IP来源的请求频率。
- ngx_http_sysguard_module 该模块监控内存(含swap分区)、CPU 和请求的响应时间,当某些监控指标达到设定的阈值时,跳转的到指定的url。目前该模块仅对系统支持sysinfo函数时,才支持基于load与内存信息的保护,以及系统支持laodavg函数时支持基于load进行保护。模块需要从/proc文件系统中读取内存信息。
- ngx_http_upstream_session_sticky_module 该模块是一个负载均衡模块,通过cookie实现客户端与后端服务器的会话保持, 在一定条件下可以保证同一个客户端访问的都是同一个后端服务器。
- ngx_http_trim_filter_module 该模块用于删除 html , 内嵌 javascript 和 css 中的注释以及重复的空白符。
3、http模块
由于http模块内容较多,将会分块解析,第三点内容内的信息均属于http模块。
http{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$proxy_host:$proxy_port" "$upstream_addr"';
access_log logs/access.log.$year$month$day main;
sendfile on;
keepalive_timeout 65;
client_max_body_size 50m;
include /app/tengine/conf.d/*.conf;
server_tag off;
#gzip压缩功能设置
gzip on;
gzip_min_length 500;
gzip_buffers 32 64k;
#gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
req_status_zone server "$host,$server_addr:$server_port" 10M;
- include mime.types; #MIME是网络资源的媒体类型,使用in