企业级nginx服务优化(一)
配置文件總結
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 ? // 臨時文件
轉載于:https://blog.51cto.com/hequan/1770441
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的企业级nginx服务优化(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页表单自动提交
- 下一篇: 使用ENTER模拟触发表单提交或者cli