nginx.conf参数优化详解


#user www www; worker_processes auto; error_log /nginx/logs/nginx_error.log crit; pid /nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; # nginx的工作模式及连接上线 events { use epoll; worker_connections 65535; #定义nginx每个进程的最大连接数 multi_accept on; #此指令的作用是立即接受所有连接放到监听队列中。 如果指令被禁用,worker进程将逐个接受连接 } http { include mime.types; #实现对配置文件所包含的文件的设定 default_type application/octet-stream; #设置默认类型为二进制流 server_names_hash_bucket_size 128; # 保存服务器名字的hash表 client_header_buffer_size 32k; #指定来自客户端请求头的headerbuffer大小,设置为32KB large_client_header_buffers 4 32k; #指定客户端请求中较大的消息头的缓存最大数量和大小,这里是4个32KB client_max_body_size 50m; #上传文件大小 map $http_x_forwarded_for $clientRealIp { "" $remote_addr; ~^(?P<firstAddr>[0-9.]+),?.*$ $firstAddr; } #用正则匹配,从 x_forwarded_for 中取得用户的原始IP 通过 map 创建了一个变量 $clientRealIp;这个就是 原始用户的真实 IP 地址, sendfile on; #开启高效模式文件传输模式,将tcp_nopush和tcp_nodely两个指另设置为on,用于防止网络阻塞。 tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; #设置客户端连接保持活动的超时时间 log_format fastcgi_connect_timeout 300; # 指定连接到后端FastCGI的超时时间 fastcgi_send_timeout 300; #指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间 fastcgi_read_timeout 300; #指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间 fastcgi_buffer_size 64k; #于指定读取FastCGI应答第一部分需要用多大的缓冲区 fastcgi_buffers 4 64k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求 fastcgi_busy_buffers_size 128k; #默认值是fastcgi_buffers的两倍 fastcgi_temp_file_write_size 256k; #表示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍 gzip on; #开启gzip 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中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; #压缩的类型 gzip_vary on; #让前端的缓存服务器混村经过的gzip压缩的页面 gzip_proxied expired no-cache no-store private auth; #Nginx作为反向代理的时候启用,根据某些请求和应答来决定是否在对代理请求的应答启用gzip压缩{依据请求头} gzip_disable "MSIE [1-6]."; # 禁用IE6的gzip压缩 #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; # 隐藏版本号 #log format #设置日志的记录格式 # $remote_addr 客户端地址;$remote_user 客户端用户名称;$time_local 访问时间和时区;$request 请求的URI和HTTP协议; $status HTTP请求状态;$body_bytes_sent 发送给客户端文件内容大小;$http_referer url跳转来源 $http_user_agent 用户终端浏览器等信息;http_x_forwarded_for 获取到IP地址 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log off; include vhost/*.conf; }
原文地址:https://www.cnblogs.com/suyj/p/9995246.html