nginx小功能的归纳

2021/5/30 7:30:05

本文主要是介绍nginx小功能的归纳,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.nginx中网页404报错处理

打开配置文件
[root@www nginx-1.18.0]# vim /usr/local/nginx/conf/nginx.conf
 #error_page  404              /404.html;  #将此行注释去掉
 在/usr/local/nginx/html 目录下创建404.html文件,并将修改好的404页面的代码放进去即可
 若是404.html文件有中文,需要将下列行注释去掉并更改
 #charset koi8-r;   --> charset utf8;      
 修改完配置文件后重启nginx服务
 [root@www nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s reload

2.nginx状态模块

使用状态模块需要编译的时候增加一个状态模块  --with-http_stub_module
打开配置文件
[root@www nginx-1.18.0]# vim /usr/local/nginx/conf/nginx.conf
在与localtion平级的其他位置添加如下内容
location /status{
        stub_status on;
        #allow IP地址;  #允许访问状态文件的ip地址
        #deny IP地址;    #拒绝访问状态文件的ip地址
}
修改完配置文件后重启nginx服务
 [root@www nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s reload

image.png

accepts : 已经接受客户端tcp链接的次数;

handle:已经处理客户端tcp链接的次数;

request:客户端发送的请求的次数


3.优化nginx并发量

优化前使用ab高并发测试
[root@www nginx-1.18.0]# ab -n 200 -c 200   -n为同时多少人访问 -c为同时访问的总次数 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   17  13.5     12      37
Processing:    14   15   1.6     15      37
Waiting:        0   13   1.1     13      14
Total:         17   32  13.5     27      51

Percentage of the requests served within a certain time (ms)
  50%     27
  66%     49
  75%     50
  80%     50
  90%     51
  95%     51
  98%     51
  99%     51
 100%     51 (longest request)
[root@www nginx-1.18.0]# ab -n 1024 -c 1024 http://192.168.0.20/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.0.20 (be patient)
socket: Too many open files (24)
一般不优化并发量的情况下,默认最大的并发量为1024
优化并发量的方法:
打开配置文件更改为
worker_processes  2;    //与cpu内核数量一致
events {
    worker_connections  65535;       // 数量填写往大写,防止资源浪费
}
重启配置文件
[root@www nginx-1.18.0]# 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) 7183
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) 7183    //最大数量用户进程
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

[root@www nginx-1.18.0]# ulimit -Hn 10000   //软限制,到这个值会有警告信息,但不能超过硬限制的值        (临时规则:重启计算机之后会回到1024初始值)
[root@www nginx-1.18.0]# ulimit -Sn 10000    //硬限制,不能超过这个值               
想要永久修改的话
[root@www nginx-1.18.0]# vim /etc/security/limits.conf           
加入两行配置
*    soft    nofile    10000
*    hard    nofile    10000


4.nginx缓存优化

打开配置文件
在server下添加以下内容
        location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)${  // ~为模糊匹配 *为不区分大小写  
                expire 30d;  //缓存30天
}
重启服务


5.nginx日志切割

nginx日志存放路径
[root@www nginx-1.18.0]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid      //access.log 日志文件   error.log 错误日志文件 nginx.pid nginx服务的进程id文件
access.log文件不能太大,超过10G左右就不能打开去分析日志,所以需要切割日志

[root@www logs]# mv access.log access2.log  //将老的日志改名
[root@www logs]# kill -USR1 `cat nginx.pid` //通过kill -USR1 命令发送一个信号让nginx进程再产生一个新的日志文件
[root@www logs]# ls
access2.log  access.log  error.log  nginx.pid
日常工作中可以编写日志切割脚本和计划任务结合使用
[root@www logs]# vim logbak.sh
#/bin/bash
date=`date +%F`
logpath=/usr/local/nginx/log
mv $logpath/access.log  $logpath/access-$date.log
mv $logpath/error.log  $logpath/error-$date.log  
kill -USR1 `cat $logpath/nginx.pid`
[root@www logs]# crontab -e  
03 03 * * 5   sh  /usr/local/nginx/logs/logbak.sh




这篇关于nginx小功能的归纳的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程