企業級nginx服務優化(一)

餘二五發表於2017-11-22

配置檔案總結

nginx.conf      httpd.conf    httpd-vhosts.conf  httpd-mpm.conf

my.cnf          php.ini        php-fpm.conf


更改版本資訊

curl -I 192.168.10.11

Server: nginx/1.6.3


第一種   修改版本及版本號

nginx編譯前更改

src/core/nginx.h

#define nginx_version      1008001

#define NGINX_VERSION      “1.8.1”  #修改想要顯示的版本如:2.2.23

#define NGINX_VER          “nginx/” NGINX_VERSION        #將nginx修改成想要顯示的軟體名稱

#define NGINX_VAR          “NGINX” #將nginx修改成想要顯示的軟體名稱(Evan Web Server)

#define NGX_OLDPID_EXT     “.oldbin”

src/http/ngx_http_header_filter_module.c

static char ngx_http_server_string[] = “Server: nginx” CRLF;  #將nginx修改為想要的版本

src/http/ngx_http_special_response.c

“<hr><center>nginx</center>” CRLF  #將nginx修改為想要的版本資訊


第二種   隱藏版本號

nginx配置檔案裡增加 server_tokens off;

server_tokens作用域是http server location語句塊

server_tokens預設值是on,表示顯示版本資訊,設定server_tokens值是off,就可以在所有地方隱藏nginx的版本資訊。

http{

      server_tokens off;

}

/application/nginx/sbin/nginx -s reload

nginx/1.6.3———————–變成了nginx   //404 Not Found


更改掉nginx的使用者

# grep “#user”    nginx.conf.default           //預設檔案

#user  nobody;

1    vim   nginx.conf   //修改配置檔案               

user  nginx  nginx;

2   ./configure  –user=nginx   –group=nginx 

ps -ef | grep nginx

root      56512      1  0 02:35 ?        00:00:00 nginx: master process      //主程式用root執行,可以更為nginx,埠必須大於1024

nginx     57470  56512  0 04:04 ?        00:00:00 nginx: worker process 


配置nginx worker程式個數

worker_processes  1;          //等於CPU的核心數   cat /proc/cpuinfo |grep -c processor 查CPU

更改為worker_processes  2;    檢視

nginx      1822   1784  0 04:14 ?        00:00:00 nginx: worker process       

nginx      1823   1784  0 04:14 ?        00:00:00 nginx: worker process


配置worker_cpu-affinity 

worker_processes  2;

worker_cpu_affinity    0101  1010;              //把每個work程式分配到單獨的CPU核數上

worker_processes  4;

worker_cpu_affinity    0001  0010 0100 1000 

worker_processes  8;

worker_cpu_affinity    0001  0010 0100 1000  0001 0010 0100 1000


安裝壓力測試軟體  webbench

wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz  

tar zxf webbench-1.5.tar.gz

cd webbench-1.5

make && make install

webbench -c 20000  -t 180  http://192.168.10.11/     // -c 表示客戶端數,-t 表示時間


taskset – retrieve or set a process’s CPU affinity

taskset  -c  1,2,3   /etc/init.d/mysql  start  //某個程式跑在某個CPU上


事件處理模型優化  在linux下epoll模型

events {  //設定nginx工作模式及連線數上限

        use    epoll;

        worker_connections  20480;    //每個程式的最大連線數,預設1024

}

Max_client=worker_processes*worker_connections;   最大數

程式的最大連線數受系統程式最大開啟檔案數限制,執行ulimit -HSn 65535,或者配置相應檔案的   worker_connections的設定後生效。

worker_rlimit_nofile    65535;      //每個程式最大檔案開啟數


優化伺服器名字hash表大小

http://hequan.blog.51cto.com/              //泛解析

http{

server_names_hash_bucket_size   64;

server_names_hash_max_size  512;              //預設為512,一般為CPU L1的4-5倍

}


開啟高效的檔案傳輸模式

sendfile  on;

tcp_nopush   on;


連線超時時間      // php服務建議 短連線


keepalive_timeout     60;               //客戶端連線保持會話的超時時間

tcp_nodelay    on;

client_header_timeout  15;    //客戶端請求頭讀取超時時間,超過不傳送資料,返回408錯誤

client_body_timeout  15;      //主體

send_timeout    15;    // 響應客戶端的超時時間


上傳檔案大小限制    (動態應用)

client_max_body_size   10m;    //客戶端最大上傳               超過了報413錯誤    0是不檢查  php預設2m


fastcgi 調優  


location ~ .*.(php|php5)?$ {

    fastcgi_pass   127.0.0.1:9000;

    fastcgi_index  index.php;

    include        fastcgi.conf;

}


fastcgi_connect_timeout   300;   //連線

fastcgi_send_timeout   300;    //傳送請求

fastcgi_read_timeout   300;  //應答

fastcgi_buffer_size   64k;   //緩衝區

fastcgi_buffer     4      64k;            //       多少個 多大的緩衝區

fastcgi_busy_buffer_size   128k;

fastcgi_temp_buffer_size   128k;

fastcgi_cache   hequan_nginx

fastcgi_cache_valid   200 302  h;

fastcgi_cache_valid 301 1d;

fastcgi_cache_valid any  1m;

fastcgi_cache_min_uses  1;


drwx—— 12 nginx root 4096 4月   5 04:32 fastcgi_temp   // 臨時檔案

本文轉自 295631788 51CTO部落格,原文連結:http://blog.51cto.com/hequan/1770441,如需轉載請自行聯絡原作者


相關文章