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的主配置文件。
修改主配置文件的方式如下:

將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

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工具抓包,查看響應報文,沒有圖片的緩存信息。
在這裏插入圖片描述
Nginx服務優化(隱藏版本號、修改用戶和組、設置鏈接超時)
(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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章