一、nginx错误代码
1)、自定义返回给客户端的404错误页面
修改Nginx配置文件,自定义报错页面
vim /usr/local/nginx/conf/nginx.conf
.. ..
charset utf-8; //报错页面含有中文,需开启utf-8
error_page 404 /404.html; //自定义错误页面
.. ..
[root@proxy ~]# vim /usr/local/nginx/html/404.html //生成错误页面
您访问的页面不存在,请联系站长!
另外404 ,303,401 .502 等等 错误代码报错 也可以指向一个网站的网页: error_page 404 403 502 401 http://baidu.com/404.html
[root@proxy ~]# nginx -s reload //重新加载配置文件
优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的40x.html页面
firefox http://192.168.4.5/XXXXXXX //访问一个不存在的页面
二、查看服务器状态
1)、编译安装时使用--with-http_stub_status_module开启状态页面模块
[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_stub_status_module //开启status状态页面
[root@proxy nginx-1.12.2]# make && make install //编译并安装
2)、修改Nginx配置文件,定义状态页面
(另起几行,创建一个location)
[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
stub_status on;
allow 192.168.4.5; //只允许本机IP访问
deny all; //拒绝所有IP访问n
}
… …
[root@proxy ~]# nginx //启动nginx
3)、优化后,查看状态页面信息
[root@proxy ~]# curl http://192.168.4.5/status
Active connections: 1 //当前活动的连接数量
server accepts handled requests //已经接受客户端的连接总数量/已经处理客户端的连接总数量/客户端发送的请求数
10 10 3
Reading: 0 Writing: 1 Waiting: 0 //当前服务器正在读取客户端请求头的数量/当前服务器正在写响应信息的数量/当前多少客户端在等待服务器的响应
三、隐藏Nginx版本号
1)、隐藏Nginx版本号
vim /usr/local/nginx/conf/nginx.conf
在http中添加一行
server_tokens off; //关闭显示nginx 版本号
nginx -s reload //重新加载配置文件
四、优化Nginx并发量
1)、优化前使用ab高并发测试
( 需安装 yum install httpd-tools )
[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/ //注意网站结尾的/必须加上 -c 表示客户端数量 -n 表示所有客户访问的页面数量
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24) //提示打开文件数量过多
2)、修改Nginx配置文件,增加并发量
一个服务器的总并发数是worker_processes*worker_connections,cpu核心数量是由服务器决定,不能自己配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes 2; //与CPU核心数量一致 cat /proc/cpuinfo | grep cores
events {
worker_connections 65535; //每个worker最大并发连接数,连接数可以自定义
}
.. ..
[root@proxy ~]# nginx -s reload //重新加载配置文件
3)、优化Linux内核参数(最大文件数量)
[root@proxy ~]# ulimit -a //查看所有属性值
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3758
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3758
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@proxy ~]# ulimit -Hn 100000 //设置硬限制(临时规则)
[root@proxy ~]# ulimit -Sn 100000 //设置软限制(临时规则),只能警告,不能限制
[root@proxy ~]# vim /etc/security/limits.conf //永久设置,设置完需要reboot
.. ..
* soft nofile 100000
* hard nofile 100000
#该配置文件分4列,分别如下:
#用户或组 硬限制或软限制 需要限制的项目 限制的值
4)、优化后测试服务器并发量
[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
五、优化Nginx数据包头缓存
1)、修改Nginx配置文件,增加数据包头部缓存大小
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size 1k; //默认请求包头信息的缓存
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量,共16K
.. ..
}
[root@proxy ~]# nginx -s reload
六、浏览器本地缓存静态数据
1)、使用Firefox浏览器查看缓存
以Firefox浏览器为例,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息,如图-3所示,点击List Cache Entries可以查看详细信息。