首頁  >  文章  >  運維  >  Nginx服務最佳化的方法

Nginx服務最佳化的方法

WBOY
WBOY轉載
2023-05-13 19:28:101068瀏覽

Nginx服務最佳化可以從隱藏版本號碼、更改使用者與群組、設定網頁快取時間、日誌切割、設定連線逾時這幾個方面進行最佳化。

1.隱藏版本號碼

在生產環境中需要隱藏Nginx的版本號,以避免洩露Nginx的版本,使×××者不能針對特定版本進行×××。查看Nginx的版本在CentOS中使用指令curl -I http://172.16.10.10/即可。

[[email protected] ~]# 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<br>

隱藏版本號有兩種方式,一種是修改Nginx的源碼文件,指定不顯示版本號,第二種是修改Nginx的主設定檔。

修改主設定檔的方式如下:

將Nginx的設定檔中的server_tokens選項值設為off,如沒有該設定項,加上即可。

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf   ...........    #省略内容     http {        include       mime.types;        default_type  application/octet-stream;        server_tokens     off;    #关闭版本号 ............    #省略内容<br>
[[email protected] ~]# 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<br>

再次造訪網址,只顯示Nginx,版本號碼已經隱藏。

[[email protected] ~]# service nginx restart #重新启动nginx服务 [[email protected] ~]# 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<br>

Nginx的原始碼檔案包含了版本訊息,可以隨意設置,然後重新編譯安裝,就會隱藏版本資訊。

[[email protected] ~]# vim /opt/nginx-1.12.0/src/core/nginx.h #编辑源码文件 #define NGINX_VERSION      "1.1.1"              #修改版本号 #define NGINX_VER          "IIS" NGINX_VERSION  #修改服务器类型<br>

重新編譯安裝

[[email protected] ~]# cd /opt/nginx-1.12.0/ [[email protected] nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install<br>

再次存取網址,只顯示修改後的版本資訊。

[[email protected] nginx-1.12.0]# service nginx restart      #重启nginx服务 [[email protected] 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<br>

2.修改使用者和群組

Nginx執行時期進程需要有使用者與群組的支持,以實現對網站檔案讀取時進行存取控制。主進程由root創建,子進程由指定的使用者與群組創建。 Nginx預設使用nobody用戶帳號與群組帳號,一般要修改。

(1)編譯Nginx時指定使用者與群組,就是設定nginx時,在./configure後面指定使用者與群組的參數。
[[email protected] ~]# cd /opt/nginx-1.12.0/ [[email protected] nginx-1.12.0]#./configure --prefix=/usr/local/nginx  --user=nginx    #指定用户名是nginx --group=nginx   #指定组名是nginx --with- && make && make install<br>
(2)修改Nginx設定檔nginx.conf指定使用者與群組。
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf  user nginx nginx;     #修改用户为nginx,组为nginx<br>

重啟nginx查看進程運行情況,主進程由root帳戶創建,子進程由nginx創建。

[[email protected] ~]# 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<br>

3.配置網頁快取時間

當Nginx將網頁資料回傳給客戶端後,可設定快取時間,方便日後進行相同內容請求是直接返回,避免重複請求,加快存取速度,一般只針對靜態資源進行設置,對動態網頁不用設定快取時間。操作步驟如下所示:

(1)以圖片作為快取對象,將game.jpg放到Nginx的網站目錄下。
[[email protected] ~]# cd /usr/local/nginx/html/    #Nginx的网站目录 [[email protected] html]# ls 50x.html  error.png  game.jpg  index.html  test.html<br>
(2)訪問http://172.16.10.10/game.jpg, 再用Fidder工具抓包,查看回應封包,沒有圖片的快取資訊。

Nginx服務最佳化的方法

# (3)修改設定文件,在新的location段加入expire參數,指定快取時間,1d表示一天。
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf     location ~\.(gif|jpg|jepg|png|bmp|ico)$ {    #加入新的location         root html;         expires 1d;     #指定缓存时间         }<br>
(4)重啟nginx服務,存取網址抓包,回應封包中含有Expire參數,表示快取的時間。
[email protected] ~]# service nginx restart<br>

[

Nginx服務最佳化的方法

以上是Nginx服務最佳化的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除