Nginx服務優化及優化深入
Ngnix服務的安裝詳細介紹請參考 部署Nginx網站服務實現訪問狀態統計以及訪問控制功能
-
Nginx服務優化
可以從隱藏版本號、更改用戶與組、配置網頁緩存時間、日誌切割、設置連接超時這幾個方面進行優化。
1.隱藏版本號
在生產環境中需要隱藏Nginx的版本號,以避免泄露Nginx的版本,使×××者不能針對特定版本進行×××。查看Nginx的版本在CentOS中使用命令curl -I http://172.16.10.10/即可。
[root@localhost ~]# curl -I http://172.16.10.10/
HTTP/1.1 200 OK
Server: nginx/1.12.0 #Nginx版本信息
Date: Fri, 29 Jun 2018 08:52:27 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes
隱藏版本號有兩種方式,一種是修改Nginx的源碼文件,指定不顯示版本號,第二種是修改Nginx的主配置文件。
(1)修改主配置文件的方式如下:
將Nginx的配置文件中的server_tokens選項值設置為off,如沒有該配置項,加上即可。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
........... #省略內容
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #關閉版本號
............ #省略內容
[root@localhost ~]# 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,版本號已經隱藏。
[root@localhost ~]# service nginx restart #重新啟動nginx服務
[root@localhost ~]# curl -I http://172.16.10.10/
HTTP/1.1 200 OK
Server: nginx #nginx隱藏了版本號
Date: Fri, 29 Jun 2018 09:09:36 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes
(2)Nginx的源碼文件包含了版本信息,可以隨意設置,然後重新編譯安裝,就會隱藏版本信息。
[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h #編輯源碼文件
#define NGINX_VERSION "1.1.1" #修改版本號
#define NGINX_VER "IIS" NGINX_VERSION #修改服務器類型
重新編譯安裝
[root@localhost ~]# 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 && make && make install
再次訪問網址,只顯示修改之後的版本信息。
[root@localhost nginx-1.12.0]# service nginx restart #重啟nginx服務
[root@localhost nginx-1.12.0]# curl -I http://172.16.10.10/HTTP/1.1 200 OK
Server: IIS1.1.1 #nginx的版本信息
Date: Fri, 29 Jun 2018 09:30:09 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes
2.修改用戶和組
Nginx運行時進程需要有用戶與組的支持,用以實現對網站文件讀取時進行訪問控制。主進程由root創建,子進程由指定的用戶與組創建。Nginx默認使用nobody用戶賬號與組賬號,一般要修改。
(1)編譯Nginx時指定用戶與組,就是配置nginx時,在./configure後面指定用戶與組的參數。
[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx
--user=nginx #指定用戶名是nginx
--group=nginx #指定組名是nginx
--with-
&& make && make install
(2)修改Nginx配置文件nginx.conf指定用戶與組。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #修改用戶為nginx,組為nginx
重啟nginx查看進程運行情況,主進程由root賬戶創建,子進程由nginx創建。
[root@localhost ~]# ps aux | grep nginx
root 14923 0.0 0.0 20540 624 ? Ss 17:30 0:00 nginx: master process /usr/local/nginx/sbin/nginx #主進程由root創建
nginx 14925 0.0 0.1 22984 1412 ? S 17:30 0:00 nginx: worker process #子進程由nginx創建
root 19344 0.0 0.0 112720 984 pts/0 R+ 17:47 0:00 grep --color=auto nginx
3.配置網頁緩存時間
當Nginx將網頁數據返回給客戶端後,可設置緩存時間,方便日後進行相同內容請求是直接返回,避免重復請求,加快訪問速度,一般只針對靜態資源進行設置,對動態網頁不用設置緩存時間。
操作步驟如下所示:
(1)以圖片作為緩存對象,將game.jpg放到Nginx的網站目錄下。
[root@localhost ~]# cd /usr/local/nginx/html/ #Nginx的網站目錄
[root@localhost html]# ls
50x.html error.png game.jpg index.html test.html
(2)訪問http://172.16.10.10/game.jpg, 再用Fidder工具抓包,查看響應報文,沒有圖片的緩存信息。
(3)修改配置文件,在新的location段加入expire參數,指定緩存時間,1d表示一天。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~\.(gif|jpg|jepg|png|bmp|ico)$ { #加入新的location
root html;
expires 1d; #指定緩存時間
}
(4)重啟nginx服務,訪問網址抓包,響應報文中含有Expire參數,表示緩存的時間。
[root@localhost ~]# service nginx restart
4.日誌切割
隨著Nginx的運行時間的增加,產生的日誌也會增加。太大的日誌文件非常不便於分析和排查,因此需要定期的進行日誌文件的切割。Nginx沒有類似Apache的cronlog日誌分割處理功能,但可以通過Nginx的信號控制功能腳本來實現日誌的自動切割,並將腳本加入到Linux的計劃任務中,讓腳本在每天的固定時間執行。
(1)首先編寫腳本/opt/fenge.sh,把Nginx的日誌文件/usr/local/nginx/logs/access.log移動到目錄/var/log/nginx下面,以當前時間作為日誌文件的名稱,然後用kill-USR1創建新的日誌文件/usr/local/nginx/logs/access.log,最後刪除前30天的日誌文件。
[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"
[ -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) #重建新的日誌文件
find $logs_path -mtime +30 | xargs rm -rf #刪除30天之前的日誌文件
(2)執行/opt/fenge.sh,測試日誌文件是否被切割。
[root@localhost ~]# chmod +x /opt/fenge.sh
[root@localhost ~]# ./fenge.sh #執行分割腳本
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20180628
(3)設置crontab任務,定期執行腳本自動進行日誌分割。
[root@localhost ~]# crontab -e
0 1 * * * /opt/fenge.sh #每天的淩晨1點執行/opt/fenge.sh腳本
5.設置連接超時
在企業網站中,為了避免同一個客戶長時間占用連接,造成資源浪費,可以設置相應的連接超時參數,實現對連接訪問的時間的控制。
(1)修改配置文件nginx.conf,設置keepalive_timeout超時時間。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
... #省略內容
http {
...
keepalive_timeout 65 180; #設置超時是180秒
client_header_timeout 80; #指定請求頭的超時時間
client_body_timeout 80; #指定請求體超時時間
... #省略內容
}
keepalive_timeout 第一個參數指定了與客戶端的keep-alive連接超時時間,服務器將會在這個時間後關閉連接;第二個參數指定了響應頭Keep-Alive:timeout=time中的time值。這個頭能讓瀏覽器主動關閉連接,這樣服務器就不必去關閉連接。
(2)重啟nginx服務,訪問網址,用Fidder工具抓包。
-
Nginx優化深入
可以從更改進程數、配置網頁壓縮、配置防盜鏈這幾個方面進行深入優化。
1.更改進程數
在高並發環境中,需要啟動更多的nginx進程以保證快速響應,用以處理用戶的請求,避免造成阻塞。使用ps aux命令查看Nginx運行的進程的個數。
[root@localhost ~]# ps aux | grep nginx
root 14923 0.0 0.0 20540 624 ? Ss 17:30 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 14925 0.0 0.1 22984 1412 ? S 17:30 0:00 nginx: worker process
root 19344 0.0 0.0 112720 984 pts/0 R+ 17:47 0:00 grep --color=auto nginx
其中master process是主進程,開啟了一個,worker process是子進程,也是開啟了一個。
(1)修改nginx的文件中的worker_process參數,一般設為CPU的個數或核數,在高並發的情況下,可設置為CPU的個數或者核數的2倍,可以先查看CPU的核數以確定參數。
[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"
2
參數設置為2,和CPU核數相同,運行進程數設置多一些,響應客戶端請求時,Nginx就不會臨時啟動新的進程提供服務,減少了系統的開銷,提升了服務的速度。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_proces 2;
修改完後,重啟服務,查看運行進程數的變化。
[root@localhost ~]# service nginx restart
[root@localhost ~]# ps aux | grep nginx
root 21453 0.0 0.0 20540 636 ? Ss 20:11 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21457 0.0 0.1 23000 1424 ? S 20:11 0:00 nginx: worker process
nginx 21458 0.0 0.1 23000 1424 ? S 20:11 0:00 nginx: worker process
root 21472 0.0 0.0 112720 984 pts/0 S+ 20:11 0:00 grep --color=auto nginx
開啟了一個主進程和2個子進程,可見參數設置起了作用。
2.配置網頁壓縮
Nginx的ngx_http_gzip_module壓縮模塊提供了對文件內容壓縮的功能,以節約網站的帶寬,提升用戶的訪問體驗,默認nginx已經安裝該模塊,只需要在配置文件加入相應的壓縮功能參數對壓縮性能進行優化。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on; #開啟gzip壓縮輸出
gzip_buffers 4 64k; #表示申請4個單位為64kB的內存作為壓縮結果流緩存
gzip_http_version 1.1; #用於設置http協議版本,默認是1.1
gzip_comp_level 2; #指定gzip壓縮比,壓縮比最小,處理速度最快
gzip_min_length 1k; #設置允許壓縮的頁面最小字節數
gzip_vary on; #讓前端的緩存服務器緩存經過gzip壓縮的頁面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss text/jpg text/png; #壓縮類型
重啟服務,以大小超過1KB的html文件作為nginx網站內容,然後訪問網址抓取數據報文,顯示使用gzip進行了壓縮。
3.配置防盜鏈
在企業網站服務中,一般都要配置防盜鏈功能,以避免網站內容被非法盜用,造成經濟損失,也避免不必要的帶寬浪費。
一、防盜鏈
需要準備兩臺主機模擬盜鏈,一臺主機作為客戶端訪問盜鏈網站。
IP地址 | 域名 | 用途 | 系統 |
---|---|---|---|
172.16.10.10 | www.benet.com | 源主機 | CentOS7 |
172.16.10.20 | www.bt.com | 盜鏈主機 | CentOS7 |
172.16.10.8 | -------------- | 客戶機 | Windows7 |
(1)修改客戶機的C:\Windows\System32\drivers\etc\hosts文件,設置域名和IP映射關系。
172.16.10.10 www.benet.com
172.16.10.20 www.bt.com
(2)修改源主機和盜鏈主機的hosts文件,設置域名和IP映射關系。
[root@localhost ~]# vim /etc/hosts
172.16.10.10 www.benet.com
172.16.10.20 www.bt.com
(3)把圖片game.jpg放到源主機benet.com的站點目錄下。
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html game.jpg index.html
(4)在盜鏈主機bt.com的站點目錄下編寫盜鏈頁面test.html,盜取源主機benet.com的圖片。
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim test.html
<html>
<h1>Server 172.16.10.20</h1>
<body>
<img src="http://www.benet.com/game.jpg"/>
</body>
</html>
(5)訪問盜鏈網頁http://www.bt.com/test.html, 查看是否盜鏈成功。
在圖片上點擊右鍵選擇屬性,可以看到網址是http://www.benet.com/game.jpg。
二、配置Nginx防盜鏈
Nginx的防盜原理是加入location項,用正則表達式過濾圖片類型文件,對於信任的網址可以正常使用,對於不信任的網址則返回相應的錯誤圖片。在源主機benet.com的配置文件加入以下代碼:
[root@localhost ~]# 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;
}
}
- ~*.(jpg|gif|swf)$: 匹配不區分大小寫,以.jpg 或.gif或 .swf結尾的文件。
- valid_referers:設置信任的網站,可以正常使用圖片。
- none:瀏覽器中refer為空的情況,就是直接在瀏覽器訪問圖片。
- blocked:瀏覽器中refer不為空的情況,但是值被代理或防火墻刪除了,這些值不以http://或 https://開頭。
- 後面的網址或域名:refer包含相關字符串的網址。
- if語句:如果鏈接的來源域名不在valid_referers所列出的列表中, $invalid_referer 為1,則執行後面的操作,即進行重寫或返回403頁面。
(1)把圖片error.png放到源主機benet.com的站點目錄下。[root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# ls 50x.html error.png game.jpg index.html test.html
(2)這時重啟服務器,重新訪問http://www.test.com/test.html, 顯示的是被重寫的圖片,說明防盜鏈成功。
需要註意的是如果在配置文件中加入了新的location項去配置網頁緩存時間,那麽防盜鏈加入的location項應置於其之前,才能防盜鏈成功。
Nginx服務優化及優化深入