web網站集羣之企業級Nginx Web服務優化詳解(一)


1. 隱藏nginx版本信息優化(安全優化)

官方參考鏈接:http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens

Syntax:  server_tokens on | off | build | string;

Default: server_tokens on;(默認顯示nginx服務版本)

Context: http, server, location

實踐配置:

server {

listen       80;

server_name  www.etiantian.org;

server_tokens off;

root   html/www;

index  index.html index.htm;

}

[root@web01 conf]# curl -I www.etiantian.org

HTTP/1.1 200 OK

Server: nginx      <-- 版本號信息已經隱藏

Date: Thu, 08 Mar 2018 06:15:18 GMT

Content-Type: text/html

Content-Length: 19

Last-Modified: Mon, 05 Feb 2018 00:58:31 GMT

Connection: keep-alive

ETag: "5a77ac37-13"

Accept-Ranges: bytes


修改nginx服務名稱信息優化(安全優化)

可以通過修改程序源代碼,實現修改nginx服務名稱

第一個文件/server/tools/nginx-1.12.2/src/core/nginx.h

13#define NGINX_VERSION      "6.6.6"            #<==已修改爲想要顯示的版本號

14 #define NGINX_VER      oldboy/" NGINX_VERSION    #<==已修改爲想要顯示的名字

22 #define NGINX_VAR          oldboy"             #<==已修改爲想要顯示的名字

將以上源碼文件中三行內容進行修改

第二個文件:/server/tools/nginx-1.12.2/src/http/ngx_http_header_filter_module.c

49 static u_char ngx_http_server_string[] = "Server: oldboy" CRLF;   #<=== nginx名稱改爲想要顯示的名字

第三個文件:nginx1.xxx/src/http/ngx_http_special_response.c

改動之前配置信息

21 static u_char ngx_http_error_full_tail[] =

22 "<hr><center>" NGINX_VER "</center>" CRLF

23 "</body>" CRLF

24 "</html>" CRLF

25 ; 

改動之後配置信息

21 static u_char ngx_http_error_full_tail[] =

22 "<hr><center>" NGINX_VER "(http://oldboy.blog.51cto.com)</center>" CRLF

23 "</body>" CRLF

24 "</html>" CRLF

25 ;

改動之前配置信息:

35 static u_char ngx_http_error_tail[] =

36 "<hr><center>nginx</center>" CRLF

37 "</body>" CRLF

38 "</html>" CRLF

39 ;

改動之後配置信息:

35 static u_char ngx_http_error_tail[] =

36 "<hr><center>oldboy</center>" CRLF

37 "</body>" CRLF

38 "</html>" CRLF


3. 修改nginx軟件worker_processes進程用戶信息(安全優化)

第一種方法:

編譯安裝軟件時,指定配置參數

--user=www --group=www

第二種方法:

修改服務配置文件,實現修改worker進程用戶

官方鏈接說明:http://nginx.org/en/docs/ngx_core_module.html#user

Syntax:user user [group];

Default:user nobody nobody;

Context:main

實踐配置:

user oldboy oldboy;

worker_processes  1;

error_log  /tmp/error.log error;

[root@web01 nginx-1.12.2]# ps -ef|grep nginx

root      47630      1  0 14:48 ?        00:00:00 nginx: master process nginx

oldboy    47713  47630  0 15:01 ?        00:00:00 nginx: worker process


修改nginx軟件worker_processes進程數量(性能優化)

官方參考鏈接:http://nginx.org/en/docs/ngx_core_module.html#worker_processes

Syntax:  worker_processes number | auto;

Default: worker_processes 1;

Context: main

worker_processes進程數據建議:(一般和CPU的核數設置一致;高併發可以和CPU核數2)

1)建議數量和你的服務器CPU核數一致

a. grep processor /proc/cpuinfo|wc -l

b. 如何通過top命令獲取cpu核數:按鍵盤數字1獲取到

2) 建議數量和你的服務器cpu核數兩倍一致


