Nginx服务优化(1)——隐藏版本号、修改用户与组、网页缓存时间、日志切割、连接超时

一、隐藏版本号

1.1、修改配置法

(1) 查看当前版本

[root@localhost ~]# curl -I http://20.0.0.11
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 16 Oct 2020 06:15:34 GMT

(2)修改配置文件

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;  ##找到这个地方
server_tokens off;  ## 手动添加这一行,隐藏版本号
==>> wq 保存
[root@localhost ~]# curl -I http://20.0.0.11
HTTP/1.1 200 OK
Server: nginx     ##版本号已经隐藏了
Date: Fri, 16 Oct 2020 06:25:37 GMT

1.2、修改源码并重新编译安装

[root@localhost ~]# vi /root/nginx-1.12.2/src/core/nginx.h
#define nginx_version      1012002
#define NGINX_VERSION      "1.1.1" ###修改版本号
#define NGINX_VER          "nginx/" NGINX_VERSION

[root@localhost nginx-1.12.2]# make && make install

[root@localhost ~]# curl -I http://20.0.0.11
HTTP/1.1 200 OK
Server: nginx/1.1.1

二、修改用户与组

Nginx运行时进程需要有用户与组的支持,站文件读取时进行访问控制
Nginx默认使用nobody用户账号与组账号
修改的方法

2.1编译安装时指定用户与组

我们在编译安装 nginx 的时候也可以指定用户与组,指定安装目录

[root@localhost nginx-1.12.2]#./configure \ 
--prefix=/usr/local/nginx \   ##指定安装位置
--user=nginx \  ## 指定用户
--group=nginx \  ## 指定组账户
--with-http_stub_status_module

2.2修改配置文件指定用户与组

[root@localhost nginx-1.12.2]# vi /usr/local/nginx/conf/nginx.conf
找到 #user nobody   ==>>  修改成 user nginx nginx;  ##打开配置文件后就在第一行,然后#号也删除
[root@localhost nginx-1.12.2]# killall -s HUP nginx   ## 刷新配置文件
[root@www ~]# ps aux | grep nginx
root       4324  0.0  0.0  20572  1516 ?        Ss   10:21   0:00 nginx: master process nginx
nginx      4725  0.0  0.0  23076  1456 ?        S    10:50   0:00 nginx: worker process
root       4738  0.0  0.0 112676   984 pts/0    S+   10:51   0:00 grep --color=auto nginx

## 修改完之后过滤查看下

三、配置Nginx网页缓存时间

1、当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
2、一般针对静态网页设置,对动态网页不设置缓存时间
3、设置方法
在主配置文件的location段加入expires参数

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm;
            expires 1d; ###设置缓存时间为1天
        }

[root@localhost ~]# systemctl restart nginx

四、实现Nginx日志分割

  • 随着Nginx运行时间增加,日志也会增加握。为了方便掌Nginx运行状态,需要时刻关注Nginx日志文件
  • 太大的日志文件对监控是一个大灾难
  • 定期进行日志文件的切割
  • Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割
  • 通过Linux的计划任务周期性地进行日志切割
    编写脚本进行日志切割的思路:
    1、设置时间变量
    2、设置保存日志路径
    3、将目前的日志文件进行重命名
    4、重建新日志文件
    5、删除时间过长的日志文件
    6、设置cron任务,定期执行脚本自动进行日志分割
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
error_log  logs/error.log  info;

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main; ###去除前面#号

[root@localhost ~]# nginx -t ###检查配置文件是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost ~]# vim fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d") 
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid" ###设置日期及路径变量
[ -d $logs_path ] || mkdir -p $logs_path ###自动创建日志目录
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d ###分割新的日志
kill -HUP $(cat $pid_path) ###生成新的日志
find $logs_path -mtime +30 | xargs rm -rf ###删除30天前的日志(xargs用来传递参数)

[root@localhost ~]# chmod +x fenge.sh
[root@localhost ~]# ./fenge.sh
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 44
-rw-r--r--. 1 root root 44866 10月 16 15:11 test.com-access.log-20201015 ###运行之后生成昨天的日志


五、配置Nginx实现连接超时

为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
超时参数
■Keepalive_timeout
设置连接保持超时时间
■Client_header_timeout
指定等待客户端发送请求头的超时时间
■Client_body_timeout
设置请求体读超时时间

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   #keepalive_timeout  0;
    keepalive_timeout  180;
    client_header_timeout 80;    ##等待客户端发送请求头的超时时间 超时会发送408错误
    client_body_timeout 80;    ##设置请求体的读超时时间

[root@localhost ~]# systemctl restart nginx

六、补充关于时间日期的命令

(1)获取当天的的日期
[root@localhost ~]# date +%Y%m%d
20201016
[root@localhost ~]# date
2020年 10月 16日 星期五 15:40:12 CST

(2)昨天
[root@localhost ~]# date -d “-1 day”
2020年 10月 15日 星期四 15:41:09 CST

(3)明天
[root@localhost ~]# date -d “day”
2020年 10月 17日 星期六 15:41:51 CST

(4)前一天的日期
[root@localhost ~]# date -d “-1 day” +%d
15

(5)前一小时
[root@localhost ~]# date -d “-1 hour” +%H
14

(6)前一分钟
[root@localhost ~]# date -d “-1 min” +%M
48

(7)前一秒钟
[root@localhost ~]# date -d “-1 second” +%S
18

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值