【nginx性能调优】nginx环境参数性能优化配置

更新时间:2019-11-21    来源:nginx    手机版     字体:

【www.bbyears.com--nginx】


老高的服务器最近表示亚历山大,先祭出此篇缓解前端压力,之后再从代码中优化一下。

I. 基本配置

请参考老高的 nginx配置详解。

用基本配置只是使用了nginx的基本特性,许多高级特性我们需要手动打开!

强调几个配置

# http://nginx.org/en/docs/ngx_core_module.html#worker_processes
# auto 1.3.8 and 1.2.5 后的版本都支持
worker_processes auto;

# 可选,老高一般是先设worker_processes为auto,如果最后跑出来的worker_processes>1,再继续设定这个值。
# http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity
worker_cpu_affinity 0001 0010 0100 1000;

# 看性能设置吧,相关命令 ulimit -n
worker_rlimit_nofile 60000;

events {
    # 分给worker_processes的最大连接数,看你机器的性能了
    worker_connections  2048;
    use epoll;
}

http{
    sendfile        on;
    #tcp_nopush     on;
    #tcp_nodelay    on;
    keepalive_timeout  65;
    reset_timedout_connection on;
    server_tokens off;

    # limit ip
    limit_conn_zone $binary_remote_addr zone=ip:10m;
    # limit server
    limit_conn_zone $server_name zone=server:10m;

}

# 下面在某server中做出以下限制
server{
    # 限制一个IP最大连接数
    limit_conn ip 3;
    # 限制一个server的最大连接数
    limit_conn server 100;
    # 限速
    limit_rate 100k;
}
II. 合理的压缩文件

这一点不用说了,js,css,图片最好都用对应的压缩软件压一下。

III. 启用gzip

直接上老高的gzip配置,注意写到http块里!

gzip  on;
#gzip_min_length 1k;
gzip_min_length 0;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png app    lication/vnd.ms-fontobject application/x-font-ttf image/svg+xml;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
IV. 使用nginx-http-concat合并请求

项目地址

nginx-http-concat

因为该模块不是标准模块,所以需要重新编译nginx,并加入--add-module=/tmp/nginx-http-concat即可!

使用方法

在location配置中开启,选项和默认值可以在项目的readme中找到。

location /js/ { 
    # 打开concat 功能 
    # 默认关闭 
    concat on; 
    # 允许concat最大的文件数,默认最大设置十个文件。 
    # (默认: 10) 
    # concat_max_files 10; 
    # 只允许相同类型的文件
    # 默认是开启的 
    # concat_unique on; 
    # 允许内容的类型 
    # (default: application/x-javascript, text/css) 
    # concat_types text/html; 
}
V. 设定过期时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf){
    expires       30d;
}

location ~ .*\.(js|css){
    expires       15d;
}
VI. proxy_cache

使用proxy_cache缓存后端服务器生成的内容。

首先在http块中配置一个cache空间。

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=phpgao:50m inactive=10m max_size=2m;
proxy_temp_path /data/nginx/tmp_cache;

# levels 指定几层hash目录
# keys_zone 空间名 大小 10M 空间名在后面会用到
# inactive 缓存默认时长
# max_size 最大单个文件
# loader_threshold 持续工作时间(单位 毫秒,默认200)
# loader_files 文件数量
# loader_sleeps 达到loader_files后休息时间 (单位 毫秒,默认50)

指定了空间名后,我们可以开始配置server了

server{
    ...
    ...
    location / {
         #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache phpgao;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid  200 304 12h;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
         expires      1d;
    }

}
VII. fastcgi_cache

fastcgi_cache通常用来来缓存php生成的文件,其配置方法与proxy_cache类似。

http段:


    add_header X-Cache-CFC "$upstream_cache_status - $upstream_response_time";

    fastcgi_temp_path /data/nginx/tmp_cache;
    fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=phpgao:50m inactive=10m max_size=2m;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
server段:

server{
    ...
    ...
    location ~ .*\.(php|php5){
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include    fastcgi_params;

        fastcgi_cache phpgao;
        fastcgi_cache_valid 200 302 301 1h;
        fastcgi_cache_valid any 1m;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_methods GET HEAD;
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_cache_key $request_method://$host$request_uri;
    }

}
更多fastcgi配置,请参考


这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递。
例:
location / {  fastcgi_pass   localhost:9000;  fastcgi_index  index.php;   fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;  fastcgi_param  QUERY_STRING     $query_string;  fastcgi_param  REQUEST_METHOD   $request_method;  fastcgi_param  CONTENT_TYPE     $content_type;  fastcgi_param  CONTENT_LENGTH   $content_length;}
一个在缓存中的实例:
http {  fastcgi_cache_path   /path/to/cache  levels=1:2                       keys_zone=NAME:10m                       inactive=5m;   server {    location / {      fastcgi_pass    http://127.0.0.1;      fastcgi_cache   NAME;      fastcgi_cache_valid   200 302  1h;      fastcgi_cache_valid   301      1d;      fastcgi_cache_valid   any      1m;      fastcgi_cache_min_uses  1;      fastcgi_cache_use_stale error  timeout invalid_header http_500;    }  }}
0.7.48以后,缓存遵循后端服务器的Cache-Control, Expires等,0.7.66版本以后,"Cache-Control:"private"和"no-store"头同样被遵循。

·指令

fastcgi_buffer_size

语法:fastcgi_buffer_size the_size ;
默认值:fastcgi_buffer_size 4k/8k ;
使用字段:http, server, location
这个参数指定将用多大的缓冲区来读取从FastCGI服务器到来应答的第一部分。
通常来说在这个部分中包含一个小的应答头。
默认的缓冲区大小为fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

fastcgi_buffers

语法:fastcgi_buffers the_number is_size;
默认值:fastcgi_buffers 8 4k/8k;
使用字段:http, server, location
这个参数指定了从FastCGI服务器到来的应答,本地将用多少和多大的缓冲区读取,默认这个参数等于分页大小,根据环境的不同可能是4K, 8K或16K。

fastcgi_cache

语法:fastcgi_cache zone|off;
默认值:off
使用字段:http, server, location
为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方。

fastcgi_cache_key

语法:fastcgi_cache_key line
默认值:none
使用字段:http, server, location
设置缓存的关键字,如:
fastcgi_cache_key localhost:9000$request_uri;
fastcgi_cache_path

语法:fastcgi_cache_path path [levels=m:n] keys_zone=name:size [inactive=time] [max_size=size]
默认值:none
使用字段:http
clean_time参数在0.7.45版本中已经移除。
这个指令指定FastCGI缓存的路径以及其他的一些参数,所有的数据以文件的形式存储,缓存的关键字(key)和文件名为代理的url计算出的MD5值。
Level参数设置缓存目录的目录分级以及子目录的数量,例如指令如果设置为:
fastcgi_cache_path  /data/nginx/cache  levels=1:2   keys_zone=one:10m;
那么数据文件将存储为:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
缓存中的文件首先被写入一个临时文件并且随后被移动到缓存目录的最后位置,0.8.9版本之后可以将临时文件和缓存文件存储在不同的文件系统,但是需要明白这种移动并不是简单的原子重命名系统调用,而是整个文件的拷贝,所以最好在fastcgi_temp_path和fastcgi_cache_path的值中使用相同的文件系统。
另外,所有活动的关键字及数据相关信息都存储于共享内存池,这个值的名称和大小通过key_zone参数指定,inactive参数指定了内存中的数据存储时间,默认为10分钟。
max_size参数设置缓存的最大值,一个指定的cache manager进程将周期性的删除旧的缓存数据。

fastcgi_cache_methods

语法:fastcgi_cache_methods [GET HEAD POST];
默认值:fastcgi_cache_methods GET HEAD;
使用字段:main,http,location
无法禁用GET/HEAD ,即使你只是这样设置:
fastcgi_cache_methods  POST;
fastcgi_cache_min_uses

语法:fastcgi_cache_min_uses n
默认值:fastcgi_cache_min_uses 1
使用字段:http, server, location
指令指定了经过多少次请求的相同URL将被缓存。

fastcgi_cache_use_stale

语法:fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]
默认值:fastcgi_cache_use_stale off;
使用字段:http, server, location
在某些网关错误、超时的情况下,nginx都将传送过期的缓存数据。

fastcgi_cache_valid

语法:fastcgi_cache_valid [http_error_code|time]
默认值:none
使用字段:http, server, location
为指定的http返回代码指定缓存时间,例如:
fastcgi_cache_valid  200 302  10m;fastcgi_cache_valid  404      1m;
将响应状态码为200和302缓存10分钟,404缓存1分钟。
默认情况下缓存只处理200,301,302的状态。
同样也可以在指令中使用any表示任何一个。
fastcgi_cache_valid  200 302 10m;fastcgi_cache_valid  301 1h;fastcgi_cache_valid  any 1m;
fastcgi_connect_timeout

语法:fastcgi_connect_timeout time
默认值:fastcgi_connect_timeout 60
使用字段:http, server, location
指定同FastCGI服务器的连接超时时间,这个值不能超过75秒。

fastcgi_index

语法:fastcgi_index file
默认值:none
使用字段:http, server, location
如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中。例如:
fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
请求"/page.php"的参数SCRIPT_FILENAME将被设置为"/home/www/scripts/php/page.php",但是"/"为"/home/www/scripts/php/index.php"。

fastcgi_hide_header

语法:fastcgi_hide_header name
使用字段:http, server, location
默认情况下nginx不会将来自FastCGI服务器的"Status"和"X-Accel-..."头传送到客户端,这个参数也可以隐藏某些其它的头。
如果必须传递"Status"和"X-Accel-..."头,则必须使用fastcgi_pass_header强制其传送到客户端。

fastcgi_ignore_client_abort

语法:fastcgi_ignore_client_abort on|off
默认值:fastcgi_ignore_client_abort off
使用字段:http, server, location
如果当前连接请求FastCGI服务器失败,为防止其与nginx服务器断开连接,可以用这个指令。

fastcgi_ignore_headers

语法:fastcgi_ignore_headers name [name...]
使用字段:http, server, location
这个指令禁止处理一些FastCGI服务器应答的头部字段,比如可以指定像"X-Accel-Redirect", "X-Accel-Expires", "Expires"或"Cache-Control"等。

fastcgi_intercept_errors

语法:fastcgi_intercept_errors on|off
默认值:fastcgi_intercept_errors off
使用字段:http, server, location
这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。
你必须明确的在error_page中指定处理方法使这个参数有效,正如Igor所说“如果没有适当的处理方法,nginx不会拦截一个错误,这个错误不会显示自己的默认页面,这里允许通过某些方法拦截错误。

fastcgi_max_temp_file_size

语法:fastcgi_max_temp_file_size 0
默认值:?
使用字段:?
根据源代码关闭FastCGI缓冲。

fastcgi_no_cache

语法:fastcgi_no_cache variable [...]
默认值:None
使用字段:http, server, location
确定在何种情况下缓存的应答将不会使用,示例:
  fastcgi_no_cache $cookie_nocache  $arg_nocache$arg_comment;  fastcgi_no_cache $http_pragma     $http_authorization;
如果为空字符串或者等于0,表达式的值等于false,例如,在上述例子中,如果在请求中设置了cookie "nocache",缓存将被绕过。

fastcgi_next_upstream

语法:fastcgi_next_upstream error|timeout|invalid_header|http_500|http_503|http_404|off
默认值:fastcgi_next_upstream error timeout
使用字段:http, server, location
指令指定哪种情况请求将被转发到下一个FastCGI服务器:
·error — 传送中的请求或者正在读取应答头的请求在连接服务器的时候发生错误。
·timeout — 传送中的请求或者正在读取应答头的请求在连接服务器的时候超时。
·invalid_header — 服务器返回空的或者无效的应答。
·http_500 — 服务器返回500应答代码。
·http_503 — 服务器返回503应答代码。
·http_404 — 服务器返回404应答代码。
·off — 禁止请求传送到下一个FastCGI服务器。
注意传送请求在传送到下一个服务器之前可能已经将空的数据传送到了客户端,所以,如果在数据传送中有错误或者超时发生,这个指令可能无法修复一些传送错误。

fastcgi_param

语法:fastcgi_param parameter value
默认值:none
使用字段:http, server, location
指定一些传递到FastCGI服务器的参数。
可以使用字符串,变量,或者其组合,这里的设置不会继承到其他的字段,设置在当前字段会清除掉任何之前的定义。
下面是一个PHP需要使用的最少参数:
  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;  fastcgi_param  QUERY_STRING     $query_string;
PHP使用SCRIPT_FILENAME参数决定需要执行哪个脚本,QUERY_STRING包含请求中的某些参数。
如果要处理POST请求,则需要另外增加三个参数:
  fastcgi_param  REQUEST_METHOD   $request_method;  fastcgi_param  CONTENT_TYPE     $content_type;  fastcgi_param  CONTENT_LENGTH   $content_length;
如果PHP在编译时带有--enable-force-cgi-redirect,则必须传递值为200的REDIRECT_STATUS参数:
fastcgi_param  REDIRECT_STATUS  200;
fastcgi_pass

语法:fastcgi_pass fastcgi-server
默认值:none
使用字段:http, server, location
指定FastCGI服务器监听端口与地址,可以是本机或者其它
fastcgi_pass   localhost:9000;
使用Unix socket:
fastcgi_pass   unix:/tmp/fastcgi.socket;
同样可以使用一个upstream字段名称:
upstream backend  {  server   localhost:1234;} fastcgi_pass   backend;
fastcgi_pass_header

语法:fastcgi_pass_header name
默认值:none
使用字段:http, server, location

fastcgi_read_timeout

语法:fastcgi_read_timeout time
默认值:fastcgi_read_timeout 60
使用字段:http, server, location
前端FastCGI服务器的响应超时时间,如果有一些直到它们运行完才有输出的长时间运行的FastCGI进程,或者在错误日志中出现前端服务器响应超时错误,可能需要调整这个值。

fastcgi_redirect_errors

语法:fastcgi_redirect_errors on|off
指令重命名为fastcgi_intercept_errors。

fastcgi_send_timeout

语法:fastcgi_send_timeout time
默认值:fastcgi_send_timeout 60
使用字段:http, server, location
指令为上游服务器设置等待一个FastCGI进程的传送数据时间,如果有一些直到它们运行完才有输出的长时间运行的FastCGI进程,那么可以修改这个值,如果你在上有服务器的error log里面发现一些超时错误,那么可以恰当的增加这个值。
指令指定请求服务器的超时时间,指完成了2次握手的连接,而不是完整的连接,如果在这期间客户端没有进行数据传递,那么服务器将关闭这个连接。

fastcgi_split_path_info

语法:fastcgi_split_path_info regex
使用字段:location
可用版本:0.7.31以上,示例:
location ~ ^(.+\.php)(.*)$ {...fastcgi_split_path_info ^(.+\.php)(.*)$;fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;...}
请求"/show.php/article/0001"的参数SCRIPT_FILENAME将设置为"/path/to/php/show.php",参数PATH_INFO为"/article/0001"。

fastcgi_store

语法:fastcgi_store [on | off | path]
默认值:fastcgi_store off
使用字段:http, server, location
制定了存储前端文件的路径,参数on指定了将使用root和alias指令相同的路径,off禁止存储,此外,参数中可以使用变量使路径名更明确:
fastcgi_store   /data/www$original_uri;
应答中的"Last-Modified"头将设置文件的最后修改时间,为了使这些文件更加安全,可以将其在一个目录中存为临时文件,使用fastcgi_temp_path指令。
这个指令可以用在为那些不是经常改变的后端动态输出创建本地拷贝的过程中。如:
location /images/ {  root                 /data/www;  error_page           404 = /fetch$uri;} location /fetch {  internal;   fastcgi_pass           fastcgi://backend;  fastcgi_store          on;  fastcgi_store_access   user:rw  group:rw  all:r;  fastcgi_temp_path      /data/temp;   alias                  /data/www;}
fastcgi_store并不是缓存,某些需求下它更像是一个镜像。

fastcgi_store_access

语法:fastcgi_store_access users:permissions [users:permission ...]
默认值:fastcgi_store_access user:rw
使用字段:http, server, location
这个参数指定创建文件或目录的权限,例如:
fastcgi_store_access  user:rw  group:rw  all:r;
如果要指定一个组的人的相关权限,可以不写用户,如:
fastcgi_store_access  group:rw  all:r;
fastcgi_temp_path

语法:fastcgi_temp_path path [level1 [level2 [level3]]]
默认值:fastcgi_temp_path fastcgi_temp
使用字段:http, server, location
指令指定存储从别的服务器传送来的数据临时文件路径,同样可以指定三级目录已经哈希存储,level的值指定为哈希设置多少标记,例如,在下列配置中:
fastcgi_temp_path  /spool/nginx/fastcgi_temp 1 2;
临时文件类似如下:
/spool/nginx/fastcgi_temp/7/45/00000123457
·传送到FastCGI服务器的相关参数

请求头是以参数的形式传送到FastCGI服务器,以具体应用和脚本运行在FastCGI服务器上,这些参数通常以环境变量的形式取得,例如,"User-agent"头以HTTP_USER_AGENT参数传递,除此之外还有一些其他的http头,都可以用fastcgi_param指令自由传递。

·变量

$fastcgi_script_name

这个变量等于一个以斜线结尾的请求URI加上fastcgi_index给定的参数。可以用这个变量代替SCRIPT_FILENAME 和PATH_TRANSLATED,以确定php脚本的名称。
如下例,请求"/info/":
 fastcgi_index  index.php;  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
SCRIPT_FILENAME等于"/home/www/scripts/php/info/index.php"。

本文来源:http://www.bbyears.com/caozuoxitong/80480.html