nginx常见问题处理

nginx常见的问题:
1.如果客户端访问服务器提示“Too many open files”如何解决
2.如何解决客户端访问头部信息过长的问题
3.如何让客户端浏览器缓存数据
4.如何自定义返回给客户端的404错误页面
5.如何查看服务器状态信息
7.如何开启gzip压缩功能,提高数据传输效率
8.如何开启文件缓存功能

对Nginx服务器进行适当优化,解决如上问题,以提升服务器的处理性能:
一、构建Nginx服务器

yun -y install gcc pcre-devel openssl-devel

tar-xf nginx-1.12.2.tar.gz

cd nginx-1.12.2

//开启SSL加密功能、开启TCP/UDP代理模块

./configure –with-http_ssl_module –with-stream

make && make install

useradd -s /sbin/nologin nginx

ln -s /usr/local/nginx/sbin/nginx /sbin/

nginx

netstat -antup | grep nginx


二、优化Nginx并发量
(1) 优化前使用ab高并发测试 [用ab命令需要装httpd-tools包]

ab -n 2000 -c 2000 http://访问ip/

(2) 修改Nginx配置文件,增加并发量

vim /usr/local/nginx/conf/nginx.conf

……
worker_processes 2; //与CPU核心数量一致
events {
worker_connections 65535; //每个worker最大并发连接数
use epoll;
}
……

nginx -s reload


三、优化Linux内核参数(最大文件数量)

ulimit -a //查看所有属性值

ulimit -Hn 100000 //临时设置硬限制

ulimit -Sn 100000 //临时设置软限制

vim /etc/security/limits.conf

……

用户或组 硬限制或软限制 限制项目 限制的值

*       soft       nofile   100000 
*       hard       nofile   100000

(4)优化后测试服务器并发量
#ab -n 2000 -c 2000 http://访问ip/


四、优化Nginx数据包头缓存
(1) 优化前,使用脚本测试长头部请求是否能获得响应
#vim buffer.sh
#!/bin/bash
URL=http://访问ip/index.html?
for i in {1..5000}
do
URL= URLv U R L v i= idonecurl i d o n e c u r l URL //经过5000次循环后,生成一个长的URL地址栏

(2)所以需要修改Nginx配置文件,增加数据包头部缓存大小
#vim /usr/local/nginx/conf/nginx.conf
……
http {
client_header_buffer_size 1k; //默认请求包头信息的缓存
large_client_header_buffers 4 4k; //大请求包头部信息的缓存个数与容量
……
}

nginx -s reload

(3)优化后,使用脚本测试长头部请求是否能获得响应

./buffer.sh (用之前写的脚本测试)


五、浏览器本地缓存静态数据
1)使用Firefox浏览器查看缓存
以Firefox浏览器为例,在Firefox地址栏内输入about:cache将显示Firefox浏览器的缓存信息,
点击List Cache Entries可以查看详细信息。

(2) 清空firefox本地缓存数据

(3)修改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天
}
}

//拷贝图片到/usr/local/nginx/html下进行测试

cp /usr/share/backgrouds/day.jpg /usr/local/nginx/html

nginx -s reload

(4) 优化后,使用Firefox浏览器访问图片,再次查看缓存信息
firefox http://访问ip/day.jpg
在Firefox地址栏内输入about:cache,查看本地缓存数据,查看是否有图片以及过期时间是否正确。

这里写图片描述

六、自定义报错页面
1)优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到

firefox http://访问ip/xxxxx //访问一个不存在的页面

2)修改Nginx配置文件,自定义报错页面

vim /usr/local/nginx/conf/nginx.conf

…..
error_page 404 /40x.html; //自定义错误页面

:wq (保存)

vim /usr/local/nginx/html/40x.html //生成错误页面

Oops,No NO no page …

:wq (保存)

nginx -s reload

请先确保nginx是启动状态才可以执行命令成功,否则报错,报错信息如下:

[error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)

3)优化后,客户端使用浏览器访问不存在的页面,会提示自己定义的40x.html页面

firefox http://访问ip/xxxxx //访问一个不存在的页面


(4) 常见http状态码
200 :一切正常
301 : 永久重定向
302 : 临时重定向
401 : 用户名或密码错误
403 : 禁止访问(客户端ip地址被拒绝)
404 : 文件不存在
414 : 请求URI头部过长
500 :服务器内部错误
502 :Bad Gateway


七、如何查看服务器状态信息
(1)编译安装时使用–with-http_stub_status_module开启状态页面模块

cd nginx-1.12.2

./configure

–with-http_ssl_module //开启SSL加密功能
–with-stream –with-stream //开启TCP/UDP代理模块
–with-http_stub_status_module //开启status状态页面

make && make install

(2) vim /usr/local/nginx/conf/nginx.conf
…..
location /status {
stub_status on;
}
……..

nginx -s reload

(2)优化后,查看状态页面信息

curl http://访问ip/status


八、对页面进行压缩处理
修改Nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

http{
…….
gzip on; //开启压缩
gzip_min_length 1000; //小文件不压缩(最小1k)
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
……
}

nginx -s reload


九、服务器内存缓存
(1)如果需要处理大量静态文件,可以将文件缓存在内存,下次访问会更快。
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次会被缓存
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值