您的位置:首页 > 运维架构 > Nginx

企业级nginx服务优化(一)

2016-05-05 16:17 816 查看
配置文件总结
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 // 临时文件

本文出自 “何全” 博客,请务必保留此出处http://hequan.blog.51cto.com/5701886/1770441
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: