NGINX常见问题及其优化

- 1)修改nginx服务器默认的报错页面

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
error_page   404  /40x.html;    //自定义错误页面
.. ..
[root@nginx ~]# vim /usr/local/nginx/html/40x.html        //生成错误页面
Do not find page …
[root@nginx ~]# nginx -s reload
修改完配置文件一定要进行配置文件的重新加载

2)常见的http报错状态码
在这里插入图片描述
3)查看服务器的状态信息

第一步:安装的时候加载模块
[root@nginx nginx-1.12.2]# ./configure   \
> --with-http_stub_status_module                //开启status状态页面
[root@proxy nginx-1.12.2]# make && make install    //编译并安装
安装nginx的时候添加上--with-http_stub_status_module模块
第二步:修改nginx的默认的配置文件
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
… …
location /status {
                stub_status on;		打开显示nginx服务器状态
                 #allow IP地址;
                 #deny IP地址;
        }
… …
[root@nginx ~]# nginx -s reload
记住:没一次修改默认配置文件的时候都需要重新加载配置文件
第三步:查看优化后的状态页面信息
[root@nginx ~]# curl  http://192.168.4.5/status
Active connections: 1 
server accepts handled requests
 10 10 3 
Reading: 0 Writing: 1 Waiting: 0
###################################################
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量。
(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。

4)优化nginx并发量

第一步:优化之前采用ab高并发测试,显示报错信息
[root@nginx ~]# ab -n 2000 -c 2000 http://192.168.4.5/
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24)                
//too many open files表示打开文件数量过多,其实就是并发量太小限制了文件的打开
第二步:修改配置增加并发量
	**方法一:修改nginx内置的配置文件**
	[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
	.. ..
	worker_processes  2;                    //修改同一时间的进程数,与CPU核心数量一致
	events {
	worker_connections 65535;        //修改每个worker最大并发连接数,修改为最大值:65535.
	use epoll;
	}
	.. ..
[root@nginx ~]# nginx -s reload		//重新加载nginx的默认配置文件
	**方法二:修改linux内核参数(修改最大文件的连接数量)**
	[root@nginx ~]# ulimit -a                        //查看所有属性值
	[root@nginx ~]# ulimit -Hn 100000                //设置硬限制(临时规则)
	[root@nginx ~]# ulimit -Sn 100000                //设置软限制(临时规则)
	以上设置的都是临时规则,重起之后就会失效,想要实现永久规则,必须修改配置文件
	[root@nginx ~]# vim /etc/security/limits.conf
	    .. ..
	*               soft    nofile            100000
	*               hard    nofile            100000
	以上四列对应的内容分别为:
	*                        (hard|soft )              nofile                    100000
	用户或组			硬限制或软限制 			限制的项目			限制的值
nofile,在配置文件的例子中可以查找,对应的信息
35#        - nofile - max number of open file descriptors
第三步:优化之后从新测试服务器的并发量
[root@nginx~]# ab -n 2000 -c 2000 http://192.168.4.5/

5)优化nginx数据包头缓存

第一步:显示常见的报错信息
		414 Request-URI Too Large				//显示url路径太长,平时基本上用不到此项优化
第二步:修改nginx默认配置文件增加包头缓存信息
		[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
		.. ..
		http {
		client_header_buffer_size    4k;        //默认请求包头信息的缓存    
		large_client_header_buffers  4 4k;        //大请求包头部信息的缓存个数与容量
		.. ..
		}
[root@nginx ~]# nginx -s reload   记得重新加载配置文件或者重新启动服务器
第三步:再去重新访问验证,就会显示正常信息

6)优化nginx服务器对于本第浏览器的缓存配置

第一步:打开浏览器,查看都有那些缓存项目
地址栏输入:about:cache查看
第二步:打开nginx的配置文件,查看nginx对页面的换村项目以及缓存时间
[root@nginx ~]# 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@nginx~]# nginx -s reload

7)对页面进行压缩处理

		为了题号nginx服务器的相应速度,所以指定对于某些比较大的文件在传递的过程中进行压缩处理
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
http {
.. ..
gzip on;                         				//开启nginx的页面压缩
gzip_min_length 1000;                //小于1000k的文件不压缩
gzip_comp_level 4;                		//压缩比率
压缩比率在0~9之间,压缩比率越大,压缩越彻底,但是速度比较慢,所以取一个合适 的值
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 //对特定文件压缩,类型参考mime.types
.. ..
}

8)优化nginx的内存缓存

如果需要处理大量的静态文件,可以将文件缓存在内存,下次访问的时候会更快。
[root@nginx ~]# cat /usr/local/nginx/conf/nginx.conf
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次会被缓存,缓存时间为指定的60秒钟,60秒后个过期。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值