Nginx之隱藏版本號,優(yōu)化緩存,日志分割-創(chuàng)新互聯(lián)

Nginx之隱藏版本號,優(yōu)化緩存,日志分割

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)烏拉特中免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

nginx之隱藏版本號

配置nginx

[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安裝環(huán)境包

[root@localhost ~]# useradd -M -s /sbin/nologin nginx  ##創(chuàng)建程序性用戶

[root@localhost ~]# mkdir /chen  ##創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mount.cifs //192.168.100.23/LNMP /chen  ##掛載
Password for root@//192.168.100.23/LNMP:  

[root@localhost chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/  ##解壓

[root@localhost chen]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

./configure \  ##安裝nginx組件
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

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

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##做軟鏈接讓系統(tǒng)能識別nginx的所有人命令
[root@localhost nginx-1.12.2]# 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

寫nginx腳本放在系統(tǒng)啟動腳本中方便service管理器管理

[root@localhost nginx-1.12.2]# cd /etc/init.d/ ##到系統(tǒng)啟動腳本

[root@localhost init.d]# vim nginx   ##寫一個nginx腳本

#!/bin/bash
#chkconfig: - 99 20  #注釋信息
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"  #這個變量,指向我的命令文件
PIDF="/usr/local/nginx/logs/nginx.pid"  #這個變量,指向nginx的進(jìn)程號
case "$1" in
    start)
        $PROG                                              
        ;;
    stop)
        kill -s QUIT $(cat $PIDF) 
        ;;
    restart)                                                  
        $0 stop
        $0 start
        ;;
    reload)                                                  
        kill -s HUP $(cat $PIDF)
        ;;
    *)                                                           
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost init.d]# chmod +x nginx  ##給Nginx提升權(quán)限
[root@localhost init.d]# chkconfig --add nginx  ##添加nginx
[root@localhost init.d]# service nginx start 
[root@localhost init.d]# netstat -ntap | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17544/nginx: master 

[root@localhost init.d]# systemctl stop firewalld.service
[root@localhost init.d]# setenforce 0

檢查現(xiàn)在我們nginx的版本號

[root@localhost ~]# curl -I http://192.168.136.163/
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 05 Nov 2019 07:30:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 05 Nov 2019 07:13:26 GMT
Connection: keep-alive
ETag: "5dc12116-264"
Accept-Ranges: bytes

關(guān)閉版本號再檢查一下

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

