nginx可优化配置

## 定义Nginx运行的用户和用户组
user  www www;                       
## nginx进程数,建议设置为等于CPU总核心数,最多开启8个,超过8个稳定性不高,性能提升受限
worker_processes auto;
## 多核CPU利用参数
worker_cpu_affinity auto;
## 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log  /usr/local/nginx/logs/error.log  warn;
## nginx的pid路径
pid nginx.pid;
## worker进程最大打开文件数
worker_rlimit_nofile 65535;

events模块中包含nginx中所有处理连接的设置

events
{
## 使用epoll的I/O 模型
    use epoll;
## 如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接
    multi_accept on;
## 设置可由一个worker进程同时打开的最大连接数
    worker_connections 65530;
}

MIME-type和Content-Type的关系:

当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的Content-Type,然后客户端如浏览器根据Content-Type的值处理文件。

http
{
## include表示纳入mime.types文件的配置
    include       mime.types;
## 如果Web程序没设置,Nginx也没找到对应文件的扩展名的话,就使用默认的Type,这个在Nginx 里用 default_type定义: default_type application/octet-stream,这是应用程序文件类型的默认值。
    default_type  application/octet-stream;
## 设置编码格式,一般情况下不需要
    charset utf-8;
## 隐藏版本号
    server_tokens off;
## 开启高效文件传输模式
    sendfile on;
## 当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
    tcp_nopush on;
## 确保数据尽快发送, 提高可数据传输效率
    tcp_nodelay on;
## 指定每个 TCP 连接最多可以保持多长时间
    keepalive_timeout 40s;
## 客户端向服务端发送一个完整的 request header 的超时时间
    client_header_timeout 10;
## 指定客户端与服务端建立连接后发送 request body 的超时时间
    client_body_timeout 10;
## 客户端请求头缓存 大小 4k
    client_header_buffer_size 4k;
## nginx配置多个域名.保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
    server_names_hash_bucket_size 128;
## 设置用于读取大客户端请求头的缓冲区的最大数目和大小
    large_client_header_buffers 4 32k;
## 大文件上传限制
    client_max_body_size 100m;
## 服务端向客户端传输数据的超时时间
    send_timeout 10;
## 告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
    reset_timedout_connection on;
## 打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉
    open_file_cache max=102400 inactive=20s;
## 定义了open_file_cache中指令参数不活动时间期间里最小的文件数
    open_file_cache_min_uses 1;
## 在open_file_cache中指定检测正确信息的间隔时间
    open_file_cache_valid 30s;
 
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;      #出现110错误需要更改相应大小值
    fastcgi_buffers 256 128k;
    fastcgi_busy_buffers_size 256k;    #fastcgi_buffers的两倍
    fastcgi_temp_file_write_size 256k; #fastcgi_buffers的两倍
 ## 后端服务器连接的超时时间_发起握手等候响应超时时间
    proxy_connect_timeout 90;
## 后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
    proxy_send_timeout 90;
# 后端服务器处理请求的时间
    proxy_read_timeout 90;
# Nginx使用该大小申请read_buf,即大小指定了 upstream header 最大长度,如果响应头超过了这个长度会报502    
proxy_buffer_size 128k;        #出现110错误需要更改相应大小值
# 设置存储被代理服务器响应的body所占用的buffer个数和每个buffer大小
proxy_buffers 256 128k;
    
proxy_busy_buffers_size 256k;    #proxy_buffers的两倍
    proxy_temp_file_write_size 256k; #proxy_buffers的两倍
 # 告诉nginx采用gzip压缩的形式发送数据;这将会减少我们发送的数据量
    gzip  on;
# 设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度
    gzip_min_length     256;
    gzip_buffers        4 16k;
    gzip_http_version   1.1;
    gzip_vary on;
# 设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置
    gzip_comp_level 5;
    gzip_disable "MSIE [1-6]\.";
# 允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。
    gzip_proxied any;
# 设置需要压缩的数据格式
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;
 
    log_format access '{"@timestamp":"$time_iso8601",'
            '"host":"$server_addr",'
            '"clientip":"$remote_addr",'
            '"size":$body_bytes_sent,'
            '"responsetime":$request_time,'
            '"upstreamtime":"$upstream_response_time",'
            '"upstreamhost":"$upstream_addr",'
            '"http_host":"$host",'
            '"url":"$uri",'
            '"xff":"$http_x_forwarded_for",'
            '"referer":"$http_referer",'
            '"agent":"$http_user_agent",'
            '"status":"$status"}';
    access_log  /usr/local/nginx/logs/access.log  access;
    server {
        listen 80;
        server_name localhost;
        index index.html index.php;
        root /data/www/default;
        access_log /usr/local/nginx/logs/access.log;
        error_log /usr/local/nginx/logs/error.log;
 
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }
 
 
        location ~ \.php {
            expires  -1;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。