1.高层配置
user www-data;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;
user 和 pid 按默认设置
worker_processes 定义了 nginx 对外提供 web 服务时的 worder 进程数。最优值取决于许多因素,包括 CPU 核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的 CPU 内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。
worker_rlimit_nofile 更改 worker 进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和 Nginx 可以处理比ulimit -a更多的文件,所以把这个值设高,这样 nginx 就不会有 too many open files问题了。
2.Events 模块
events 模块中包含 nginx 中所有处理连接的设置
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
worker_connections 设置可由一个 worker 进程同时打开的最大连接数。如果设置了上面提到的 worker_rlimit_nofile,我们可以将这个值设得很高。
multi_accept 告诉 nginx 收到一个新连接通知后接受尽可能多的连接。
use 设置用于复用客户端线程的轮询方法。
3.HTTP 模块
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
...
}
server_tokens 并不会让 nginx 执行的速度更快,但它可以关闭在错误页面中的 nginx 版本数字,这样对于安全性是有好处的。
sendfile 可以让 sendfile()发挥作用。sendfile()可以在磁盘和 TCP socket 之间互相拷贝数据 (或任意两个文件描述符)。Pre-sendfile 是传送数据之前在用户空间申请数据缓冲区,之后用 read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到 OS 缓存。因为这种拷贝是在内核完成的,sendfile()要比组合 read()和 write()以及打开关闭丢弃缓冲更加有效
tcp_nopush 告诉 nginx 在一个数据包里发送所有头文件,而不一个接一个的发送
tcp_nodelay 告诉 nginx 不要缓存数据,而是一段一段的发送。
access_log off;
error_log /var/log/nginx/error.log crit;
access_log 设置 nginx 是否存储访问日志。关闭这个选项可以让读取磁盘 IO 操作更快
error_log 告诉 nginx 只记录严重的错误
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
keepalive_timeout 给客户端分配 keep-alive 连接超时时间。服务器将在这个超时时间过后关闭链接。将它设置低些可以让 ngnix 持续工作的时间更长。
client_header_timeout 和 client_body_timeout 设置请求头和请求体(各自)的超时时间。也可以把这个设置低些。
reset_timeout_connection 告诉 nginx 关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx 就会关闭连接。
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
limit_conn_zone 设置用于保存各种 key(比如当前连接数)的共享内存的参数。5m 就是 5 兆字节,这个值应该被设置的足够大以存储(32K5)32byte 状态或者(16K5)64byte 状态。limit_conn 为给定的 key 设置最大连接数。这里 key 是 addr,我们设置的值是 100,也就是说我们允许每一个 IP 地址最多同时打开有 100 个连接。
include /etc/nginx/mime.types;
default_type text/html;
charset UTF-8;
default_type 设置文件使用的默认的 MIME-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 是告诉 nginx 采用 gzip 压缩的形式发送数据。这将会减少我们发送的数据量。
gzip_disable 为指定的客户端禁用 gzip 功能。我们设置成 IE6 或者更低版本以使我们的方案能够广泛兼容。
gzip_static 告诉 nginx 在压缩资源之前,先查找是否有预先 gzip 处理过的资源。这要求你预先压缩你的文件,从而允许你使用最高压缩比,这样 nginx 就不用再压缩这些文件了。
gzip_proxied 允许或者禁止压缩基于请求和响应的响应流。我们设置为 any,意味着将会压缩所有的请求。
gzip_min_length 设置对数据启用压缩的最少字节数。如果一个请求小于 1000 字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
gzip_comp_level 设置数据的压缩等级。这个等级可以是 1-9 之间的任意数值,9 是最慢但是压缩比最大的。我们设置为 4,这是一个比较折中的设置。
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;
open_file_cache 打开缓存的同时也指定了缓存最大数目,以及缓存的时间。可以设置一个相对高的最大时间,这样可以在它们不活动超过 20 秒后清除掉。
open_file_cache_valid 在 open_file_cache 中指定检测正确信息的间隔时间。
open_file_cache_min_uses 定义了 open_file_cache 中指令参数不活动时间期间里最小的文件数。
open_file_cache_errors 指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。
示例:
user www www;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /data0/log-data/nginx_error.log error;
events {
use epoll;
worker_connections 65535;
}
http {
add_header 'X-Server' $hostname;
server_tokens off;
include mime.types;
default_type text/html;
charset UTF-8;
output_buffers 1 512k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 3M;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_types application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-javascript application/xml application/xml+rss application/xhtml+xml font/opentype font/truetype image/svg+xml text/css text/javascript text/js text/json text/plain text/x-component text/xml;
gzip_vary on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 4 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
access_log off;
include conf.d/*.conf;
}
#常用安全配置
1、将每个~ .php$请求转递给 PHP 将 php.ini 文件中 cgi.fix_pathinfo 设置为 0 (cgi.fix_pathinfo=0) 。这样确保 PHP 检查文件全名
2、禁用 autoindex 模块
配置文件的 location 块中增加 autoindex off;。
3、关闭服务器标记
如果开启的话(默认情况下)所有的错误页面都会显示服务器的版本和信息。将 server_tokens off;声明添加到 Nginx 配置文件。
4、将 timeout 设低来防止 DOS 攻击
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;
5、限制用户连接数来预防 DOS 攻击
limit_zone slimits $binary_remote_addr 5m;
limit_conn slimits 5;