一.Nginx服务优化
nginx应用配置文件的优化:
nginx的性能优化
开启网页压缩 gzip on;
页面缓存 expires 缓存时间;
连接保持超时 keepalive_timeout 服务端超时时间 客户端超时时间;
设置工作进程数 work_processes 与服务器CPU数量相同或auto
设置工作进程连接数 worker_connections worker_rlimit_nofile
工作进程静态绑核 worker_cpu_affinity
开启高效文件传输模式 sendfile on; tcp_nopush on; tcp_nodelay on;
IO多路复用 use epoll;
连接优化 multi_accept on;(一个进程同时接受多个网络连接) accept_mutex on;(以串行方式接入新连接,防止惊群现象发生)
nginx的安全优化
隐藏版本号 server_tokens off; 修改源代码 nginx.h
防盗链 valid_referers if ($invalid_referer) {rewrite ....} rewrite地址重写
访问控制 deny/allow
设置运行用户/组 user 用户名 组名;
限制请求数 limit_req_zone limit_req
限制连接数 limit_conn_zone limit_conn
日志分割 : shell脚本 + crontab
系统内核优化:
/etc/sysctl.conf 内核参数配置文件
#用于解决系统存在大量TIME WAIT状态连接的问题
net.ipv4.tcp_syncookies=1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击
net.ipv4.tcp_tw_reuse=1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接
net.ipv4.tcp_tw_recycle=1 表示开启TCP连接中TIME-WAIT sockets的快速回收
net.ipv4.tcp_fin_timeout=30 修改MSL值,系统默认的TIMEOUT时间
#如果连接数本身就很多,可再优化TCP的可用端口范围,进一步提升服务器的并发能力
net.ipv4.tcp_keepalive_time=1200 #当keepalive启用时,TCP发送keepalive探测消息的频率,确认客户端是否断网
net.ipv4.ip_local_port_range=1024 65535 #用于向外连接的端口范围。缺省情况下很小,为32768 60999
net.ipv4.tcp_max_syn_backlog=8192 #SYN队列长度,默认为1024,加大队列长度为8192,可容纳更多等待连接的网络连接数
net.ipv4.tcp_max_tw_buckets=5000 #表示系统同时保持TIME WAIT的最大数量
net.core.somaxconn=65535 #一个端口能够监听的最大连接数
#如果需要IP路由转发
net.ipv4.ip_forward=1
/etc/security/limits.conf 内核资源限制文件
* soft noproc 65535 打开系统进程数
* hard noproc 65535
* soft nofile 65535 进程打开文件数
* hard nofile 65535
二.Nginx常用模块:
http_stub_status_module 访问状态统计模块
http_gzip_module 网页压缩模块
http_rewrite_module URL地址重写模块
http_ssl_module https安全加密模块
http_auth_basic_module 网页用户认证模块
http_fastcgi_module fastcgi转发模块
http_image_filter_module 图片处理模块
http_mp4/flv_module mp4/flv视频格式模块
http_limit_req_module 限制请求数模块
http_limit_conn_module 限制连接数模块
http_proxy_module 代理转发模块
http_upstream_*_module 负载均衡模块
stream 四层代理转发模块