http {  ##在http級別下添加
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;  ##關(guān)閉版本號

[root@localhost init.d]# service nginx stop  ##關(guān)閉服務(wù)
[root@localhost init.d]# service nginx start  ##開啟服務(wù)
[root@localhost init.d]# curl -I http://192.168.136.163/  ##查看Nginx信息
HTTP/1.1 200 OK      
Server: nginx            ##版本號被隱藏了
Date: Tue, 12 Nov 2019 14:22:00 GMT
Content-Type: text/html
Content-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 {
        include       mime.types;
        default_type  application/octet-stream;
        server_tokens on;   ##開啟版本號
修改Nginx源碼包文件
[root@localhost init.d]# cd /opt/nginx-1.12.2/src/core/  ##切換到src源碼包目錄
[root@localhost core]# vim nginx.h  ##修改文件

#define NGINX_VERSION      "1.1.2"  ##此處版本號偽造成1.1.2
重新編譯安裝
[root@localhost core]# cd /opt/nginx-1.12.2/    ##到Nginx下
[root@localhost nginx-1.12.2]# ./configure \     ##重新安裝組件
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make     ##重新編譯
...
[root@localhost nginx-1.12.0]# make install   ##重新安裝
...
重啟Nginx服務(wù),查看版本信息
[root@localhost nginx-1.12.2]# service nginx stop  ##關(guān)閉
[root@localhost nginx-1.12.2]# service nginx start  ##開啟
[root@localhost nginx-1.12.2]# curl -I http://192.168.136.163/   ##查看Nginx信息
HTTP/1.1 200 OK 
Server: nginx/1.1.2       ##此時的版本號就是偽造的版本號
Date: Tue, 12 Nov 2019 14:34:02 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT
Connection: keep-alive
ETag: "5dcab7bb-264"
Accept-Ranges: bytes

Nginx優(yōu)化之緩存時間

當(dāng)Nginx將網(wǎng)頁數(shù)據(jù)返回給客戶端后,可設(shè)置緩存時間,以方便在日后進(jìn)行相同內(nèi)容的請求時直接返回,避免重復(fù)請求,加快了訪問速度
一般針對靜態(tài)網(wǎng)頁設(shè)置,對動態(tài)網(wǎng)頁不設(shè)置緩存時間
可在Windows客戶端中使用fiddler查看網(wǎng)頁緩存時間

我們先準(zhǔn)備一張圖片在宿主機(jī)的共享目錄中

把圖片放到nginx的站點(diǎn)中

[root@localhost ~]# cd /chen/
[root@localhost chen]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.20.tar.gz
fang.png                   nginx-1.12.2.tar.gz  shu.jpg
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2
[root@localhost chen]# cp shu.jpg /usr/local/nginx/html/
[root@localhost chen]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html  shu.jpg

[root@localhost html]# vim index.html  ##進(jìn)入網(wǎng)頁做個圖片的路徑

 15 <img src="shu.jpg"/> ##在第15行添加圖片

設(shè)置緩存時間

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
##在server服務(wù)器級別
76         location ~\.(gif|jepg|jpg|icp|bmp|png)$ { ##加入能使別這些圖片格式
 77             root html;  ##管理員頁面
 78             expires 1d;  ##緩存時間1天

 16   user nginx nginx; ##16行加入nginx用戶和組
[root@localhost html]# service nginx start

客戶機(jī)去測試一下訪問網(wǎng)站

Nginx之隱藏版本號,優(yōu)化緩存,日志分割

用抓包工具看一下緩存時間

Nginx之隱藏版本號,優(yōu)化緩存,日志分割

Nginx之日志切割

隨著Nginx運(yùn)行時間增加,日志也會增加。為了方便掌握Nginx運(yùn)行狀態(tài),需要時刻關(guān)注日志文件
太大的日志文件對監(jiān)控是一個大災(zāi)難
定期進(jìn)行日志文件的切割
Nginx自身不具備日志分割處理的功能,但可以通過Nginx信號控制功能的腳本實現(xiàn)日志的自動切割,并通過Linux的計劃任務(wù)周期性的進(jìn)行日志切割

編寫日志分割腳本文件

[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  ##沒有目錄則創(chuàng)建目錄
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d

原有日志文件生成到新路徑下

kill -USR1 $(cat $pid_path)  ##結(jié)束重新生成新的pid文件
find $logs_path -mtime +30 | xargs rm -rf  ##刪除30天前的日志文件

[root@localhost ~]# chmod +x fenge.sh  ##給執(zhí)行權(quán)限
[root@localhost ~]# ./fenge.sh     ##執(zhí)行腳本文件

查看日志分割情況

[root@localhost ~]# cd /var/log/nginx/   ##切換到Nginx的日志目錄下
[root@localhost nginx]# ls
test.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     ##重新執(zhí)行腳本
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ls           ##查看日志分割日志文件
test.com-access.log-20191112  test.com-access.log-20191113

設(shè)置周期性計劃任務(wù)

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

以上就是我們?nèi)康膬?nèi)容了,謝謝收看

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章標(biāo)題:Nginx之隱藏版本號,優(yōu)化緩存,日志分割-創(chuàng)新互聯(lián)
分享路徑:http://bm7419.com/article30/dihpso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、小程序開發(fā)、電子商務(wù)定制網(wǎng)站、搜索引擎優(yōu)化、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)