Nginx
 is
an open-source Web Server. It is a high-performance HTTP server that uses very
low server resources, is reliable and integrates beautifully with Linux. In this
article, I’ll talk about optimizing your nginx server for maximum
performance.

Install
Nginx with a minimal number of modules

Run
Nginx with only the required modules. This reduces the memory footprint and
hence the server performance. Example configuration

./configure --prefix=/webserver/nginx --without-mail_pop3_module --without-mail_imap_module  --without-mail_smtp_module --with-http_ssl_module  --with-http_stub_status_module  --with-http_gzip_static_module

worker_processes

A
worker process is a single-threaded process. If Nginx is doing CPU-intensive
work such as SSL or gzipping and you have 2 or more CPUs/cores, then you may set
worker_processes to be equal to the number of CPUs or cores. Example, i’m
running nginx on server has CPU is X3340 (4 cores) then i set worker_processes
= 4
. If you are serving a lot of static files and the total size of the
files is bigger than the available memory, then you may increase
worker_processes to fully utilize disk bandwidth.

worker_connections

This
sets the number of connections that each worker can handle. You can determine
the value by using ulimit
-n
command which output is something like 1024, then your worker
connections would need to be set to 1024 or less but 1024 is a good default
setting.
You
can work out the maximum clients value by multiplying this and the
worker_processes settings

max_clients = worker_processes * worker_connections

Buffers

One
of the most important things you need to tweak is the buffer sizes you allow
Nginx to use. If the buffer sizes are set too low Nginx will have to store the
responses from upstreams in a temporary file which causes both write and read
IO, the more traffic you get the more of a problem this becomes. Edit and set
the buffer size limitations for all clients as follows:

client_body_buffer_size 8K;
client_header_buffer_size 1k;
client_max_body_size 2m;
large_client_header_buffers 2 1k;

Where,

1.
client_body_buffer_size:
 The directive specifies the client request
body buffer size. If the request body is more than the buffer, then the entire
request body or some part is written in a temporary file.
2.
client_header_buffer_size:
 Directive sets the headerbuffer size for the
request header from client. For the overwhelming majority of requests it is
completely sufficient a buffer size of 1K.
3.
client_max_body_size:
 Directive assigns the maximum accepted body size
of client request, indicated by the line Content-Length in the header of
request. If size is greater the given one, then the client gets the error
“Request Entity Too Large” (413).
4.
large_client_header_buffers:
 Directive assigns the maximum number and
size of buffers for large headers to read from client request. The request line
can not be bigger than the size of one buffer, if the client send a bigger
header nginx returns error “Request URI too large” (414). The longest header
line of request also must be not more than the size of one buffer, otherwise the
client get the error “Bad request” (400).

You
also need to control timeouts to improve server performance and cut clients.
Edit it as follows:

client_body_timeout   10;
client_header_timeout 10;
keepalive_timeout     15;
send_timeout          10;

Where,

1.
client_body_timeout:
 Directive sets the read timeout for the request
body from client. The timeout is set only if a body is not get in one readstep.
If after this time the client send nothing, nginx returns error “Request time
out” (408).
2.
client_header_timeout:
 Directive assigns timeout with reading of the
title of the request of client. The timeout is set only if a header is not get
in one readstep. If after this time the client send nothing, nginx returns error
“Request time out” (408).
3.
keepalive_timeout:
 The first parameter assigns the timeout for
keep-alive connections with the client. The server will close connections after
this time. The optional second parameter assigns the time value in the header
Keep-Alive: timeout=time of the response. This header can convince some browsers
to close the connection, so that the server does not have to. Without this
parameter, nginx does not send a Keep-Alive header (though this is not what
makes a connection “keep-alive”).
4.
send_timeout:
 Directive assigns response timeout to client. Timeout is
established not on entire transfer of answer, but only between two operations of
reading, if after this time client will take nothing, then nginx is shutting
down the connection.

Access
Logs

By
default nginx will write every request to a file on disk for logging purposes.
If you don’t use access logs for anything you can simply just turn it off and
avoid the disk writes.

access_log off;

Gzip

Gzip
compress content before it is delivered to the client. It’s a simple and
effective way to speed up your site.

gzip             on;
gzip_comp_level  2;
gzip_min_length  1000;
gzip_proxied     expired no-cache no-store private auth;
gzip_types       text/plain application/xml;
gzip_disable     "MSIE [1-6]\.";

Caching
static files

80%
of the end-user response time is spent on the front-end. Most of this time is
tied up in downloading all the components in the page: images, stylesheets,
scripts, Flash, etc. Reducing the number of components in turn reduces the
number of HTTP requests required to render the page. Example, i’m using the
following configuration to cache static files on nginx

