nginx 的配置主要可以划分为main、events、http、server、location 块。
下面就以配置文件作为参考
每个指令必须以分号结束
|
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# main块 user nobody nobody; # 配置nginx运行的用户或者组,如果只配置了一个,说明用户跟组都是同一个名称 worker_processes 1; # 允许生成的进程数,默认为1;可以设置为auto,一般设置为cpu的核心数 #pid logs/nginx.pid; # nginx 进程pid的存放地址 # 制定日志路径,级别。这个设置可以放入全局块,http块,server块, # 级别以此为:debug|info|notice|warn|error|crit|alert|emerg error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; # events块 events { accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; #最大连接数,默认为512 } # http块 http { # http 全局块 include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream; #默认文件类型,默认为text/plain #access_log off; #取消服务日志 # 自定义日志模板 main 为自定义日志模板的名称 #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; sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。 # 启用了sendfile 才会生效,作用是等数据包累积到一定大小才发送 #tcp_nopush on; # 开启gzip压缩,对于文本文件,在服务端发送响应之前进行 GZip 压缩,压缩后的文本大小会减小到原来的 1/4 - 1/3 gzip on; # 负载均衡配置 upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; #热备(其它所有的非backup机器down或者忙的时候,请求backup机器)) } upstream mysvr2 { #weigth参数表示权值,权值越高被分配到的几率越大 server 192.168.1.11:80 weight=5; server 192.168.1.12:80 weight=1; server 192.168.1.13:80 weight=6; } # server 块 server { # server 全局块 listen 80; # 监听端口 server_name localhost; # 监听地址 keepalive_requests 120; #单连接请求上限次数 # http请求强制跳转https rewrite ^(.*)$ https://$host$1 permanent; #charset koi8-r; #access_log logs/host.access.log main; # location 块 location / { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写 root html; #根目录设置 index index.html index.htm; #设置默认页,html/index.html proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表 deny 127.0.0.1; #拒绝的ip allow 172.18.5.54; #允许的ip } # HTTPS配置,此配置需要ssl模块的支持 server { listen 443 ssl; server_name localhost; # https 证书地址 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; } } } |
|
1 2 3 4 5 6 7 8 |
user nobody nobody; worker_processes 1; pid logs/nginx.pid; daemon off; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; worker_rlimit_nofile 65535; |
可以通过·getconf PAGESIZE· 来查看LINUX的分页大小
|
1 2 3 4 5 6 7 8 9 10 |
events { #use epoll; accept_mutex on; multi_accept on; worker_connections 20000; client_header_buffer_size 4k; open_file_cache max=2000 inactive=60s; open_file_cache_valid 60s; open_file_cache_min_uses 1; } |
惊群现象:指多进程(多线程)在同时阻塞等待同一个事件的时候(休眠状态),如果等待的这个事件发生,那么他就会唤醒等待的所有进程(或者线程),但是最终却只能有一个进程(线程)获得这个时间的“控制权”,对该事件进行处理,而其他进程(线程)获取“控制权”失败,只能重新进入休眠状态,这种现象和性能浪费就叫做惊群效应
nginx服务器每个工作进程可以同时接受多个新的网络连接,但是默认是关闭的,需要在nginx.conf配置文件中设置multi_accept on;
Linux查询系统分页大小命令:getconf PAGESIZE