- 全局配置优化,优化nginx并发量
- 修改配置文件
.. ..
worker_processes 2; //与CPU核心数量一致
events {
worker_connections 65535; //每个worker最大并发连接数
use epoll;
}
.. ..
- 修改内核参数(最大文件数量)
ulimit -a //查看所有属性值
ulimit -Hn 100000 //设置硬限制(临时规则)
ulimit -Sn 100000 //设置软限制(临时规则)
vim /etc/security/limits.conf
.. ..
* soft nofile 100000
* hard nofile 100000
- 优化Nginx数据包头缓存
.. ..
http {
client_header_buffer_size 1k; //默认请求包头信息的缓存
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量
.. ..
}
- 浏览器本地缓存静态数据
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 30d; //定义客户端缓存时间为30天
}
}
- 如何查看服务器状态信息
- 编译安装时使用--with-http_stub_status_module开启状态页面模块
- 修改配置文件
… …
location /status {
stub_status on;
}
- 然后测试 curl http://192.168.4.5/status
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。
- 对页面进行压缩处理
http {
.. ..
gzip on; //开启压缩
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;
//对特定文件压缩,类型参考mime.types
.. ..
}
- 自定义报错页面
When nothing seems to help, I go look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before. -- Jacob Riis