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


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

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

Syntax:  server_tokens on | off | build | string;php

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

Context: http, server, locationhtml

實踐配置:java

server {node

listen       80;linux

server_name  www.etiantian.org;nginx

server_tokens off;web

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


12 配置Nginx gzip壓縮實現性能優化

100k      ---- 1s   90k

100k      ---- 5s   10k

gzip on;

gzip_min_length             1k;

gzip_buffers                4 16k;

gzip_http_version           1.1;

gzip_comp_level             7;

gzip_types                  text/css text/xml application/javascripts;

gzip_vary                   on;

Syntax: gzip_buffers number size;

Default:

gzip_buffers 32 4k|16 8k;

Context: http, server, location

 

Syntax: gzip_comp_level level;

Default:

gzip_comp_level 1;

Context: http, server, location

 

Syntax: gzip_types mime-type ...;

Default:

gzip_types text/html;

Context: http, server, location

 

Syntax: gzip_vary on | off;

Default:

gzip_vary off;

Context: http, server, location

 

Syntax: gzip_min_length length;

Default:

gzip_min_length 20;

Context: http, server, location

 

Syntax: gzip_http_version 1.0 | 1.1;

Default:

gzip_http_version 1.1;

Context: http, server, location


13 配置Nginx expires實現讓客戶端緩存數據

範例1:將網站的全部圖片進行緩存

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$   oldboy.jpg

{

    expires      30d;

}


範例2:將網站程序代碼文件在本地進行緩存

location ~ .*\.(js|css)$

{

    expires      30d;

}

注意事項:

01. 進行緩存的時間要設置合理

02. 不是全部請求的數據信息均可以進行緩存


14 Nginx日誌相關優化與安全


1)要將web服務日誌信息進行切割處理

2)不記錄不須要的訪問日誌

   location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {

       access_log off;

      }

3)對重要日誌信息進行受權

   chown -R root.root /app/logs

      chmod -R 700 /app/logs

4)進行日誌清理

   編寫腳本自動化清除日誌


15 Nginx站點目錄及文件URL訪問控制

公司會搭建內部網站平臺,只想讓公司內部人員進行查看

範例1:配置Nginx,禁止解析指定目錄下的指定程序。

location ~ ^/images/.*\.(php|php5|sh|pl|py|html)$ 

    { 

        allow 10.0.0.0/24;

        deny 10.0.0.0/24;

    } 

location ~ ^/static/.*\.(php|php5|sh|pl|py)$ 

    { 

        deny all;

    } 

location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$ 

    { 

        deny all;

    } 

範例1:配置禁止訪問指定的單個或多個目錄。

禁止訪問單個目錄的命令以下:

location ~ ^/(static)/ {

        deny all;

}

location ~ ^/static {

        deny all;

}


16 配置Nginx,禁止非法域名解析訪問企業網站


方法1:讓使用IP訪問網站的用戶,或者惡意解析域名的用戶,收到501錯誤,命令以下:

server {

listen 80;

server_name _;

return 501;

}


方法2:經過301跳轉到主頁,命令以下:

server {

listen 80;

server_name _;

rewrite ^(.*) http://blog.etiantian.org/$1 permanent;

}


方法3:發現某域名惡意解析到公司的服務器IP,在server標籤裏添加如下代碼便可,如有多個server則要多處添加。

if ($host !~ ^www\.oldboyedu\.com$)

{

    rewrite ^(.*) http://blog.etiantian.org/$1 permanent;

}


17 Nginx圖片及目錄防盜鏈解決方案

模擬實現盜鏈過程:

01. 配置盜鏈網站信息

第一個里程:編寫盜鏈網站配置文件

server {

     listen       80;

     server_name  www.daolian.org;

     root   html/daolian;

     index  index.html index.htm;

 }


第二里程:編寫盜鏈網站程序代碼

<html>

<head>

<title>老男孩教育

</title>

</head>

<body bgcolor=green>

whw的博客!

<br>個人博客是linux

<a href="http://oldboy.blog.51cto.com" target="_blank">博客地址

</a>

<img src="http://www.etiantian.org/ mp4/ sdfasdfdsaadsf/10-10/test.mp4">

</body>

</html>

 

02. 配置被盜鏈網站信息

在站點目錄下,生成要被盜鏈的圖片信息

[root@web01 www]# ll oldboy.jpg 

-rw-r--r-- 1 root root 71806 3月   9 16:27 oldboy.jpg

1) 根據HTTP referer實現防盜鏈

