nginx 配置反向代理及负载均衡
nginx 配置反向代理及负载均衡
配置文件详解
1 # This number should be, at maximum, the number of CPU cores on your system. 2 # (since nginx doesn't benefit from more than one worker per CPU.) 3 # 这里的数值不能超过 CPU 的总核数,因为在单个核上部署超过 1 个 Nginx 服务进程并不起到提高性能的作用。 4 worker_processes 24; 5 6 # Number of file descriptors used for Nginx. This is set in the OS with 'ulimit -n 200000' 7 # or using /etc/security/limits.conf 8 # Nginx 最大可用文件描述符数量,同时需要配置操作系统的 "ulimit -n 200000",或者在 /etc/security/limits.conf 中配置。 9 worker_rlimit_nofile 200000; 10 11 # only log critical errors 12 # 只记录 critical 级别的错误日志 13 error_log /var/log/nginx/error.log crit 14 15 # Determines how many clients will be served by each worker process. 16 # (Max clients = worker_connections * worker_processes) 17 # "Max clients" is also limited by the number of socket connections available on the system (~64k) 18 # 配置单个 Nginx 单个进程可服务的客户端数量,(最大值客户端数 = 单进程连接数 * 进程数 ) 19 # 最大客户端数同时也受操作系统 socket 连接数的影响(最大 64K ) 20 worker_connections 4000; 21 22 # essential for linux, optmized to serve many clients with each thread 23 # Linux 关键配置,允许单个线程处理多个客户端请求。 24 use epoll; 25 26 # Accept as many connections as possible, after nginx gets notification about a new connection. 27 # May flood worker_connections, if that option is set too low. 28 # 允许尽可能地处理更多的连接数,如果 worker_connections 配置太低,会产生大量的无效连接请求。 29 multi_accept on; 30 31 # Caches information about open FDs, freqently accessed files. 32 # Changing this setting, in my environment, brought performance up from 560k req/sec, to 904k req/sec. 33 # I recommend using some varient of these options, though not the specific values listed below. 34 # 缓存高频操作文件的FDs(文件描述符/文件句柄) 35 # 在我的设备环境中,通过修改以下配置,性能从 560k 请求/秒 提升到 904k 请求/秒。 36 # 我建议你对以下配置尝试不同的组合,而不是直接使用这几个数据。 37 open_file_cache max=200000 inactive=20s; 38 open_file_cache_valid 30s; 39 open_file_cache_min_uses 2; 40 open_file_cache_errors on; 41 42 # Buffer log writes to speed up IO, or disable them altogether 43 # 将日志写入高速 IO 存储设备,或者直接关闭日志。 44 # access_log /var/log/nginx/access.log main buffer=16k; 45 access_log off; 46 47 # Sendfile copies data between one FD and other from within the kernel. 48 # More efficient than read() + write(), since the requires transferring data to and from the user space. 49 # 开启 sendfile 选项,使用内核的 FD 文件传输功能,这个比在用户态用 read() + write() 的方式更加高效。 50 sendfile on; 51 52 # Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet, 53 # instead of using partial frames. This is useful for prepending headers before calling sendfile, 54 # or for throughput optimization. 55 # 打开 tcp_nopush 选项,Nginux 允许将 HTTP 应答首部与数据内容在同一个报文中发出。 56 # 这个选项使服务器在 sendfile 时可以提前准备 HTTP 首部,能够达到优化吞吐的效果。 57 tcp_nopush on; 58 59 # don't buffer data-sends (disable Nagle algorithm). Good for sending frequent small bursts of data in real time. 60 # 不要缓存 data-sends (关闭 Nagle 算法),这个能够提高高频发送小数据报文的实时性。 61 tcp_nodelay on; 62 63 # Timeout for keep-alive connections. Server will close connections after this time. 64 # 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接。 65 keepalive_timeout 30; 66 67 # Number of requests a client can make over the keep-alive connection. This is set high for testing. 68 # 单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。 69 keepalive_requests 100000; 70 71 # allow the server to close the connection after a client stops responding. Frees up socket-associated memory. 72 # 允许服务器在客户端停止发送应答之后关闭连接,以便释放连接相应的 socket 内存开销。 73 reset_timedout_connection on; 74 75 # send the client a "request timed out" if the body is not loaded by this time. Default 60. 76 # 配置客户端数据请求超时时间,默认是 60 秒。 77 client_body_timeout 10; 78 79 # If the client stops reading data, free up the stale client connection after this much time. Default 60. 80 # 客户端数据读超时配置,客户端停止读取数据,超时时间后断开相应连接,默认是 60 秒。 81 send_timeout 2; 82 83 # Compression. Reduces the amount of data that needs to be transferred over the network 84 # 压缩参数配置,减少在网络上所传输的数据量。 85 gzip on; 86 gzip_min_length 10240; 87 gzip_proxied expired no-cache no-store private auth; 88 gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; 89 gzip_disable "MSIE [1-6].";