天天看點

Nginx服務優化(1)——隐藏版本号、修改使用者與組、網頁緩存時間、日志切割、連接配接逾時一、隐藏版本号二、修改使用者與組三、配置Nginx網頁緩存時間四、實作Nginx日志分割五、配置Nginx實作連接配接逾時六、補充關于時間日期的指令

這裡寫目錄标題

  • 一、隐藏版本号
      • 1.1、修改配置法
      • 1.2、修改源碼并重新編譯安裝
  • 二、修改使用者與組
      • 2.1編譯安裝時指定使用者與組
      • 2.2修改配置檔案指定使用者與組
  • 三、配置Nginx網頁緩存時間
  • 四、實作Nginx日志分割
  • 五、配置Nginx實作連接配接逾時
  • 六、補充關于時間日期的指令

一、隐藏版本号

1.1、修改配置法

(1) 檢視目前版本

[[email protected] ~]# 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)修改配置檔案

[[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;  ##找到這個地方
server_tokens off;  ## 手動添加這一行,隐藏版本号
==>> wq 儲存
[[email protected] ~]# 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、修改源碼并重新編譯安裝

[[email protected] ~]# 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

[[email protected] nginx-1.12.2]# make && make install

[[email protected] ~]# curl -I http://20.0.0.11
HTTP/1.1 200 OK
Server: nginx/1.1.1

           

二、修改使用者與組

Nginx運作時程序需要有使用者與組的支援,站檔案讀取時進行通路控制

Nginx預設使用nobody使用者賬号與組賬号

修改的方法

2.1編譯安裝時指定使用者與組

我們在編譯安裝 nginx 的時候也可以指定使用者與組,指定安裝目錄

[[email protected] nginx-1.12.2]#./configure \ 
--prefix=/usr/local/nginx \   ##指定安裝位置
--user=nginx \  ## 指定使用者
--group=nginx \  ## 指定組賬戶
--with-http_stub_status_module
           

2.2修改配置檔案指定使用者與組

[[email protected] nginx-1.12.2]# vi /usr/local/nginx/conf/nginx.conf
找到 #user nobody   ==>>  修改成 user nginx nginx;  ##打開配置檔案後就在第一行,然後#号也删除
[[email protected] nginx-1.12.2]# killall -s HUP nginx   ## 重新整理配置檔案
[[email protected] ~]# 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參數

[[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm;
            expires 1d; ###設定緩存時間為1天
        }

[[email protected] ~]# systemctl restart nginx

           

四、實作Nginx日志分割

  • 随着Nginx運作時間增加,日志也會增加握。為了友善掌Nginx運作狀态,需要時刻關注Nginx日志檔案
  • 太大的日志檔案對監控是一個大災難
  • 定期進行日志檔案的切割
  • Nginx自身不具備日志分割處理的功能,但可以通過Nginx信号控制功能的腳本實作日志的自動切割
  • 通過Linux的計劃任務周期性地進行日志切割

    編寫腳本進行日志切割的思路:

    1、設定時間變量

    2、設定儲存日志路徑

    3、将目前的日志檔案進行重命名

    4、重建新日志檔案

    5、删除時間過長的日志檔案

    6、設定cron任務,定期執行腳本自動進行日志分割

[[email protected] ~]# 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; ###去除前面#号

[[email protected] ~]# 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

[[email protected] ~]# 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用來傳遞參數)

[[email protected] ~]# chmod +x fenge.sh
[[email protected] ~]# ./fenge.sh
[[email protected] ~]# cd /var/log/nginx/
[[email protected] 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

設定請求體讀逾時時間

[[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf
   #keepalive_timeout  0;
    keepalive_timeout  180;
    client_header_timeout 80;    ##等待用戶端發送請求頭的逾時時間 逾時會發送408錯誤
    client_body_timeout 80;    ##設定請求體的讀逾時時間

[[email protected] ~]# systemctl restart nginx

           

六、補充關于時間日期的指令

(1)擷取當天的的日期

[[email protected] ~]# date +%Y%m%d

20201016

[[email protected] ~]# date

2020年 10月 16日 星期五 15:40:12 CST

(2)昨天

[[email protected] ~]# date -d “-1 day”

2020年 10月 15日 星期四 15:41:09 CST

(3)明天

[[email protected] ~]# date -d “day”

2020年 10月 17日 星期六 15:41:51 CST

(4)前一天的日期

[[email protected] ~]# date -d “-1 day” +%d

15

(5)前一小時

[[email protected] ~]# date -d “-1 hour” +%H

14

(6)前一分鐘

[[email protected] ~]# date -d “-1 min” +%M

48

(7)前一秒鐘

[[email protected] ~]# date -d “-1 second” +%S

18