安全配置
隐藏版本号
vim nginx.conf
http {
……省略
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off; <- 关闭
…….省略
}
优化配置
user www-data;
pid /var/run/nginx.pid;
worker_processes auto; # worder进程数,auto将自动获取系统内核数
worker_rlimit_nofile 100000; # worker进程的最大打开文件数限制,设高以避免出现 open too many files
events {
worker_connections 2048; # 单worker同时打开的最大连接数
multi_accept on; # 告诉nginx收到一个新连接通知后接受尽可能多的连接
use epoll; # 设置复用客户端线程的轮询方法
}
http {
server_tokens off; # 隐藏版本号
sendfile on; # 开启sendfile(),可以在磁盘和TCP socket之间拷贝数据
tcp_nopush on; # 在一个数据包中发送所有头文件
tcp_nodelay on; # 不要缓存数据,而是一段一段的发送
...
}
access_log off; # 关闭日志,并只记录严重错误
error_log /var/log/nginx/error.log crit;
keepalive_timeout 10; # 给客户端分配keepalive连接超时时间,将在这个时间之后关闭连接,尽可能小以让nginx工作的时间更长
client_header_timeout 10; # 请求头和请求体的超时时间
client_body_timeout 10;
reset_timedout_connection on; # 关闭不响应的客户端连接
send_timeout 10; # 客户端的响应超时时间
# 设置用于保存各种key的共享内存参数
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100; # 为给定的key设置最大的连接数,addr指允许一个IP最多同时打开100个连接
include /etc/nginx/mime.types; # include为导入文件,以供使用
default_type text/html; # 设置文件使用的默认的MIME-type
charset UTF-8; # 默认字符集
gzip on; # 在用gzip压缩发送数据
gzip_disable "msie6"; # 为指定的客户端禁用gzip功能
# gzip_static on; # 告诉nginx在压缩之前寻找是否有预先处gzip处理过的资源
gzip_proxied any; # 允许或者禁止压缩基于请求和响应的响应流,any为压缩所有请求
gzip_min_length 1000; # 设置对数据启用压缩的最小字节数
gzip_comp_level 4; # 设置数据压缩等级,下面的gzip_types是指需要压缩的数据格式
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# 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在指定检测正确信息的时间间隔
open_file_cache_min_uses 2; # 定义了open_file_cache中指令参数不活动时间期间里最小的文件数
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/*;