溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于CentOS7上的nginx系統(tǒng)優(yōu)化

發(fā)布時間:2020-04-05 12:30:48 來源:網(wǎng)絡(luò) 閱讀:12094 作者:漂移的兔子 欄目:建站服務(wù)器

基于CentOS7上的nginx系統(tǒng)優(yōu)化

隱藏版本信息

首先在CentOS7上安裝好nginx服務(wù)之后,可以查看當(dāng)前的nginx版本信息:

[root@localhost init.d]# curl -I http://192.168.234.174        //查看當(dāng)前版本信息
HTTP/1.1 200 OK
Server: nginx/1.12.0      //當(dāng)前的nginx版本信息
Date: Sat, 30 Jun 2018 06:23:15 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 30 Jun 2018 06:17:15 GMT
Connection: keep-alive
ETag: "5b37206b-264"
Accept-Ranges: bytes

為了避免版本信息泄露,從而導(dǎo)致不必要的麻煩,下面介紹兩種隱藏版本信息的方法:

  • 基于已經(jīng)安裝好nginx服務(wù)的方法

修改nginx的主配置文件

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
.....  省略
http {
    include       mime.types;
    default_type  application/octet-stream;
     server_tokens off;    //添加關(guān)閉版本顯示

重新加載nginx的配置,并且再次查看版本信息

[root@localhost init.d]# service nginx reload    //重新加載nginx的配置文件
[root@localhost init.d]# curl -I http://192.168.234.174
HTTP/1.1 200 OK
Server: nginx      //這里可以看到當(dāng)前的版本信息已經(jīng)被隱藏起來了
Date: Sat, 30 Jun 2018 06:35:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 30 Jun 2018 06:17:15 GMT
Connection: keep-alive
ETag: "5b37206b-264"
Accept-Ranges: bytes
  • 基于nginx服務(wù)尚未安裝的方法

    首先修改nginx的源代碼,使別人誤認(rèn)為我們使用的是別的版本

[root@localhost init.d]# vim /opt/nginx-1.12.0/src/core/nginx.h    //修改源代碼包

... ...省略
#define nginx_version      1012000
#define NGINX_VERSION      "1.12.0"  //修改為1.1.1

然后進(jìn)行編譯安裝

[root@localhost init.d]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \ 
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module        //編譯安裝

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

[root@localhost conf]# vim nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
        server_tokens on;     //開啟顯示版本信息

[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start     //重新啟動nginx服務(wù)
[root@localhost conf]# curl -I http://192.168.234.174
HTTP/1.1 200 OK
Server: nginx/1.1.1      //可以看到nginx的版本信息就被篡改了
Date: Sat, 30 Jun 2018 07:03:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 30 Jun 2018 06:17:15 GMT
Connection: keep-alive
ETag: "5b37206b-264"
Accept-Ranges: bytes

修改用戶和組

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

#user  nobody;    //nobody修改為nginx 
nginx;

修改緩存時間

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
... ... 省略
 location / {
            root   html;
            index  index.html index.htm;
        }      //在下面添加
        location ~\.(gif|jpg|jepg|png|bmp|ico)$ {
            root html;
            expires 1d;
        }

[root@localhost conf]# cd /usr/local/nginx/html/
[root@localhost html]# cp /abc/Apache/ai.jpg /usr/local/nginx/html/   //復(fù)制一張圖片到html站點(diǎn)的目錄下

[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start    //重啟nginx服務(wù)

然后此時使用一臺安裝了fiddler工具的win7客戶機(jī)去訪問nginx服務(wù)器

基于CentOS7上的nginx系統(tǒng)優(yōu)化

基于CentOS7上的nginx系統(tǒng)優(yōu)化

然后就可以看到這里圖片的緩存時間已經(jīng)被修改為一天了

日志分割

[root@localhost ~]# vim /opt/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"    #日志的進(jìn)程序列號
[ -d $logs_path ] || mkdir -p $logs_path 
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
#將訪問日志移動到根據(jù)日期每天生成不同的日志文件
kill -USR1 $(cat $pid_path)   #中斷日志文件的創(chuàng)建,方便下一次在依次剪切移動
find $logs_path -mtime +30 | xargs rm -rf          #將30天之前的日志文件刪除

[root@localhost opt]# chmod +x fenge.sh     //給與日志分割腳本一個執(zhí)行權(quán)限
[root@localhost opt]# ./fenge.sh     //執(zhí)行腳本
[root@localhost opt]# cd /var/log/nginx/   //查看nginx的日志文件
[root@localhost nginx]# ls
test.com-access.log-20180629      //這里就會產(chǎn)生一個前一天的日志文件

這里還可以添加為周期性計(jì)劃任務(wù)

[root@localhost nginx]# crontab -e
0 1 * * * /opt/fenge.sh   //這樣日志分割任務(wù)就會周期性的生成,就不需要我們每天都手動執(zhí)行一遍腳本了

連接超時

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
... ... 省略
    #keepalive_timeout  0;
    keepalive_timeout  65;   //刪除此行,并在下面添加

    keepalive_timeout  65 180;
    client_header_timeout 80;
    client_body_timeout 80;

[root@localhost nginx]# nginx -t   //檢查語法

[root@localhost nginx]# service nginx stop 
[root@localhost nginx]# service nginx start   //重啟nginxfuwu

然后此時同樣使用裝有一臺安裝了fiddler工具的win7客戶機(jī)去訪問nginx服務(wù)器

基于CentOS7上的nginx系統(tǒng)優(yōu)化

基于CentOS7上的nginx系統(tǒng)優(yōu)化

防盜鏈

首先在進(jìn)行防盜鏈配置時,我們要先準(zhǔn)備兩臺win7客戶機(jī)(網(wǎng)卡模式是NAT,IP地址自動獲取),一臺(win7)進(jìn)行盜鏈操作,另一臺(win7-1)做訪問端

開始進(jìn)行盜用鏈接配置,首先盜鏈端(win7)開啟IIS服務(wù),并且寫入一個首頁內(nèi)容

<html>
<head></head>
 <body>
   <h2>this is test!!!</h2>    //首頁內(nèi)容
   <img src="http://www.benet.com/game.jpg">   //進(jìn)行盜鏈的網(wǎng)站和圖片
</body>
</html>

然后在CentOS7上安裝DNS服務(wù),并且修改配置

[root@localhost nginx]# yum install bind -y

[root@localhost nginx]# vim /etc/named.conf   //修改主配置文件

listen-on port 53 { 127.0.0.1; };    //127.0.0.1修改為any

allow-query     { localhost; };    //localhost修改為any

[root@localhost nginx]# vim /etc/named.rfc1912.zones   //修改區(qū)域配置文件
//添加下面的配置
zone "benet.com" IN {
        type master;
        file "benet.com.zone";
        allow-update { none; };
};

zone "test.com" IN {
        type master;
        file "test.com.zone";
        allow-update { none; };
};

[root@localhost nginx]# cd /var/named/
[root@localhost named]# cp -p named.localhost benet.com.zone
[root@localhost named]# vim benet.com.zone  //修改區(qū)域數(shù)據(jù)庫文件
刪除末行,添加
www  IN A 192.168.234.174  //解析指向nginx服務(wù)器的IP

[root@bogon named]# cp -p benet.com.zone test.com.zone
[root@localhost named]# vim test.com.zone
修改末行的www 后的解析地址,指向盜鏈服務(wù)器的IP,即
www  IN A  192.168.234.180 //這里是win7的IP

這里盜鏈的操作就完成了,我們可以看下效果

訪問www.test.com

基于CentOS7上的nginx系統(tǒng)優(yōu)化

訪問www.benet.com

基于CentOS7上的nginx系統(tǒng)優(yōu)化

接下來配置防盜鏈配置

[root@bogon html]# vim /usr/local/nginx/conf/nginx.conf
... ...省略     添加//
location ~*\.(jpg|gif|swf)$ {
            valid_referers none blocked *.benet.com benet.com;
            if ( $invalid_referer ) {
               rewrite ^/ http://www.benet.com/error.png;
            }
        }

[root@bogon named]# cd /usr/local/nginx/html/
[root@bogon html]# cp /abc/LNMP/error.png ./    //添加一張重定向的圖片
[root@bogon html]# service nginx stop
[root@bogon html]# service nginx start    //重啟nginx服務(wù)

防盜鏈操作完成后,我們在來使用win7-1客戶機(jī)訪問查看效果(訪問前先清空緩存):

訪問www.test.com

基于CentOS7上的nginx系統(tǒng)優(yōu)化

訪問www.benet.com

基于CentOS7上的nginx系統(tǒng)優(yōu)化

這樣防盜鏈就完成了。

以上就是CentOS7上nginx的所有優(yōu)化配置了,請各位看官多多點(diǎn)評與點(diǎn)贊?。?!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI