454luikty 发表于 2017-8-21 11:08:53

Nginx优化配置详解

基本参数优化server_tokens off;#关闭在错误页面中的nginx版本号,安全性是有好处的sendfile on;#传输文件时发挥作用tcp_nopush on;#一个数据包里发送所有头文件tcp_nodelay on;#不缓存数据keepalive_timeout 10; #在这个超时时间过后关闭客户端链接client_header_timeout 10; #设置请求头的超时时间client_body_timeout 10;#设置请求体的超时时间reset_timeout_connection on;#开启关闭不响应的客户端连接功能,释放客户端所占的内存空间send_timeout 10;#客户端的响应超时时间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。#limit_conn_zone 设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K5)32byte状态或者(16K5)64byte状态。limit_conn#为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接。default_type#设置文件使用的默认的MIME-type。charset#设置我们的头文件中的默认的字符集
Gzip压缩优化gzip_types#压缩的文件类型 text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascriptgzip on;#采用gzip压缩的形式发送数据
gzip_disable "msie6"#为指定的客户端禁用gzip功能gzip_static;#压缩前查找是否有预先gzip处理过的资源gzip_proxied any;#允许或者禁止压缩基于请求和响应的响应流gzip_min_length1000;#设置对数据启用压缩的最少字节数gzip_comp_level 6;#设置数据的压缩等级
FastCGI参数优化fastcgi_cache_path /data/ngx_fcgi_cache #缓存路径levels=2:2 #目录结构等级keys_zone=ngx_fcgi_cache:512m            #关键字区域存储时间inactive=1d #非活动删除时间       fastcgi_connect_timeout 240; #连接到后端fastcgi的超时时间fastcgi_send_timeout 240; #建立连接后多久不传送数据就断开fastcgi_read_timeout 240; #接收fastcgi应答的超时时间fastcgi_buffer_size 64k; #指定读取fastcgi应答缓冲区大小fastcgi_buffers 4 64k;#指定本地缓冲区大小(缓冲FaseCGI应答请求)fastcgi_busy_buffers_size 128k; #繁忙时的buffer,可以是fastcgi_buffer的两倍fastcgi_temp_file_write_size128k; #在写入缓存文件时用多大的数据块,默认是fastcgi_buffer的两倍fastcgi_cache mingongge;#开启缓存时指定一个名称fastcgi_cache_valid 200 302 1h;#指定应答码200 302 缓存一小时fastcgi_cache_valid 301 1d; #指定应答码301缓存一天fastcgi_cache_valid any 1m;#指定其它应答码缓存一月
其它参数优化open_file_cache#指定缓存最大数目以及缓存的时间open_file_cache_valid#在open_file_cache中指定检测正确信息的间隔时间open_file_cache_min_uses   #定义了open_file_cache中指令参数不活动时间期间里最小的文件数open_file_cache_errors   #指定了当搜索一个文件时是否缓存错误信息location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$#指定缓存文件的类型      {      expires 3650d;             #指定缓存时间      }      location ~ .*\.(js|css)?$      {      expires 3d;                           }expires有个缺点就是如果更新WEB数据后,用户没有清理缓存,会看到旧的数据,因此建议将时间设置短一点
优化后完整的配置文件user www; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofile 100000; events { worker_connections 2048; multi_accept on; use epoll; } http { server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; access_log off; error_log /var/log/nginx/error.log crit; keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10; limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; include /etc/nginx/mime.types; default_type text/html; charset UTF-8; gzip on; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }

页: [1]
查看完整版本: Nginx优化配置详解