003.Nginx原理和配置
1. Nginx原理
- 对于每个Worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销
- 采用独立的进程,互相之间不会相互影响,一个进程退出后,其他进程还在工作,服务不会中断,master进程很快就会启动新的worker进程,当然,请求到意外挂掉的worker上的请求肯定会失败
- 通常服务器有多少个核就设置多少个worker数,Nginx同Redis一样,使用IO多路复用机制,通过异步非阻塞的方式来处理请求,每个worker可以把一个CPU的性能发挥到极致,所以worker数和服务器CPU核数相等是最好的,设置少了会浪费CPU设置多了则会造成频繁的CPU切换,影响性能
2.Nginx配置详解
# 为了安全起见,给Worker的权限一定是最小的,建议就使用nobody,最不建议使用root #user nobody; # 设置Worker数量,设置为服务器CPU核数最为适宜 worker_processes 1; # Work绑定CPU(4个Worker绑定4个CPU) # work_cpu_affinity 0001 0010 0100 1000 # Work绑定CPU(4个Worker绑定8个CPU中的4个) # work_cpu_affinity 00000001 00000010 00000100 00001000 # error_log path level # level: debug/info/notice/warn/error/crit,从左到右日志详细程度递减,默认为crit #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; # pid存放位置 #pid logs/nginx.pid; events { # 每个Worker进程所能建立的最大连接数 # 一个Nginx可以建立的最大连接数为max_connection=${num_workers}*${worker_connections} # 如果客户端访问Nginx的静态资源,那么Nginx允许的最大并发为${max_connection}/2 # 因为每个访问要占用两个连接,请求到Worker使用一个连接,Worker将静态资源返回给客户端也要一个连接 # 如果客户端访问Nginx且反向代理到后端的Tomcat,则处理一次请求使用4个了连接 # 请求到Worker使用一个,Worker到Tomcat使用一个,Tomcat返回给Worker使用一个,Worker到客户端使用一个 # 所以如果客户端请求反向代理,Nginx最大的并发数为${max_connection}/4 worker_connections 1024; # 使用epoll来实现IO多路复用 # 一般就选择epoll就可以,(*BSD)系列的Linux使用kquene # Windows不支持IO多路复用,不用配置 use epoll; # 当一个Worker抢占到一个连接时,是否尽可能的让其获得更多的连接,默认是off # 高并发场景下建议开启 multi_accept on; # 是否开启nginx的抢占锁机制,默认on # 当并发量小的时候,没有必要开 # 当并发量大的时候,开启此机制,让处理能力强的Worker去抢占更多的请求 accept_mutex on; } http { # 当Web服务器收到静态资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type # 再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件 # conf/mime.types文件中记录了大部分媒体类型 include mime.types; # 如果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"'; # 访问日志的路径和格式 #access_log logs/access.log main; # 开启从磁盘直接到网络的文件传输,适用于大文件上传下载时提高IO效率 sendfile on; #tcp_nopush on; # 一个请求完成之后还要保持连接多久,默认为0,表示请求完成之后立即关闭连接 #keepalive_timeout 0; keepalive_timeout 65; # 开启或关闭gzip模块,如果要使用压缩功能,必须开启 #gzip on; # 设置允许压缩的页面最小字节数,页面字节数可以从Header的Content-Length中进行获取 gzip_min_lenth 1k; # gzip压缩比,压缩比越小,处理速度越快,压缩比越大,压缩后文件越小,但也越消耗CPU,可配置范围1-9 gzip_comp_level 4; # 匹配MIME类型进行压缩,无论是否指定,"text/html"类型总是会被压缩 #gzip_types types text/plain text/css application/json application/x-javascript text/xml; # 动静分离配置 # 服务器端静态资源缓存,设置缓存到内存中的最多的文件个数及其不活跃时间 open_file_cache max=655360 inactive=20s; # 活跃期限内最少使用的次数,否则视为不活跃(在20s内如果资源被使用了两次,视为活跃,否则就是不活跃资源,从缓存中剔除) open_file_cache_min_uses 2; # 验证是否活跃的时间间隔 open_file_cache_valid 30s; upstream myserver { # 反向代理的算法 # 1.轮询(默认),每个请求按照时间顺序逐一分配到后端服务器,可以自动剔除挂掉的后端服务器 # 2.设置权重,设置轮询的几率,weight和访问比例成正比,适用于后端服务器性能不均衡的情况 # 3.IP绑定,ip_hash,每个请求按照请求的源IP的hash结果进行分配,这样每个用户固定访问一个后端服务器,解决session共享问题 # 4.备机方式,backup,只有当所有的非备机全部宕机的情况下, # 5.fair(第三方),按后端服务器的响应时间来分配请求,响应时间短的优先分配 # 6.url_hash(第三方),按访问url的hash结果来分配请求,是每个url都固定访问一个后端服务器,后端服务器为缓存时比较有效 # ip_hash; # hash $request_uri; # hash方法使用的算法 # hash_method crc32; server 10.0.0.1:8080 weight=1; server 10.0.0.1:8081 weight=1 backup; } server { # 监听端口号 listen 80; # 服务器名称 server_name 10.0.0.102; # 字符集 #charset utf-8; #access_log logs/host.access.log main; # location [=|~|~*|^~] /uri/... # = 精确匹配 # ~ 正则匹配,区分大小写 # ~* 正则匹配,不区分大小写 # ^~ 关闭正则表达式 # 匹配原则 # 1.匹配分两个阶段,普通匹配和正则匹配 # 2.普通匹配,首先通过"="来匹配完全精确的location # 2.1 如果没有精确匹配到,那么按照最大前缀匹配的原则来匹配location # 2.2 如果匹配到的location有"^~",则以此location为匹配最终结果,如果没有那么会把匹配结果暂存,继续进行正则匹配 # 3.正则匹配,一次从上到下匹配前缀是"~"或者"~*"的location,一旦匹配成功一次,则立刻以此location为准,不再向下进行继续进行正则匹配 # 4.如果正则匹配都不成功,则继续使用之前暂存的普通匹配成功的location location / { # 匹配任何查询,因为所有请求都以"/"开头,但是正则表达式规则和长的块规则将被优先和查询匹配 # 定义服务器的默认网站根目录位置 root html; # 默认访问首页文件的名称 index index.html index.htm; # 反向代理路径 proxy_pass http://myserver; # 反向代理的超时时间 proxy_connect_timeout 10; proxy_redirect default; } location /images/ { root images; } location ^~ /images/jpg/ { # 匹配任何以/images/jpg/开头的任何查询并停止继续向下匹配,所有的正则表达式将不会被测试 root images/jpg/; } location ~*.(gif|jpg|jpeg)$ { # 所有静态文件直接读取磁盘 root pic; # 用户的浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 3d; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }