在http的功能里添加log_format模块,内容如下:

log_format main escape=json '{ "@timestamp": "$time_iso8601", '

'"remote_addr": "$remote_addr",'
'"request_time": "$request_time",'
'"upstream_response_time": "$upstream_response_time",'
'"status": $status,'
'"x_forwarded": "$http_x_forwarded_for",'
'"http_referer": "$http_referer",'
'"request": "$request",'
'"upstream_addr": "$upstream_addr",'
'"body_bytes_sent":$body_bytes_sent,'
'"request_body":$request_body,'
'"http_user_agent": "$http_user_agent" }';

access_log /usr/local/openresty/nginx/logs/access.log main;

 

########################################以下是一个完整的nginx.conf文件的配置与说明#########################

user nginx nginx; #启动用户
worker_processes 4; #nginx的进程数,建议和cpu核数一致


#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log logs/error.log warn; #全局LOG定义

pid logs/nginx.pid; # 进程文件ID

worker_rlimit_nofile 65535; #nginx进程打开的文件数
#工作模式与连接数配置
events {
use epoll; # epoll是高版本内核优化后的网络I/O模型
worker_connections 65535; #单个进程最大连接数
multi_accept on; #打开快速接收新连接
}

#http服务设置
http {
include mime.types;#文件扩展名与类型映射表
default_type application/octet-stream;#默认文件类型

charset utf-8;#默认编码
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 32k; #上传文件大小限制
large_client_header_buffers 4 32k; #设定请求缓存数
client_max_body_size 32m; #设定请求缓存大小
#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"';

#access_log logs/access.log main;
#resolver 8.8.8.8;
sendfile on; #开启高效文件传输
tcp_nopush on; #防止网络阻塞
tcp_nodelay on;

#keepalive_timeout 0;
keepalive_timeout 30; #长连接超时时间,默认单位秒

####解决跨域问题###########
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With,Content-Type,If-Modified-Since;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

#以下是FastCGI 的相关参数,主要作用减少资源占用优化网站性能提高访问速度
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 64k;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
open_file_cache max=10240 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;


#gizp优化配置,加速传速
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable msie6;

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;


log_format main escape=json '{ "@timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr",'
'"request_time": "$request_time",'
'"upstream_response_time": "$upstream_response_time",'
'"status": $status,'
'"x_forwarded": "$http_x_forwarded_for",'
'"http_referer": "$http_referer",'
'"request": "$request",'
'"upstream_addr": "$upstream_addr",'
'"body_bytes_sent":$body_bytes_sent,'
'"request_body":$request_body,'
'"http_user_agent": "$http_user_agent" }';
access_log /usr/local/openresty/nginx/logs/access.log main;
error_log /usr/local/openresty/nginx/logs/error.log;
# 虚拟主机配置我这定义到了/usr/local/openresty/nginx/conf/vhosts目录下
include vhosts/*.conf;

}

#############################################################################################################################################

以下的文件摘自:原文链接:https://blog.csdn.net/dongl890426/article/details/83863763

worker_processes

工作进程数,操作系统启动多少个工作进程运行Nginx。注意是工作进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工作进程worker process。worker process。主进程负责监控端口,协调工作进程的工作状态,分配工作任务,工作进程负责进行任务处理。一般这个参数要和操作系统的CPU内核数成倍,(当值为 auto 时,nginx会自己决定工作进程数量)

下面是nginx官网原话:

Syntax: worker_processes number | auto;
Default: worker_processes 1;

Context: main Defines the number of worker processes. The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it). The auto parameter is supported starting from versions 1.3.8 and 1.2.5.

网上寻找的一些经验总结:

一般设置为1就足够了,可以把连接数设置的很大,比如65535。
如果有SSL,gzip等比较消耗CPU资源的工作,而且CPU是多核的话,可以把值设置为和CPU核数一样。
如果有很多静态文件而且它们的总大小超过内存大小,那么可以增加工作进程来充分利用I/O带宽。
如果要开多个工作进程,最好是CPU核数的1~2倍。正常情况下,不要太多,因为工作进程太多会影响nginx主进程调度。
worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。
work_cpu_affinity

Nginx默认没有开启利用多核CPU,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU。CPU是任务处理,计算最关键的资源,CPU核越多,性能就越好(worker_processes配合使用,当值为 auto 时,nginx会自己决定CPU核数使用)。

几个简单的配置用例:

worker_processes     2;
worker_cpu_affinity 01 10;

01表示启用第一个CPU内核,10表示启用第二个CPU内核。
worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。

 

worker_processes     4;
worker_cpu_affinity 01 10 01 10;

开启了四个进程,它们分别对应着开启2个CPU内核。

 

worker_processes     8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推。

worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

 

worker_rlimit_nofile

nginx官网原文:

Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.

为nginx工作进程改变打开最多文件描述符数目的限制。用来在不重启主进程的情况下增加限制。

如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件。

 

events模块

nginx官网原文:

Provides the configuration file context in which the directives that affect connection processing are specified.

events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

1、use method;

Specifies the connection processing method to use. There is normally no need to specify it explicitly, because nginx will by default use the most efficient method.

eg: use epoll;

使用epoll的I/O 模型(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)。

如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型
    A)标准事件模型
    Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
    B)高效事件模型
    Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
    Epoll:使用于Linux内核2.6版本及以后的系统。
    /dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    Eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁

查看linux版本号可以使用 cat /proc/version命令

2、worker_connections

原文:

Sets the maximum number of simultaneous connections that can be opened by a worker process.

It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.

设置工作进程的最大连接数量

按反向代理模式下最大连接数的理论计算公式:

   最大连接数 = worker_processes * worker_connections/4

3、multi_accept

原文:

If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time.

The directive is ignored if kqueue connection processing method is used, because it reports the number of new connections waiting to be accepted.
如果multi_accept被禁止了,nginx一个工作进程只能同时接受一个新的连接。否则,一个工作进程可以同时接受所有的新连接。
如果nginx使用kqueue连接方法,那么这条指令会被忽略,因为这个方法会报告在等待被接受的新连接的数量。

 

http模块

1、server_names_hash_bucket_size

Sets the bucket size for the server names hash tables. The default value depends on the size of the processor’s cache line. The details of setting up hash tables are provided in a separate document.

服务器名字的hash表大小,默认(32|64|128)

2、client_header_buffer_size

Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.

用于指定来自客户端请求头headerbuffer大小,对于大多数请求,1KB(默认)的缓冲区大小已经足够,如果自定义了消息头或有更大的cookie,可以增加缓冲区大小。这里设置为32KB

3、large_client_header_buffers

Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.

用来指定客户端请求中较大的消息头的缓存最大数量和大小,

eg: large_client_header_buffers 4 128k;   “4”为个数,“128”为大小,最大缓存为4个128KB

4、client_max_body_size

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.

设置客户端请求的最大文件字节数

5、client_header_timeout

Defines a timeout for reading client request header. If a client does not transmit the entire header within this time, the request is terminated with the 408 (Request Time-out) error.

(默认60s)用于设置客户端请求读取header超时时间,如果超过这个时间,客户端没有发送任何数据,nginx将返回“request time out (408)”错误。

6、client_body_timeout

Defines a timeout for reading client request body. The timeout is set only for a period between two successive read operations, not for the transmission of the whole request body. If a client does not transmit anything within this time, the request is terminated with the 408 (Request Time-out) error.

用于设置客户端请求主体读取超时时间,默认值为60s,如果超过这个时间,客户端还没有发送任何数据,nginx将返回“Request time out(408)。

7、send_timeout

Sets a timeout for transmitting a response to the client. The timeout is set only between two successive write operations, not for the transmission of the whole response. If the client does not receive anything within this time, the connection is closed.

服务器响应超时设置。默认60s。

8、sendfile

Enables or disables the use of sendfile().

Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():

location /video/ {
sendfile on;
tcp_nopush on;
aio on;
}
In this configuration, sendfile() is called with the SF_NODISKIO flag which causes it not to block on disk I/O, but, instead, report back that the data are not in memory. nginx then initiates an asynchronous data load by reading one byte. On the first read, the FreeBSD kernel loads the first 128K bytes of a file into memory, although next reads will only load data in 16K chunks. This can be changed using the read_ahead directive.

Before version 1.7.11, pre-loading could be enabled with aio sendfile;.
开启高效文件传输模式,将tcp_nopush和tcp_nodely两个指令设置为on,用于防止网络阻塞。

sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。

9、tcp_nopush

Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows

sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;
sending a file in full packets.
防止网络阻塞,允许把httpresponse header和文件的开始放在一个文件里发布,作用是减少网络报文段的数量。

10、tcp_nodelay

Enables or disables the use of the TCP_NODELAY option. The option is enabled when a connection is transitioned into the keep-alive state. Additionally, it is enabled on SSL connections, for unbuffered proxying, and for WebSocket proxying.

内核会等待将更多的字节组成一个数据包,从而提高I/O性能。

11、keepalive_timeout

The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.

The “Keep-Alive: timeout=time” header field is recognized by Mozilla and Konqueror. MSIE closes keep-alive connections by itself in about 60 seconds.

keepalive_timeout 来指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。

12、server_tokens

Enables or disables emitting nginx version on error pages and in the “Server” response header field.


The build parameter (1.11.10) enables emitting a build name along with nginx version.

Additionally, as part of our commercial subscription, starting from version 1.9.13 the signature on error pages and the “Server” response header field value can be set explicitly using the string with variables. An empty string disables the emission of the “Server” field.

隐藏nginx相关版本信息。

13、limit_conn_zone

Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state includes the current number of connections. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.

Prior to version 1.7.6, a key could contain exactly one variable.
Usage example:

limit_conn_zone $binary_remote_addr zone=addr:10m;
Here, a client IP address serves as a key. Note that instead of $remote_addr, the $binary_remote_addr variable is used here. The $remote_addr variable’s size can vary from 7 to 15 bytes. The stored state occupies either 32 or 64 bytes of memory on 32-bit platforms and always 64 bytes on 64-bit platforms. The $binary_remote_addr variable’s size is always 4 bytes for IPv4 addresses or 16 bytes for IPv6 addresses. The stored state always occupies 32 or 64 bytes on 32-bit platforms and 64 bytes on 64-bit platforms. One megabyte zone can keep about 32 thousand 32-byte states or about 16 thousand 64-byte states. If the zone storage is exhausted, the server will return the error to all further requests.

 这个模块的目的主要是对连接进行限制,如果是这样的话,那么我们就需要对连接的状态进行进行存储。那么是用什么来进行存储呢?存储肯定是需要空间的。这个limit_conn_zone就是开辟了这样的一个空间。这个空间里,我们需要对那个作为key要进行说明,比如说,以客户端ip作为Key。那么这样的话,就以http_addr这个变量作为Key。如果要以别的内置变量作为key来作为key来进行配置的时候,那么同样,可以写到key配置的这一项中。那么后面的zone=name:size,就是限制的空间,name指的是空间的名字,size表示的是空间的大小。在真正实现限制的时候就会调用这个空间。在limit_conn zone number中就可以调用这个空间。在limit_conn zone number中,是要结合先定义好的zone 才能使用limit_conn.这个zone指的是我们需要调用的zone的name.number指的是并发的限制个数。

该指令描述会话状态存储区域。键的状态中保存了当前连接数,键的值可以是特定变量的任何非空值(空值将不会被考虑)。$variable定义键,zone=name定义区域名称,后面的limit_conn指令会用到的。size定义各个键共享内存空间大小。

注释:客户端的IP地址作为键。注意,这里使用的是$binary_remote_addr变量,而不是$remote_addr变量。
$remote_addr变量的长度为7字节到15字节,而存储状态在32位平台中占用32字节或64字节,在64位平台中占用64字节。
$binary_remote_addr变量的长度是固定的4字节,存储状态在32位平台中占用32字节或64字节,在64位平台中占用64字节。
1M共享空间可以保存3.2万个32位的状态,1.6万个64位的状态。
如果共享内存空间被耗尽,服务器将会对后续所有的请求返回 503 (Service Temporarily Unavailable) 错误。


14、limit_conn

Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the error in reply to a request. For example, the directives

limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
location /download/ {
limit_conn addr 1;
}
allow only one connection per an IP address at a time.

In HTTP/2 and SPDY, each concurrent request is considered a separate connection.
There could be several limit_conn directives. For example, the following configuration will limit the number of connections to the server per a client IP and, at the same time, the total number of connections to the virtual server:

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;

server {
...
limit_conn perip 10;
limit_conn perserver 100;
}
These directives are inherited from the previous level if and only if there are no limit_conn directives on the current level.

指定每个给定键值的最大同时连接数,当超过这个数字时被返回503 (Service Temporarily Unavailable)错误。

15、limit_conn_log_level

当达到最大限制连接数后,记录日志的等级。

16、gzip

开启gzip压缩服务。

17、gzip_min_length

设置最小的压缩值,单位为bytes。超过设置的min_length的值会进行压缩,小于的不压缩。

18、gzip_comp_level

压缩等级设置(1~9),1是最小压缩,速度也是最快的,9刚好相反,最大的压缩,速度是最慢的,消耗的cpu资源也多。

19、gzip_buffers

 

设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流,它可以避免nginx频繁向系统申请压缩空间。  例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。 如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。

20、gzip_types

需要进行gzip压缩的Content-Type的Header的类型。建议js、text、css、xml、json都要进行压缩; #图片就没必要了,gif、jpge文件已经压缩得很好了,就算再压,效果也不好,而且还耗费cpu。  javascript有多种形式。其中的值可以在 mime.types 文件中找到。

21、gzip_http_version  (1.0|1.1)

Sets the minimum HTTP version of a request required to compress a response.

# gzip压缩基于的http协议版本,默认就是HTTP 1.1

22、gzip_proxied

# 默认值:off

# Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。

# off - 关闭所有的代理结果数据的压缩

# expired - 启用压缩,如果header头中包含 "Expires" 头信息

# no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息

# no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息

# private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息

# no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息

# no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息

# auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息

# any - 无条件启用压缩

23、gzip_vary

是否在http header中添加Vary: Accept-Encoding,建议开启,和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩。

24、gzip_disable

一般设置 这个  gzip_disable "MSIE [1-6]\.";

禁用IE6的gzip压缩。

25、proxy_temp_path

缓存临时文件路径,存储承载从代理服务器接收到的数据的临时文件定义目录。指定目录下支持3级子目录结构。

26、proxy_cache_path

proxy_cache_path 缓存文件路径

levels 设置缓存文件目录层次;levels=1:2 表示两级目录

keys_zone 设置缓存名字和共享内存大小

inactive 在指定时间内没人访问则被删除
max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。

比如:proxy_cache_path /home/tengine/proxy_cache levels=1:2 keys_zone=cache_ones:500m inactive=1d max_size=30g

设置web缓存区名称cache_ones,内存缓存空间大小为500mb,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB

27、client_body_buffer_size

请求主体的缓冲区大小。 如果主体超过缓冲区大小,则完整主体或其一部分将写入临时文件。 如果NGINX配置为使用文件而不是内存缓冲区,则该指令会被忽略。 默认情况下,该指令为32位系统设置一个8k缓冲区,为64位系统设置一个16k缓冲区。

一般我们设置成 client_body_buffer_size 1024k;

28、proxy_connect_timeout (default 60s)

后端服务器连接的超时时间_发起握手等候响应超时时间。

29、proxy_read_timeout (default 60s)

连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)。

30、proxy_send_timeout (default 60s)

后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据。

31、proxy_buffer_size

指定后端 response 的 buffer 的大小。它是来自后端 response 的一部分,它包含 Headers,从 response 分离出来。它仅用于限定 headers 的 buffer 区,所以它的值比 proxy_buffers 更低。

proxy_buffer_size 有一点特殊在于,无论 proxy_buffering 是否开启,proxy_buffer_size 都会起作用。

后端服务器的相应头会放到proxy_buffer_size当中,这个大小默认等于proxy_buffers当中的设置单个缓冲区的大小。 proxy_buffer_size只是响应头的缓冲区,没有必要也跟着设置太大。 proxy_buffer_size最好单独设置,一般设置个4k就够了。

32、proxy_buffers

配置接受一次响应的buffer个数和你每个buffer的大小

proxy_buffers的缓冲区大小一般会设置的比较大,以应付大网页。 proxy_buffers当中单个缓冲区的大小是由系统的内存页面大小决定的,Linux系统中一般为4k。 proxy_buffers由缓冲区数量和缓冲区大小组成的。总的大小为number*size。

若某些请求的响应过大,则超过_buffers的部分将被缓冲到硬盘(缓冲目录由_temp_path指令指定), 当然这将会使读取响应的速度减慢, 影响用户体验. 可以使用proxy_max_temp_file_size指令关闭磁盘缓冲.

eg: proxy_buffer 4 4k;

number代表数量,size代表大小,一般size设置为内存页的大小4k或者8k,那么一次响应的Proxy Buffer总大小为4 * 4k = 16k。

33、proxy_busy_buffers_size

proxy_busy_buffers_size不是独立的空间,他是proxy_buffers和proxy_buffer_size的一部分。nginx会在没有完全读完后端响应的时候就开始向客户端传送数据,所以它会划出一部分缓冲区来专门向客户端传送数据(这部分的大小是由proxy_busy_buffers_size来控制的,建议为proxy_buffers中单个缓冲区大小的2倍),然后它继续从后端取数据,缓冲区满了之后就写到磁盘的临时文件中。

34、proxy_temp_file_write_size

是一次访问能写入的临时文件的大小,默认是proxy_buffer_size和proxy_buffers中设置的缓冲区大小的2倍,Linux下一般是8k。

35、proxy_next_upstream

proxy_next_upstream项定义了什么情况下进行重试

语法: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |http_404 | off ...; 默认值: proxy_next_upstream error timeout; 上下文: http, server, location
error # 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现错误 timeout # 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现超时 invalid_header # 后端服务器返回空响应或者非法响应头 http_500 # 后端服务器返回的响应状态码为500 http_502 # 后端服务器返回的响应状态码为502 http_503 # 后端服务器返回的响应状态码为503 http_504 # 后端服务器返回的响应状态码为504 http_404 # 后端服务器返回的响应状态码为404 off # 停止将请求发送给下一台后端服务器
 更多的参考官网文档:http://nginx.org/en/docs/

 

 

nginx.conf 优化之后的 代理配置:


worker_processes  auto;   # 工作进程数,为CPU的核心数或者两倍
worker_cpu_affinity auto;


error_log   /home/tengine/logs/error.log  notice; # debug|info|notice|warn|error|crit
pid        /home/tengine/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;


events {
    use epoll;                         
    worker_connections  65535;
    multi_accept on;
}

http {
    include       mime.types;             #设定mime类型,类型由mime.type文件定义
    default_type  application/octet-stream;

    charset  utf-8;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;  
    
    #lua依赖路径  
    lua_package_path  "/usr/local/openresty/lualib/?.lua;;";  
    lua_package_cpath  "/usr/local/openresty/lualib/?.so;;";

    #初始化脚本  
    #init_by_lua_file "/usr/local/openresty/nginx/lua/util.lua";       
    

    lua_shared_dict healthcheck 1m;

    lua_socket_log_errors off;

   
    #设定请求缓冲
    server_names_hash_bucket_size 256;    #增加,原为128
    client_header_buffer_size 256k;       #增加,原为32k
    large_client_header_buffers 4 256k;   #增加,原为32k
 
    #size limits
    client_max_body_size          2050m;    #允许客户端请求的最大的单个文件字节数
    client_header_timeout         3m;
    client_body_timeout           3m;
    send_timeout                  3m;

    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             60;
    tcp_nodelay                   on;
    server_tokens                 on;    #不显示nginx版本信息
 
    #limit_conn_zone $binary_remote_addr zone=perip:10m; #添加limit_zone,限制同一IP并发数
    limit_conn_zone $binary_remote_addr zone=TotalConnLimitZone:10m;
    limit_conn  TotalConnLimitZone  500;
    limit_conn_log_level notice;
    #fastcgi_intercept_errors on;         #开启错误页面跳转
     

    gzip on;
    gzip_min_length   1k;  #设置最小的压缩值,单位为bytes.超过设置的min_length的值会进行压缩,小于的不压缩.
    gzip_comp_level   3;   #压缩等级设置,1-9,1是最小压缩,速度也是最快的;9刚好相反,最大的压缩,速度是最慢的,消耗的CPU资源也多
    gzip_buffers      16 64k;   #设置系统的缓存大小,以存储GZIP压缩结果的数据流,它可以避免nginx频烦向系统申请压缩空间大小
    gzip_types text/plain application/x-javascript text/css text/javascript;
 
   gzip_http_version 1.1;      #识别http的协议版本(1.0/1.1)
   gzip_proxied      any;      #设置使用代理时是否进行压缩,默认是off的
   gzip_vary         on;       #和http头有关系,加个vary头,代理判断是否需要压缩
   gzip_disable "MSIE [1-6]."; #禁用IE6的gzip压缩
  

  #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
  proxy_temp_path   /home/tengine/proxy_temp;

   #设置Web缓存区名称为cache_one,内存缓存空间大小为500MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
   proxy_cache_path  /home/tengine/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;

   client_body_buffer_size  1024k;     #原为512k
   proxy_connect_timeout    50;       #代理连接超时
   proxy_read_timeout       600;      #代理发送超时
   proxy_send_timeout       600;      #代理接收超时
   proxy_buffer_size        128k;     #代理缓冲大小,原为32k
   proxy_buffers           16 256k;   #代理缓冲,原为4 64k
   proxy_busy_buffers_size 512k;      #高负荷下缓冲大小,原为128k
   proxy_temp_file_write_size 1024m;  #proxy缓存临时文件的大小原为128k
   proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;

 
}



 

posted on 2019-03-19 14:05  爱技术努力学技术  阅读(2091)  评论(0编辑  收藏  举报