#user nobody; worker_processes 2;#开启的进程数,一般跟逻辑cpu核数一致 worker_cpu_affinity 00000001 00000010 ; error_log logs/error.log crit; #定于全局错误日志文件,级别以notice显示。还有debug、info、warn、error、crit模式,debug输出最多,。 pid nginx.pid; worker_rlimit_nofile 102400; #指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制 events { use epoll; worker_connections 1024000;#允许连接数 } http { include mime.types; default_type application/octet-stream; #include proxy.conf; #一定要指向代理文件 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; client_max_body_size 20m; #设置允许客户端请求的最大的单个文件字节数 sendfile on; #开启高效文件传输模式 tcp_nopush on; #开启防止网络阻塞 charset utf-8; tcp_nodelay on; #开启防止网络阻塞 keepalive_timeout 120; #设置客户端连接保存活动的超时时间 client_header_timeout 10; #用于设置客户端请求读取超时时间 client_body_timeout 10; #用于设置客户端请求主体读取超时时间 client_header_buffer_size 4k; open_file_cache max=102400 inactive=20s;#max指定缓存数量,inactive是指经过多长时间文件没被请求后删除缓存。 open_file_cache_valid 30s;#30秒检查一次缓存的有效信息 open_file_cache_min_uses 1;#缓存中文件的使用次数少于1次将被删除 send_timeout 10; #用于设置相应客户端的超时时间 #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。 fastcgi_cache_key $host$uri$is_args$args; fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 16 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_cache TEST; fastcgi_cache_valid 200 1h; fastcgi_cache_valid 304 301 1d; fastcgi_cache_valid any 1m; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; proxy_http_version "1.1"; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 304 302 24h; proxy_temp_file_write_size 10m; proxy_temp_path /usr/local/nginx/nginx_cache/temp_dir; proxy_cache_path /usr/local/nginx/nginx_cache/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g; proxy_connect_timeout 300s; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 32k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Host $host; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #httpGzip modules gzip on; #开启gzip压缩 gzip_disable "MSIE [1-6].";#禁止IE1-6压缩 gzip_min_length 1k; #设置允许压缩的页面最小字节数 gzip_buffers 4 16k; #申请4个单位为16K的内存作为压缩结果流缓存 gzip_http_version 1.1; #设置识别http协议的版本,默认是1.1 gzip_comp_level 2; #指定gzip压缩比,1-9 数字越小,压缩比越小,速度越快. gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php ;#image/jpeg image/gif image/png; # 这里实现轮询 upstream www.dahongwa.com{ server 10.1.7.*:9000 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9001 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9002 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9003 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9004 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9007 weight=10 max_fails=4 fail_timeout=30s; } # 这里实现轮询 upstream img.dahongwa.com{ server 10.1.7.*:9019 weight=10 max_fails=4 fail_timeout=30s; server 10.1.7.*:9011 weight=10 max_fails=4 fail_timeout=30s; } server { listen 80; server_name www.dahongwa.com; root www.dahongwa.com; location /{ index index.html index.jsp index.htm; proxy_redirect off; proxy_pass http://www.dahongwa.com; # } location ~ .*\.(css|js)(.*) { expires max; access_log img.dahongwa.com.log; } ## 缓存部分 location ~ /photoserver/.*\.(gif|jpg|png|htm|jpeg|bmp|html|flv|ico|swf|doc)(.*) { root nginx_cache/cache; if (!-f $request_filename) { proxy_pass http://img.dahongwa.com; #图片服务器 } proxy_cache cache_one; proxy_cache_valid 200 304 302 24h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_cache_methods GET HEAD POST; proxy_cache_min_uses 1; expires max; access_log logs/img.dahongwa.com.log; } location ~ /common/goods/images/.*\.(gif|jpg|png|htm|jpeg|bmp|html|flv|ico|swf|doc)(.*) { if (!-f $request_filename) { proxy_pass http://img.dahongwa.com; #图片服务器 } if ( -f $request_filename ) { expires 10y; } proxy_cache cache_one; proxy_cache_valid 200 304 302 24h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_cache_methods GET HEAD POST; proxy_cache_min_uses 1; expires max; access_log logs/img.dahongwa.com.log; } location ~ .*\.(sh|bash)(.*){ rewrite ^/(.*) http://www.dahongwa.com; } location ~ /purge(/.*) { allow 10.244.1.170; allow 10.1.7.114; allow 10.1.7.115; deny all; proxy_cache_purge cache_one $host$uri$is_args$args; } access_log logs/www.dahongwa.com.log; } }
timer_resolution 100ms; worker_processes 4;#为避免阻塞 加快处理进度,设置大一点 reset_timedout_conncetion off; 连接超时时 立马清除内存中相关对象。 使用环境 http server location recursive_error_pages off; 防止错误页面递归 使用环境 http server location send_timeout 100; 设置响应超时 当超过时间时 nginx 将会关闭连接。 使用环境 http server location linux 系统性能优化 1 nginx 每次访问完一个文件后,linux 将会对她的Access访问时间进行修改 这对磁盘写操作影响是非常大的 因此需要关闭 原配置 为 /dev/sdb1 /dataext3 defaults 0 0 修改为 /dev/sdb1 /dataext3 defaults,noatime,nodiratime 0 0 重启系统有效 2 ulimit -n 用户打开文件个数 默认 1024 ulimit -u 用户打开进程数 默认 8040 需要在 /etc/security/limits.conf 文件下添加 如下配置 * soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535 重启系统有效 打开文件限制数量 也可以使用nginx配置完成 worker_rlimit_nofile 65535 3 优化内核TCP 选项 fs.file-max = 65535 kernel.pid_max = 65536 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_timestsmps = 0 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 10000 65535 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_wmem = 8192 436600 873200 net.ipv4.tcp_rmem = 32768 436600 873200 net.ipv4.tcp_mem = 94500000 91500000 92700000 net.ipv4.tcp_max_orphans = 3276800 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 sysctl -p /etc/sysctl.conf // 作用:重新载入/etc/sysctl.conf文件 4 计算开启的进程数 5 限制流量 limit_rate 4K // 该指令用于指定向客户端传输数据的速度,针对单个连接,如果同时有两个连接, 那么他的速度将是该指令设置的2倍。默认值为 no 使用环境:http server location if in location limit_rate_after 3m //以最大的速度下载3m之后 再如何如何,默认为 1m. 使用环境 http server location if in location 配置实例 location /download{ limit_rate_after 3m; limit_rate 512k; } 解释为 以最大速度下载3m之后 再以51k的速度下载。 6 limit_zone 用于定义一个zone 用于存储回话状态 默认值 no FX: limit_zone someone $binary_remote_addr 10m; 设置一个叫 someone 的zone,设置存储回话的大小 为10m $binary_remote_addr 长度为 4字节,可以存储更多的会话。直接使用IP地址 其长度为 7-15个字节。 使用环境 http 7 limit_conn 设置一个会话最大的并发数 ,如果超出这个限制 将出现 503 默认 no 使用环境 http server location 8 limit_conn_log_level 当达到连接级别时 将会产生日志 语法 limit_conn_log_level info|notice|warn|error 默认 error 使用环境 http server location 举个例子 http{ limit_zone someone $binary_remote_addr 10m; server{ listen 80; location /download{ limit_conn someone 1; } } } 重新启动nginx之后,我们用两个浏览器去访问下载资源的时候,第一个会显示 下载另存为 后面的那个浏览器就不会了,只会显示 服务器太忙,暂时无法提供服务,实际上是同一个IP连接受限制了。 这个在资源下载的时候 用到比较合适。 9 隐藏nginx 版本号 出于安全考虑 需要隐藏版本号 在http段 加入 server_tokens off; 10 open_log_file_cache 用于设置缓存 存储带有变量的日志文件路径而又频繁使用的文件描述符,这些被频繁使用的文件描述符将会被 缓存在缓存中。默认值 off 可选项:max 换存中可以存储的最大描述符数量。 inactive :设置时间间隔 在这个时间间隔内 没有被命中的描述符将会被移除。默认 10秒。 min_uses 用来设置访问次数。在一定的时间间隔内 一个文件描述符至少被访问多少次后就将该描述符放入换存中,默认为1. valid 用于设置检查同名文件存在的时间,默认值是 60秒。 例如 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m; 使用环境 http server location 11 nginx 防止DDos攻击 limit_req_log_level 该指令用于控制记录延时消息日志的级别,他的默认值为warn 只能放置在http段。 limit_req_zone 该指令定义了一个区域,用于存储回话状态,至于会话中存储的是什么,则由变量来决定, 该指令有三个值 指定变量是第一个值,在这里我们使用的是 $binary_remote_addr 在上面的实例中指定存储回话的zone名字为someone 并且指定用于存储回话的空间为10mb 这是第二个值 至于第三个值 就是 rate=1r/s 他表示对该zone的平均查询速度,单位是每秒钟多少个请求。 实际应用中可以将这个值设置为 10个左右。设置了回话限制后,访问这里的每一个回话都将会被跟踪。对于速度的单位, 可以设置为 每秒钟请求数(r/s) 也可以设置每分钟请求书(r/m) 该指令只能放置在http段 没有默认值。 limit_req 该指令指定的zone someone ,并且同时指定了该zone最大可能的突发请求数burst 如果请求的rate超过这个值,那么清酒就会 被延迟 过量的请求被延迟时,直到请求的数量小于指定的burst值,因此对于请求率要有一个合适的速率,在这种情况下 如果你继续访问 将会出现503 该指令可以放置在http server location 访问 查看进程 lsof -i:80|grep nginx |wc -l 查看请求情况 time ab -n 1000 -c 100 http://192.168.3.175/download 预防DDos 攻击 还可以通过限制nginx的最高连接数 减少 keepalive_timeout 的值等措施。 12 缓存设置 其他选项不详解 新增 must-revalidate 强制浏览器重新验证文件是否过期。 add_header Cache-Control must-revalidate 使用环境 http location 13 proxy-revalidate 如果设置改值则会强制proxy严格遵守在nginx服务器端设置的缓存规则。 add_header Cache-Control proxy-revalidate 14 因系统频繁打开文件造成的500错误。 empty_gif 使用环境 location location = /1.gif{ empty_gif; }