一、nginx优化
1. 性能优化:
需求内容 | 实现 |
---|
IO多路复用 | use epoll; |
设置工作进程数 | worker_processes 与CPU数量相同或auto; |
工作进程静态绑核 | worker_cpu_affinity |
设置并发 | worker_connections worker_rlimit_nofile |
连接保持超时 | keepalive_timeout 服务器超时时间 [客户端超时时间]; |
网页压缩 | gzip on; |
页面缓存时间 | expires 时间; |
2. 安全优化:
需求内容 | 实现 |
---|
隐藏版本号 | server_tokens off; || 修改源代码 |
防盗链 | valid_referers if ($invalid_referer) {rewrite } |
设置运行用户/组 | user 用户名 组名; |
限制单个ip的访问频率 | limit_req_zone limit_req |
限制单个ip的连接数 | limit_conn_zone limit_conn |
二、系统内核优化
vim /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 65536
net.core.somaxconn = 65536
net.ipv4.tcp_keepalive_time = 600
-----------------------------------------------------------------------------------------------------
vi /etc/security/limits.conf 内核限制文件
* soft noproc 65535
* hard noproc 65535
* soft nofile 65535
* hard nofile 65535
* soft memlock unlimited
* hard memlock unlimited
三、nginx常用模块
模块名 | 模块作用 |
---|
ngx_http_core_module | 核心模块,提供了基本的 HTTP 功能,如请求处理、重定向、访问控制等。 |
ngx_http_ssl_module | SSL/TLS 模块,用于支持 HTTPS 协议,提供了加密通信和安全认证功能。 |
ngx_http_proxy_module | 反向代理模块,用于将客户端请求转发到后端服务器,并将后端服务器的响应返回给客户端。 |
ngx_http_rewrite_module | 重写模块,用于重写 URL,修改请求头和请求体等。 |
ngx_http_gzip_module | gzip 压缩模块,用于在服务器端对响应内容进行压缩,减少传输数据量,提高网站加载速度。 |
ngx_http_fastcgi_module | FastCGI 模块,用于与 FastCGI 应用程序通信,实现动态内容的处理和生成。 |
ngx_http_limit_req_module | 请求限速模块,用于限制客户端的请求速率,防止恶意请求或者 DDoS 攻击。 |
ngx_http_geoip_module | GeoIP 模块,用于根据客户端的 IP 地址获取其地理位置信息,用于内容定制和访问控制。 |
ngx_http_access_module | 访问控制模块,用于设置访问规则,允许或拒绝特定的客户端请求。 |
ngx_http_headers_module | HTTP 头部处理模块,用于添加、修改或删除 HTTP 请求和响应的头部信息。 |
ngx_http_cache_module | HTTP 缓存模块,用于在服务器端缓存响应内容,加速对重复请求的响应。 |
ngx_http_stub_status_module | 状态监控模块,用于实时监控 Nginx 的运行状态和性能指标。 |