大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了。而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能很好地工作了。然而,如果你真的想挤压出Nginx的性能,你必须更深入一些。在本指南中,我将解释Nginx的那些设置可以微调,以优化处理大量客户端时的性能。需要注意一点,这不是一个全面的微调指南。这是一个简单的预览——那些可以通过微调来提高性能设置的概述。你的情况可能不同。
基本的 (优化过的)配置
我们将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置。你应该能够在服务器的/etc/nginx目录中找到nginx.conf。首先,我们将谈论一些全局设置,然后按文件中的模块挨个来,谈一下哪些设置能够让你在大量客户端访问时拥有良好的性能,为什么它们会提高性能。本文的结尾有一个完整的配置文件。
高层的配置
nginx.conf文件中,Nginx中有少数的几个高级配置在模块部分之上。
-
user
www-data; -
pid
/var/run/nginx.pid; -
worker_processes
auto; -
worker_rlimit_nofile
100000;
user和pid应该按默认设置 - 我们不会更改这些内容,因为更改与否没有什么不同。
worker_processes
worker_rlimit_nofile
Events模块
events模块中包含nginx中所有处理连接的设置。
-
events
{ -
worker_connections
2048; -
multi_accept
on; -
use
epoll; -
}
worker_connections
记住,最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处。
multi_accept
use
(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)
HTTP 模块
HTTP模块控制着nginx http处理的所有核心特性。因为这里只有很少的配置,所以我们只节选配置的一小部分。所有这些设置都应该在http模块中,甚至你不会特别的注意到这段设置。
-
http
{ -
server_tokens
off; -
sendfile
on; -
tcp_nopush
on; -
tcp_nodelay
on; -
...
-
}
server_tokens
sendfile
tcp_nopush
tcp_nodelay
-
access_log
off; -
error_log
/var/log/nginx/error.log crit;
access_log
error_log
-
keepalive_timeout
10; -
client_header_timeout
10; -
client_body_timeout
10; -
reset_timedout_connection
on; -
send_timeout
10;
keepalive_timeout
client_header_timeout 和client_body_timeout
reset_timeout_connection
send_timeout
-
limit_conn_zone
$binary_remote_addr zone=addr:5m; -
limit_conn
addr 100;
limit_conn_zone
limit_conn
-
include
/etc/nginx/mime.types; -
default_type
text/html; -
charset
UTF-8;
include
default_type
charset
-
gzip
on; -
gzip_disable
"msie6"; -
#
gzip_static on; -
gzip_proxied
any; -
gzip_min_length
1000; -
gzip_comp_level
4; -
gzip_types
text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip
gzip_disable
gzip_static
gzip_proxied
gzip_min_length
gzip_comp_level
gzip_type
-
#
cache informations about file descriptors, frequently accessed files -
#
can boost performance, but you need to test those values -
open_file_cache
max=100000 inactive=20s; -
open_file_cache_valid
30s; -
open_file_cache_min_uses
2; -
open_file_cache_errors
on; -
##
-
#
Virtual Host Configs -
#
aka our settings for specific servers -
##
-
include
/etc/nginx/conf.d/*.conf; -
include
/etc/nginx/sites-enabled/*;
open_file_cache
open_file_cache_valid
open_file_cache_min_uses
open_file_cache_errors
一个完整的配置
-
user
www-data; -
pid
/var/run/nginx.pid; -
worker_processes
auto; -
worker_rlimit_nofile
100000; -
events
{ -
worker_connections
2048; -
multi_accept
on; -
use
epoll; -
}
-
http
{ -
server_tokens
off; -
sendfile
on; -
tcp_nopush
on; -
tcp_nodelay
on; -
access_log
off; -
error_log
/var/log/nginx/error.log crit; -
keepalive_timeout
10; -
client_header_timeout
10; -
client_body_timeout
10; -
reset_timedout_connection
on; -
send_timeout
10; -
limit_conn_zone
$binary_remote_addr zone=addr:5m; -
limit_conn
addr 100; -
include
/etc/nginx/mime.types; -
default_type
text/html; -
charset
UTF-8; -
gzip
on; -
gzip_disable
"msie6"; -
gzip_proxied
any; -
gzip_min_length
1000; -
gzip_comp_level
6; -
gzip_types
text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; -
open_file_cache
max=100000 inactive=20s; -
open_file_cache_valid
30s; -
open_file_cache_min_uses
2; -
open_file_cache_errors
on; -
include
/etc/nginx/conf.d/*.conf; -
include
/etc/nginx/sites-enabled/*; -
}
编辑完配置后,确认重启nginx使设置生效。
-
sudo
service nginx restart
12万+

被折叠的 条评论
为什么被折叠?



