nginx深度優化——隱藏版本號、日誌分割、緩存處理

內容要點:html

  • 隱藏版本號nginx

  • 網頁緩存vim

  • 日誌分割緩存


隱藏版本號bash

兩種配置方法:app

  • 修改配置文件法curl

  • 修改源碼法ide

1、修改配置文件法:測試

[root@localhost init.d]# curl -I http://192.168.13.140/   ##查看Nginx信息
HTTP/1.1 200 OK
Server: nginx/1.12.2    ##顯示版本號
Date: Tue, 12 Nov 2019 14:23:24 GMT
Content-Type: text/htmlContent-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"Accept-Ranges: bytes
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf  ##修改配置文件

http {           ##在http下添加
        include       mime.types;
        default_type  application/octet-stream;
        server_tokens off;    ##關閉版本號

[root@localhost init.d]# service nginx stop  ##關閉服務
[root@localhost init.d]# service nginx start  ##開啓服務
[root@localhost init.d]# curl -I http://192.168.13.140/  ##查看Nginx信息
HTTP/1.1 200 OK      
Server: nginx            ##版本號被隱藏
Date: Tue, 12 Nov 2019 14:22:00 GMT
Content-Type: text/html
Content-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 
GMTConnection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

使用curl -I 命令檢測,能夠看到版本號優化

blob.png

blob.png


再次使用curl -I 進行查詢

blob.png


2、僞造版本號(需從新編譯安裝,也可在編譯安裝以前操做)

[root@localhost ~]# cd /opt/nginx-1.12.1/src/core
[root@localhost core]# vim nginx.h           進入配置文件中修改你想展現的版本號
[root@localhost core]# cd ../../
[root@localhost nginx-1.12.2]# ./configure \                而後進行從新編譯
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

blob.png

重啓nginx服務,查看版本信息

[root@localhost nginx-1.12.2]# service nginx stop  ##關閉
[root@localhost nginx-1.12.2]# service nginx start  ##開啓
[root@localhost nginx-1.12.2]# curl -I http://192.168.13.140/   ##查看Nginx信息
HTTP/1.1 200 OK 
Server: nginx/1.1.1       ##此時的版本號就是僞造的版本號
Date: Tue, 12 Nov 2019 14:34:02 
GMTContent-Type: text/htmlContent-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 
GMTConnection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes


網頁緩存時間

  • 當Nginx將網頁數據返回給客戶端後,可設置緩存時間,以方便在往後進行相同內容的請求時直接返回,避免重複請求,加快了訪問速度

  • 通常針對靜態網頁設置 ,對動態網頁不設置緩存時間

  • 可在Windows客戶端中使用fiddler查看網頁緩存時間

配置方法:

  • 可修改配置文件,在http段,或者server段,或者location段加入對特定內容的過時參數

一、將測試圖片複製至nginx網頁站點目錄下

[root@localhost mnt]# cp test.jpg /usr/local/nginx/html/   ##複製圖片到站點中
[root@localhost mnt]# cd /usr/local/nginx/html/    ##切換到站點下
[root@localhost html]# ls
test.jpg  50x.html  index.ht

二、修改網頁信息,將測試圖片添加到index.html文件中

[root@localhost html]# vim index.html  ##修改網頁信息

</head>
<body>
<h1>Welcome to nginx!</h1>
<img src="test.jpg"/>  ##加入圖片到網頁中

blob.png

三、修改配置文件信息,添加緩存時間

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf   ##修改配置文件events {
        worker_connections  1024;
}
        user nginx nginx;     ##修改Nginx用戶和組

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~\.(gif|jepg|jpg|ico|bmp|png)$ {     ##支持圖片格式
        root html;     ##站點
        expires 1d;   ##緩存一天
        }
[root@localhost html]# service nginx stop   ##關閉開啓服務[root@localhost html]# service nginx start

四、訪問網頁,使用fiddler查看緩存

Nginx優化——隱藏版本號,網頁緩存,日誌分割(一)


nginx的日誌分割

  • 隨着Nginx運行時間增長,日誌也會增長。爲了方便掌握Nginx運行狀態,須要時刻關注日誌文件

  • 太大的日誌文件對監控是一個大災難按期進行日誌文件的切割

  • Nginx自身不具有日誌分割處理的功能,但能夠經過Nginx信號控制功能的腳本實現日誌的自動切割,並經過Linux的計劃任務週期性的進行日誌切割

一、編寫日誌分割腳本文件

[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"    ##pid的路徑
[ -d $logs_path ] || mkdir -p $logs_path  ##沒有目錄則建立目錄
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
##原有日誌文件生成到新路徑下
kill -USR1 $(cat $pid_path)  ##結束從新生成新的pid文件
find $logs_path -mtime +30 | xargs rm -rf  ##刪除30天前的日誌文件

[root@localhost ~]# chmod +x fenge.sh  ##給執行權限
[root@localhost ~]# ./fenge.sh     ##執行腳本文件

二、查看日誌分割狀況

[root@localhost ~]# cd /var/log/nginx/   ##切換到Nginx的日誌目錄下
[root@localhost nginx]# lstest.com-access.log-20191112
[root@localhost nginx]# date -s 2019-11-14  ##修改日期爲明天的時間
2019年 11月 14日 星期四 00:00:00 CST
[root@localhost nginx]# cd ~
[root@localhost ~]# ./fenge.sh     ##從新執行腳本
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ls           ##查看日誌分割日誌文件
test.com-access.log-20191112  test.com-access.log-20191113

三、設置週期性計劃任務,進行按期分割

[root@localhost nginx]# crontab -e   ##週期性計劃任務
0 1 * * * /opt/fenge.sh


更多nginx優化狀態統計、訪問控制等,進我主頁查看

相關文章
相關標籤/搜索