優化nginx服務進程均勻分配到不同CPU進行處理(性能優化)

官方參考鏈接:http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity

Syntax: worker_cpu_affinity cpumask ...;

worker_cpu_affinity auto [cpumask];

Default: —

Context: main

44CPU配置方法

worker_processes    4

worker_cpu_affinity 0001 0010 0100 1000

84CPU配置方法:

worker_processes    8

worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000

worker_processes    8

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000

4核心2CPU配置方法:

worker_processes    4

worker_cpu_affinity 0001 0010 0100 1000

worker_processes    4

worker_cpu_affinity 0101 1010


優化nginx事件處理模型(性能優化)

官方參考鏈接:http://nginx.org/en/docs/ngx_core_module.html#use

Syntax: use method;

Default: —

Context: events     

實踐優化配置:

    events {

       worker_connections  1024;

       use epoll;

    } 


調整Nginx單個進程允許的客戶端最大連接數(性能優化)

官方參考鏈接:http://nginx.org/en/docs/ngx_core_module.html#worker_connections

 Syntax:worker_connections number;

 Default:worker_connections 512;

 Context:events 

注意事項:

worker_connections*worker_processes <= 系統的最大打開文件數

#加大文件描述

echo '*               -       nofile          65535 ' >>/etc/security/limits.conf

 


配置Nginx worker進程最大打開文件數(性能優化)

官方參考鏈接:http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile

Syntax:worker_rlimit_core size;

Default:

Context:main

實踐配置:

worker_rlimit_nofile 65535

#<==最大打開文件數,可設置爲系統優化後的ulimit -HSn的結果,在第3章中,調整系統文件描述符和這個問題有相同之處


優化nginx服務數據高效傳輸模式(性能優化)

官方鏈接參考:http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile

Syntax:sendfile on | off;

Default:sendfile off;

Context:http, server, location, if in location

sendfile on      開啓底層高效傳輸數據模式

官方鏈接參考:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nopush 

Syntax:tcp_nopush on | off;

Default:tcp_nopush off;

Context:http, server, location

tcp_nopush on    對快遞員有利(自身服務器有利)

讓數據不着急傳輸

網絡中傳輸數據的車==數據包 1500  3100k  1500 1500  100 1400

官方鏈接參考:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay

 Syntax:tcp_nodelay on | off;

 Default:tcp_nodelay on;

 Context:http, server, location

 tcp_nodelay on 對用戶感知更好  

 儘快將數據傳輸出去

 實踐配置:

    http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    tcp_nopush      on;

    tcp_nodelay     off;


10 優化Nginx連接參數,調整連接超時時間 (安全優化)

keepalive_timeout       <-- 表示傳輸雙方在指定時間內,沒有數據傳輸就超時斷開連接

官方鏈接參考:https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout

Syntax: keepalive_timeout timeout [header_timeout];

Default: keepalive_timeout 75s;

Context: http, server, location

client_header_timeout <-- 表示客戶端發送請求報文的請求頭部信息間隔超時時間

官方鏈接參考:https://nginx.org/en/docs/http/ngx_http_core_module.html#client_header_timeout

Syntax:client_header_timeout time;

Default:

client_header_timeout 60s;

Context:http, server

client_body_timeout     <-- 表示客戶端發送請求報文的請求主體信息間隔超時時間

官方鏈接參考:https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout

Syntax:client_body_timeout time;

Default:

client_body_timeout 60s;

Context:http, server, location

send_timeout            <-- 表示服務端發送響應報文的間隔超時時間

官方鏈接參考:https://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout

Syntax:send_timeout time;

Default:

send_timeout 60s;

Context:http, server, location


11 優化nginx服務上傳文件限制 (安全優化)

client_max_body_size  控制上傳數據大小限制參數

官方鏈接參考:https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

Syntax:client_max_body_size size;

Default:client_max_body_size 1m;

Context:http, server, location

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