location ~* "\.(js|ico|gif|jpg|png|css|html|htm|swf|htc|xml|bmp|cur)$" {
root /home/site/public_html;
       add_header Pragma "public";
       add_header Cache-Control "public";
expires     3M;
       access_log  off;
log_not_found off;
}

KeepAlive

KeepAlive allows
multiple requests to be sent over the same TCP/IP connection. Turning it on can
greatly improve the speed of your server, particularly when you have static
pages and are serving quite a bit of images from your server. An example would
be a catalogue site with screenshots. From my experience it is best to keep it
On.
keepalive_timeout in
nginx has default is very high. I recommend change it to 10-20.

keepalive_timeout 15

Make
best nginx configuration

To
make best nginx configuration, you should visit to http://wiki.nginx.org/Pitfalls

nginx调优的更多相关文章

  1. nginx调优(二)

    nginx调优(一) (1).Fastcgi调优 FastCGI全称快速通用网关接口(FastCommonGatewayInterface),可以认为FastCGI是静态服务和动态服务的一个接口.Fa ...

  2. nginx调优配置

    nginx调优配置 user www www; #工作进程:数目.根据硬件调整,通常等于CPU数量或者2倍于CPU. worker_processes 8; worker_cpu_affinity 0 ...

  3. 记一次单机Nginx调优,效果立竿见影

    一.物理环境 1.系统是Centos 8,系统配置 2核4G,8M带宽,一台很轻的应用服务器. 2.站点部署情况.但站点部署两个实例,占用两个端口,使用nginx 负载转发到这两个web站点.  二. ...

  4. nginx调优(一)

    (1).隐藏nginx版本号 隐藏版本号可以有效避免黑客根据nginx版本信息,查找对应漏洞进行攻击. 下载nginx源码包(http://nginx.org/en/download.html)并上传 ...

  5. nginx调优buffer参数设置

    内容来自 https://blog.tanteng.me/2016/03/nginx-buffer-params/.有空再详细了解 Nginx性能调优之buffer参数设置 打开Nginx的error ...

  6. nginx 调优

    般来说nginx配置文件中对优化比较有作用的为以下几项:worker_processes 8;1 nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 0 ...

  7. Nginx 调优经验记录

    1.2017年连续爆出5.x版本xshell安全问题和intel的cpu设计漏洞 ,此时我就注意到尽量少暴露自己线上使用的工具以及版本.例如:mysql版本,以及缓存层策略,服务器版本等,以下为 隐藏 ...

  8. Linux学习系列之Nginx调优实战

    Nginx配置文件性能微调 全局的配置 user www-data; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofi ...

  9. Nginx调优实战

    Nginx配置文件性能微调 全局的配置 user www-data; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofi ...

随机推荐

  1. !!!四种常见的 POST 提交数据方式(含application/json)

    HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...

  2. 测试服务API的_苏飞开发助手_使用说明

    1 工具说明_json对象字符串拼接 2 工具说明_纯字符串拼接

  3. Android开发方向

    运行Android平台的硬件只是手机.平台电脑等便携式设备,这些设备的计算能力.数据存储能力都是有限的, 不太可能在Android平台上部署大型企业级应用,因此Android应用可能以纯粹客户端应用的 ...

  4. 2016.6.20 计算机网络复习要点第三章之CSMA/CD协议

    1.最早的以太网是将许多计算机都连接到一根总线上: (1)总线的特点是:当一台计算机发送数据时,总线上的所有计算机都检测到这个数据,这种就是广播通信方式: (2)为了实现在总线上的一对一通信,可以使每 ...

  5. http://my.oschina.net/chinacion/blog/647641

    http://my.oschina.net/chinacion/blog/647641

  6. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  7. ZOJ2332 Gems(最大流)

    题目大概说,alsomagic有宝石,宝石有颜色和形状两个属性:他有一种法力可以将某些颜色形状的宝石转化成另一种颜色形状的宝石:他的女朋友对各个颜色都有一定的容忍数量,而他自己也对各个形状都有一定的容 ...

  8. BZOJ3103 : Palindromic Equivalence

    用Manacher可以推出O(n)对相等和不等关系. 将相等的用并查集维护,不等的连边. 然后从1到n,如果该等价类还没被考虑过,则ans*=26-与它不等的考虑过的等价类个数. #include&l ...

  9. storm源码之一个class解决nimbus单点问题【转】

    本文导读: storm nimbus 单节点问题概述 storm与解决nimbus单点相关的概念 nimbus目前无法做到多节点的原因 解决nimbus单点问题的关键 业界对nimbus单点问题的努力 ...

  10. CentOS6.4 安装LVS-RRD监控LVS

    1.安装依赖包 yum install -y php httpd bc rrdtool 启动apache (我看网上的一些文档说不能用80端口,但我用80端口试了一下也好使,如果出现不好使的情况就改一 ...