Nginx服務優化
本篇重點
nginx版本隱藏
nginx版本號僞裝
nginx超時時間管理
nginx進程管理
nginx日誌分割
實驗前提:
手工編譯安裝完成Nginx服務
一、nginx版本隱藏
nginx服務是默認顯示版本號的,
[root@localhost nginx]# curl -I http://192.168.142.128
HTTP/1.1 200 OK
Server: nginx/1.12.2 //默認是顯示版本號的
Date: Wed, 13 Nov 2019 08:11:41 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 13 Nov 2019 08:04:45 GMT
Connection: keep-alive
ETag: "5dcbb91d-264"
Accept-Ranges: bytes
這樣就給人家看到你的服務器nginx版本是1.12.2
,這樣暴露出來的版本號就容易變成
攻撃する者可利用的信息。所以,從安全的角度來說,隱藏版本號會相對安全些!
下面就開始隱藏版本號的步驟:
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# vim conf/nginx.conf //主配置文件
在server段後面空白處,添加
server_tokens off;
wq保存退出
[root@localhost nginx]# service nginx stop //重啓服務
[root@localhost nginx]# service nginx start
現在,重啓服務後,再次查看版本號
[root@localhost nginx]# curl -I http://192.168.142.128
HTTP/1.1 200 OK
Server: nginx //版本號已隱藏
Date: Wed, 13 Nov 2019 08:20:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 13 Nov 2019 08:04:45 GMT
Connection: keep-alive
ETag: "5dcbb91d-264"
Accept-Ranges: bytes
二、nginx版本號僞裝
除了將版本號進行隱藏,我們還可以反將一波,可以給意圖不軌者一個錯誤的版本號,修改nginx.h
wen件 (缺點:需要將nginx重新進行編譯安裝)
[root@localhost nginx]# cd /opt/nginx-1.12.2/src/core/
修改該目錄下源碼文件nginx.h
[root@localhost core]# vim nginx.h
#define NGINX_VERSION "9.9.9"
修改僞裝版本號
將nginx進行重新編譯安裝
[root@localhost core]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@localhost nginx-1.12.2]# make && make install //重新進行編譯安裝
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# service nginx stop //重啓服務
[root@localhost conf]# service nginx start
檢查是否僞裝成功(確保主配置文件中server_tokens on;
)
[root@localhost conf]# curl -I http://192.168.142.128
HTTP/1.1 200 OK
Server: nginx/9.9.9 //僞裝成功
Date: Wed, 13 Nov 2019 08:38:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 13 Nov 2019 08:04:45 GMT
Connection: keep-alive
ETag: "5dcbb91d-264"
Accept-Ranges: bytes
三、nginx超時時間管理
爲了保證資源的最有效的利用,不被長時間不進行操作的用戶所佔用,因此就需要進行超時時間的管理。
修改主配置文件
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
找到'keepalive_timeout',修改並在下面添加
keepalive_timeout 65 180; #前後分別爲服務器端超時時間,客戶端超時時間。
client_header_timeout 80; #等待客戶端發送請求頭的超時時間
client_body_timeout 70; #客戶端發請求體超時時間
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
四、nginx進程管理
通常默認情況下,nginx的運行進程(worker_processes
)僅爲1
[root@localhost conf]# ps aux | grep nginx
root 43055 0.0 0.0 20540 608 ? Ss 17:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx //主進程不可改變
nginx 43056 0.0 0.0 23064 1380 ? S 17:13 0:00 nginx: worker process //工作進程可以根據具體情況手動進行更改
root 43189 0.0 0.0 112728 968 pts/1 S+ 17:25 0:00 grep --color=auto nginx
爲了給有多核處理器的服務器有更高的處理效率,我們需要對進程進行修改(本次實驗環境爲2核服務器)
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
搜索'worker_processes',並添加
worker_processes 2; #修改爲與cpu數量相同
worker_cpu_affinity 01 10; #設置每個進程由不同cpu處理
[root@localhost conf]# service nginx stop #重啓服務
[root@localhost conf]# service nginx start
此時,我們再次查看nginx的進程(會擁有兩條工作進程)
[root@localhost conf]# ps aux | grep nginx
root 43353 0.0 0.0 20540 604 ? Ss 17:36 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 43354 0.0 0.0 23064 1372 ? S 17:36 0:00 nginx: worker process
nginx 43355 0.0 0.0 23064 1364 ? S 17:36 0:00 nginx: worker process
root 43367 0.0 0.0 112728 972 pts/1 S+ 17:37 0:00 grep --color=auto nginx
五、nginx日誌分割
每天都有大量的日誌,日積月累,查詢累死人怎麼辦?放心!日誌分割來幫你。
所需只有一個腳本和一份計劃任務:
腳本
[root@localhost nginx]# vim fenge.sh
#!/bin/bash
#每隔一天進行一次分割,每隔20天清理一次日誌文件
pid="/usr/local/nginx/logs/nginx.pid"
log="/usr/local/nginx/logs/access.log"
time=`date -d "-1 day" "+%Y%m%d"`
[ -d "/var/log/nginx" ] || mkdir -p /var/log/nginx
mv $log /var/log/nginx/access.log-$time #建立分割日誌
pidhao=`cat $pid`
kill -USR1 $pidhao #日誌分割
find /var/log/nginx -mtime +20 | xargs rm -rf #清理20天前的日誌文件
計劃任務
[root@localhost nginx]# crontab -e
添加
* * */1 * * /usr/local/nginx/fenge.sh //每隔一天執行