Web伺服器叢集--Nginx企業級優化(隐藏/修改版本号,修改使用者與組,緩存時間,日志切割,網頁壓縮)與防盜鍊
- 一:Nginx服務優化
-
- 1.1:配置Nginx隐藏版本号
-
- 1.1.1:配置 Nginx隐藏版本号–修改配置檔案法
- 1.1.2:配置 Nginx隐藏版本号–修改源碼法
- 1.1.3:檢視版本号的指令
- 1.1.4:實驗流程
- 1.2:修改Nginx使用者與組
-
- 1.2.1:編譯安裝時指定
- 1.2.2:修改配置檔案時指定使用者與組
- 1.2.3:實驗流程
- 1.3:優化Nginx網頁緩存時間
-
- 1.3.1:實驗流程
- 1.4:實作Nginx日志分割
-
- 1.4.1:編寫腳本進行日志切割的思路
- 1.5:配置Nginx實作連接配接逾時
- 二:Nginx深入優化
-
- 2.1:更改Nginx運作程序數
- 2.2:如何配置Nginx實作網頁壓縮功能
-
- 2.2.1:壓縮功能參數
- 2.2.2:優化網頁壓縮配置
- 2.3:防盜鍊
-
- 2.3.1:防盜鍊概述
- 3.3.2:盜鍊配置
- 2.3.3:防盜鍊配置
- 2.3.4:配置說明
- 2.4:對FPM子產品進行參數優化
-
- 2.4.1:FPM子產品概述
- 2.4.2:FPM優化參數講解
- 2.4.3:FPM優化參數執行個體
一:Nginx服務優化
1.1:配置Nginx隐藏版本号
- 在生産環境中,需要隐藏Ngnx的版本号,以避免安全漏洞的洩漏
-
檢視方法
使用fdde工具在 Windows用戶端檢視 Nginx版本号
在 Centos系統中使用“curl -l 網址”指令檢視N
-
nginx隐藏版本号的方法
修改配置檔案法
修改源碼法
1.1.1:配置 Nginx隐藏版本号–修改配置檔案法
- Nginx的配置檔案中的 server_tokens選項的值設定為off
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; '添加'
}
- 重新開機服務,通路網站使用curl -l 指令檢測
[[email protected] conf]# systemctl restart nginx
[[email protected] conf]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx
- 使用PHP處理動态網頁
1.若php配置檔案中配置了 fastcgi_ param SERVER SOFTWARE選項
2.則編輯 php-fpm配置檔案,将 fastcgi_param SERVER SOFTWARE對應的值修改為fastcgi_param SERVER_SOFTWARE nginx;
1.1.2:配置 Nginx隐藏版本号–修改源碼法
- Nginx源碼檔案/usr/src/ nginx-1.12.2/src/ core/nginx. h包含了版本資訊,可以随意設定
- 重新編譯安裝,隐藏版本資訊
-
示例:
#define NGINX_VERSION “1.1.1″,修改版本号為1.1.1
#define NGINX_VER “IIS/",修改軟體類型為lls
vim /opt/nginx-1.12.0/src/core/nginx.h
#define nginx version 1012000
#define NGINX VERSION "1.1.1" '修改版本号'
#define NGINX VER "IIS/" NGINX_VERSION
重新編譯,make && make install
1.1.3:檢視版本号的指令
1.1.4:實驗流程
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
tar
[[email protected] opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin 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.2]# make && make install
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig:- 99 20
# description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
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 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[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
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 10 Aug 2020 07:07:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:02:56 GMT
Connection: keep-alive
ETag: "5f30f120-264"
Accept-Ranges: bytes
方法一配置
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[[email protected] conf]# vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; '添加這行,關閉版本号'
[[email protected] conf]# systemctl restart nginx
[[email protected] conf]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx '版本号已被隐藏'
Date: Mon, 10 Aug 2020 07:12:03 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:02:56 GMT
Connection: keep-alive
ETag: "5f30f120-264"
Accept-Ranges: bytes
1.2:修改Nginx使用者與組
- Nginx運作時程序需要有使用者與組的支援,以實作對網站檔案讀取時進行通路控制
- Nginx預設使用 nobody使用者賬号與組賬号,一般也要進行修改
-
修改的方法
編譯安裝時指定使用者與組
修改配置檔案時指定使用者與組
1.2.1:編譯安裝時指定
- 建立使用者賬号與組賬号,如 nginx
- 在編譯安裝時–user與- -group指定Nginx服務的運作使用者與組賬号
1.2.2:修改配置檔案時指定使用者與組
- 建立使用者賬号,如 nginx
- 修改主配置檔案user選項,指定使用者賬号
- 重新開機 nginx服務,使配置生效
- 使用 ps aux指令檢視nginx的程序資訊,驗證運作使用者賬号改變效果
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; '#注釋掉,修改使用者為nginx,組為nginx'
service nginx restart
ps aux |grep nginx
root 130034 0.0 0.0 20220 620 ? Ss 19:41 0:00 nginx:master process
/usr/local/sbin/nginx '主程序由root建立'
nginx 130035 0.0 0.0 20664 1512? S 19:41 0:00 nginx:worker process '子程序由nginx建立'
1.2.3:實驗流程
cd /usr/local/nginx/conf
[[email protected] conf]# id nobody
uid=99(nobody) gid=99(nobody) 組=99(nobody)
[[email protected] conf]# vim nginx.conf
user nginx nginx; '#注釋掉,将nobody改成nginx'
[[email protected] conf]# 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] conf]# systemctl restart nginx
[[email protected] conf]# ps aux | grep nginx
root 11947 0.0 0.0 20544 608 ? Ss 15:18 0:00 nginx: master process /usr/localnginx/sbin/nginx
nginx 11948 0.0 0.0 23072 1384 ? S 15:18 0:00 nginx: worker process
root 11950 0.0 0.0 112724 988 pts/1 S+ 15:18 0:00 grep --color=auto nginx
1.3:優化Nginx網頁緩存時間
- 當Nginx将網頁資料傳回給用戶端後,可設定緩存的時間,以友善在日後進行相同内容的請求時直接傳回,避免重複請求,加快了通路速度
- 一般針對靜态網頁設定,對動态網頁不設定緩存時間
- 可在 Windows用戶端中使用 fiddler檢視網頁緩存時間
-
設定方法
可修改配置檔案,在http段、或者 server段、或者 location段加入對特定内容的過期參數
-
示例
修改 Nginx的配置檔案,在 location段加入 expires參數
[[email protected] conf]# vim nginx.conf
location ~\.(gif|jpg|jpeg|png|bmp|ico)$ { '加入新的location'
root html;
expires 1d; '指定緩存時間為一天'
}
1.3.1:實驗流程
[[email protected] conf]# vim nginx.conf
location ~\.(gif|jpg|jpeg|png|bmp|ico)$ {
root html;
expires 1d;
}
[[email protected] conf]# cd ..
[[email protected] nginx]# cd html/
[[email protected] html]# rz -E
rz waiting to receive.
[[email protected] html]# ls
50x.html chiji.jpg index.html
[[email protected] html]# vim index.html
<img src="chiji.jpg"\>
[[email protected] html]# systemctl restart nginx
[[email protected] html]# nginx

1.4:實作Nginx日志分割
- 随着 Nginx運作時間增加,日志也會增加。為了友善掌握 Nginx運作狀态,需要時刻關注Ngnx日志檔案
-
太大的日志檔案對監控是一個大災難
定期進行日志檔案的切割
- Nginx自身不具備日志分割處理的功能,但可以通過Nginx信号控制功能的腳本實作日志的自動切割,并通過Lnux的計劃任務周期性地進行日志切割
1.4.1:編寫腳本進行日志切割的思路
- 設定時間變量
- 設定儲存日志路徑
- 将目前的日志檔案進行重命名
- 删除時間過長的日志檔案
- 設定cron任務,定期執行腳本自動進行日志分割
[[email protected] html]# 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"
#'自動建立日志目錄'
[ -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)
#'删除30天前的日志'
find $logs_path -mtime +30 | xargs rm -rf
chmod +x fenge.sh
crontab -e '//設定周期性任務'
0 1 * * * /opt/fenge.sh
----date -d +1(second minute hour day month year)--
--------kill -QUIT 5410 結束程序 -HUP 平滑重新開機 類似 reload -USRl 日志分隔 -USR2平滑更新-----
date -d "-1 day" "+%Y%m%d" '//##時間向前推進一天'
date -s 2020-08-11 '//##時間向後推移一天'
1.5:配置Nginx實作連接配接逾時
- 在企業網站中,為了避免同一個客戶長時間占用連接配接,造成資源浪費,可設定相應的連接配接逾時參數,實作控制連接配接訪向問時間
- 使用 Fiddler工具檢視 connection參數
- 逾時參數講解
- Keepalive_timeout
- 設定連接配接保持逾時時間,一般可隻設定該參數,預設為75秒,可根據網站的情況設定,或者關閉,可在http段、 server段、或者 location段設定
- Client_header_timeout
- 指定等待用戶端發送請求的逾時時間
- Client_body_timeout
- 設定請求體讀逾時時間
- Keepalive_timeout
vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 100; '連接配接逾時時間 100'
Client_header_timeout 80; '等待用戶端發送請求的逾時時間,逾時會發送408錯誤'
Client_body_timeout 80; '設定用戶端發送請求逾時時間'
二:Nginx深入優化
2.1:更改Nginx運作程序數
- 在高并發場景,需要啟動更多的 Nginx程序以保證快速響應,以處理使用者的請求,避免造成阻塞
- 可以使用 ps auxi指令檢視Ngnx運作程序的個數
-
更改程序數的配置方法
修改配置檔案,修改程序配置參數
-
修改配置檔案的 worker_ processes參數
一般設為CPU的個數或者核數
在高并發情況下可設定為CPU個數或者核數的2倍
- 運作程序數多一些,響應通路請求時, Nginx就不會臨時啟動新的程序提供服務,減少了系統的開銷,提升了服務速度
- 使用 ps aux檢視運作程序數的變化情況
- 預設情況, Nginx的多個程序可能跑在一個cPU上,可以配置設定不同的程序給不同的CPU處理,充分利用硬體多核多CPU
-
在一台4核實體伺服器,可進行以下配置,将程序進行配置設定
Worker_cpu_affinity 0001 0010 0100 1000 ‘//核心數的序列位置’
[[email protected] opt]# cat /proc/cpuinfo | grep -c "physical" cpu核數
8
[[email protected] opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 8; '建議修改為核數相同,不要超過核數'
[[email protected] opt]# systemctl restart nginx
[[email protected] opt]# ps aux | grep nginx '一個主程序中包含一個子程序'
work_cpu_affinity 01 10; '設定每個程序由不同CPU處理'
2.2:如何配置Nginx實作網頁壓縮功能
- Nginx的ngx_htto_gzip_ module壓縮子產品提供對檔案内容壓縮的功能
- 允許Nginx伺服器将輸出内容在發送用戶端之前進行壓縮,以節約網站帶寬,提升使用者的通路體驗,預設已經安裝
- 可在配置檔案中加入相應的壓縮功能參數對壓縮性能進行優化
2.2.1:壓縮功能參數
- gzip on:開啟gzip壓縮輸出g
- zip_min_length 1k:用于設定允許壓縮的頁面最小位元組數
- gzip_buffers 4 16k:表示申請4個機關為16k的記憶體作為壓縮結果流緩存,預設值是申請與原始資料大小相同的記憶體空間來存儲gzip壓縮結果(buffers:緩存區)
- zip_http_version1.0:用于設定識别htt協定版本,預設是1.1,目前大部分浏覽器已經支援gzip解壓,但處理最慢,也比較消耗伺服器CPU資源
- gzip_comp_level2:用來指定gzp縮比,1壓縮比最小,處理速度最快;9壓縮比最大,傳輸速度快,但處理速度最慢,使用預設即可
- gzip_types text/plain:壓縮類型,是就對哪些網頁文檔啟用壓縮功能
- gzip_vary on:選項可以讓前端的緩存伺服器緩存經過gzi壓縮的頁面
- 将以上的壓縮功能參數加入到主配置檔案httpd配置中
- 重新開機服務,并用 Fiddler工具檢視開啟結果
2.2.2:優化網頁壓縮配置
[[email protected] opt]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/g if application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE[1-6]\.";
gzip_vary on;
[[email protected] opt]# systemctl restart nginx
2.3:防盜鍊
2.3.1:防盜鍊概述
- 在企業網站服務中,一般都要配置防盜鍊功能,以避免網站内容被非法盜用,造成經濟損失
- Nginx防盜鍊功能也非常強大。預設情況下,隻需要進行簡單的配置,即可實作防盜鍊處理
3.3.2:盜鍊配置
源主機
tar zxvf nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel -y
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id 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.2]# make && make install
[root@yuan nginx-1.12.2]# vim /etc/init.d/nginx
[root@yuan nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig:- 99 20
# description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
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 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[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
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@yuan nginx-1.12.2]# cd /usr/local/nginx/html/
[[email protected] html]# vim index.html
<img src="chiji.jpg"\>
[[email protected] html]# service nginx stop
[[email protected] html]# service nginx start
盜鍊網站
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install httpd -y
[[email protected] yum.repos.d]# vim /etc/httpd/conf/httpd.conf
Listen 20.0.0.51:80
#Listen 80
ServerName www.test.com:80
[[email protected] yum.repos.d]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# vim index.html
<h1>this is test web</h1>
<img src="http://www.kevin.com/chiji.jpg"\>
[[email protected] html]# echo "nameserver 20.0.0.47" > /etc/resolv.conf
[[email protected] html]# systemctl start httpd.service
2.3.3:防盜鍊配置
源主機
[[email protected] html]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
valid_referers none blocked *.kevin.com kevin.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.kevin.com/error.png;
}
}
[[email protected] html]# service nginx stop
[[email protected] html]# service nginx start
- 測試機測試
Web伺服器叢集--Nginx企業級優化(隐藏/修改版本号,修改使用者與組,緩存時間,日志切割,網頁壓縮)與防盜鍊一:Nginx服務優化二:Nginx深入優化
2.3.4:配置說明
- valid_referers:設定信任的網站,即能引用相應圖檔的網站
- none:浏覽器中 Referer為空的情況,就是直接在浏覽器通路圖檔
- blocked:浏覽器中referer不為空的情況,但是值被代理或防火牆删除了,這些值不以http://或者https://開頭
- 後面的網址或者域名:referer中包含相關字元串的網址
- if句:如果連結的來源域名不在 valid_referers所列出的清單中, $invalid_referer為1,則執行後面的操作,即進行重寫或傳回403頁面
2.4:對FPM子產品進行參數優化
2.4.1:FPM子產品概述
- Nginx的PHP解析功能實作如果是交由FPM處理的,為了提高PHP的處理速度,可對FPM子產品進行參數的調整
- FPM子產品參數調整,要根據伺服器的記憶體與服務負載進行調整
-
啟動fpm程序方式
static:将産生固定數量的fpm程序
dynamic:将以動态的方式産生fpm程序
通過pm參數指定
2.4.2:FPM優化參數講解
- Static的方式的參數
- pm.max_children:指定啟動的程序數量
- Dynamic方式的參數
- pm.max_children:指定啟動的程序數量最大的數量
- pm.start_servers:動态方式下初始的m程序數量
- pm.min_spare_servers:動态方式下最小的fpm空閉程序數
- pm.max_spare_servers:動态方式下最大的fpm空閉程序數
2.4.3:FPM優化參數執行個體
- 優化原因:
- 伺服器為雲伺服器,運作了個人論壇,記憶體為15G,fpm程序數為20,記憶體消耗近1G,處理比較慢
-
優化參數調整
FPM啟動時有5個程序,最小空閑2個程序,最大空閑8個程序,最多可以有20個程序存在
vim php.fpm.conf
pm=dynamic
pm.max_children=20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8