1.对并发量的优化
一个服务器的总并发数是worker_processes*worker_connections,cpu核心数量是由服务器决定,不能自己配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; //与CPU核心数量一致
events {
worker_connections 65535; //每个worker最大并发连接数
use epoll;
}
[root@proxy ~]# nginx -s reload
2.优化Linux内核参数(最大文件数量)
[root@proxy ~]# ulimit -a //查看所有属性值
[root@proxy ~]# ulimit -Hn 100000 //设置硬限制(临时规则)
[root@proxy ~]# ulimit -Sn 100000 //设置软限制(临时规则)
[root@proxy ~]# vim /etc/security/limits.conf
* soft nofile 100000 ---用户或组 硬限制或软限制 需要限制的项目 限制的值
* hard nofile 100000
3.优化Nginx数据包头缓存
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http {
client_header_buffer_size 1k; //默认请求包头信息的缓存
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量
}
[root@proxy ~]# nginx -s reload
4.浏览器本地缓存静态数据
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
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天
}
}
[root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# nginx -s reload
5.自定义报错页面
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
error_page 404 /40x.html; //自定义错误页面
[root@proxy ~]# vim /usr/local/nginx/html/40x.html //生成错误页面
[root@proxy ~]# nginx -s reload
6. 如何查看服务器状态信息
[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel
[root@proxy ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@proxy ~]# cd nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure \
> --with-http_ssl_module //开启SSL加密功能
> --with-stream //开启TCP/UDP代理模块
> --with-http_stub_status_module //开启status状态页面
[root@proxy nginx-1.12.2]# make && make install //编译并安装
[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
stub_status on;
}
… …
[root@proxy ~]# nginx
[root@proxy ~]# curl http://192.168.4.5/status
Active connections: 1
server accepts handled requests
10 10 3
Reading: 0 Writing: 1 Waiting:
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。
7.对页面进行压缩处理
[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
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
.. ..
}
7.服务器内存缓存
http {
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
//设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
//文件句柄的有效时间是60秒,60秒后过期
//只有访问次数超过5次会被缓存
}