location ~ .*\.(jpg|gif|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {    <--- 判斷只要用戶訪問的是圖片資源

    valid_referers none blocked *.etiantian.org etiantian.org;

if ($invalid_referer){ 

    rewrite ^/  http://www.etiantian.org/nolink.png;

    } 


location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {     <==說明:將盜鏈提示圖片緩存到用戶本地

valid_referers none blocked server_names *.etiantian.org etiantian.org;

if ($invalid_referer){

        rewrite ^/ http://bbs.etiantian.com/img/nolink.jpg;

    }

    access_log off;

    root html/www;

    expires 1d;

    break;

   }

   }

2) 根據cookie防盜鏈

3) 經過加密變換訪問路徑實現防盜鏈(擴展研究)

   研究nginx模塊  ngx_http_accesskey_module

4)在產品設計上解決盜鏈方案 

   在數據信息上加上水印logo信息


18 Nginx錯誤頁面的優雅顯示


##www

   server {

       listen       80;

       server_name  www.etiantian.org;

       location / {

           root   html/www;

           index  index.html index.htm;

       }

       error_page  403  /403.html; #<==當出現403錯誤時,會跳轉到403.html頁面

   }


範例2:50x頁面放到本地單獨目錄下,進行優雅顯示。

# redirect server error pages to the static page /50x.html

error_page   500 502 503 504  /50x.html;

location = /50x.html {

    root   /data0/www/html;

}

   

範例3:改變狀態碼爲新的狀態碼,並顯示指定的文件內容,命令以下:

error_page 404 =200 /empty.gif;

    server {

        listen       80;

        server_name www.linuxpeixun.com;

        location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

            fastcgi_intercept_errors on;

            error_page  404 =200    /ta.jpg;

            access_log  /app/logs/bbs_access.log  commonlog;

        }

}


範例4:錯誤狀態碼URL重定向,命令以下:

server {

        listen       80;

        server_name www.oldboyedu.com;

        location / {

            root   html/www;

            index  index.html index.htm;

        error_page   404  http://oldboy.blog.51cto.com;

#<==當出現404錯誤時,會跳轉到指定的URL http://oldboy.blog.51cto.com頁面顯示給用戶,

這個URL通常是企業另外的可用地址

            access_log  /app/logs/bbs_access.log  commonlog;

        }

}


19 Nginx站點目錄文件及目錄權限優化


1. robots.txt機器人協議介紹(君子協議)

2. 利用user_agent參數信息進行防爬蟲

   範例1:阻止下載協議代理,命令以下:

   ## Block download agents ##

   if ($http_user_agent ~* LWP::Simple|BBBike|wget)

    {

       return 403;

   }


   if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo!Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot")

   {

      return 403;

   }


20 利用Nginx限制HTTP的請求方法


#Only allow these request methods

 if ($request_method !~ ^(GET|HEAD|POST)$ ) {

     return 501;

 }

 #Do not accept DELETE,SEARCH and other methods


 if ($request_method ~* ^(GET)$ ){

     return 501;

 }


21 使用普通用戶啓動Nginx(監牢模式)


第一個里程:把nginx服務重要文件或目錄信息放置到普通用戶家目錄中

[oldboy@web02 ~]$ mkdir {conf,html,logs}

[oldboy@web02 ~]$ cp /application/nginx/conf/nginx.conf.default ./conf/

[oldboy@web02 ~]$ cp /application/nginx/conf/mime.types ./conf/


第二個里程:編寫nginx配置文件

[oldboy@web02 ~]$ cat ./conf/nginx.conf

worker_processes  1;

error_log  /home/oldboy/logs/error.log;

pid       /home/oldboy/logs/nginx.pid;


events {

    worker_connections  1024;

}

http {

  include       mime.types;

     default_type  application/octet-stream;

     sendfile        on;

     keepalive_timeout  65;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                       '$status $body_bytes_sent "$http_referer" '

                       '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  /home/oldboy/logs/web_blog_access.log  main;

     server {

         listen       80;               <-- 對於普通用戶是沒有資格應用特殊端口 大於1024的端口可讓普通用戶管理

         server_name  www.etiantian.org;

         location / {

             root   /home/oldboy/html/;

             index  index.html index.htm;

         }

     }

}

第三個里程:利用普通用啓動nginx程序

[oldboy@web02 ~]$ /application/nginx/sbin/nginx  -c /home/oldboy/conf/nginx.conf

nginx: [alert] could not open error log file: open() "/application/nginx-1.12.2/logs/error.log" failed (13: Permission denied)

[oldboy@web02 ~]$ ps -ef|grep nginx

oldboy    24337      1  0 19:00 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -c /home/oldboy/conf/nginx.conf

oldboy    24338  24337  0 19:00 ?        00:00:00 nginx: worker process                                        

oldboy    24340  24297  0 19:00 pts/0    00:00:00 grep --color=auto nginx

相關文章
相關標籤/搜索