企业级nginx服务优化合集
企业级nginx服务优化(一)配置文件总结
nginx.conf httpd.conf httpd-vhosts.confhttpd-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
"nginx" 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> nginx/1.6.3-----------------------变成了nginx //404 Not Found
更改掉nginx的用户
grep "#user" nginx.conf.default //默认文件
#usernobody;
1 vim nginx.conf //修改配置文件
usernginxnginx;
2 ./configure--user=nginx --group=nginx
ps -ef | grep nginx
root 56512 10 02:35 ? 00:00:00 nginx: master process //主进程用root运行,可以更为nginx,端口必须大于1024
nginx 57470565120 04:04 ? 00:00:00 nginx: worker process
配置nginx worker进程个数
worker_processes1; //等于CPU的核心数 cat /proc/cpuinfo |grep -c processor 查CPU
更改为
worker_processes2; 查看
nginx 1822 17840 04:14 ? 00:00:00 nginx: worker process
nginx 1823 17840 04:14 ? 00:00:00 nginx: worker process
配置worker_cpu-affinity
worker_processes2;
worker_cpu_affinity 01011010; //把每个work进程分配到单独的CPU核数上
worker_processes4;
worker_cpu_affinity 00010010 0100 1000 ;
worker_processes8;
worker_cpu_affinity 00010010 0100 10000001 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 180http://192.168.10.11/ // -c 表示客户端数,-t 表示时间
taskset - retrieve or set a process’s CPU affinity
taskset-c1,2,3 /etc/init.d/mysqlstart//某个进程跑在某个CPU上
事件处理模型优化在linux下epoll模型
events {//设定nginx工作模式及连接数上限
use epoll;
worker_connections20480; //每个进程的最大连接数,默认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_size512; //默认为512,一般为CPU L1的4-5倍
}
开启高效的文件传输模式
sendfileon;
tcp_nopush on;
连接超时时间 // php服务建议 短连接
keepalive_timeout 60; //客户端连接保持会话的超时时间
tcp_nodelay on;
client_header_timeout15; //客户端请求头读取超时时间,超过不发送数据,返回408错误
client_body_timeout15; //主体
send_timeout 15; // 响应客户端的超时时间
上传文件大小限制 (动态应用)
client_max_body_size 10m; //客户端最大上传 超过了报413错误 0是不检查php默认2m
fastcgi 调优
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.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 302h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any1m;
fastcgi_cache_min_uses1;
drwx------ 12 nginx root 4096 4月 5 04:32 fastcgi_temp // 临时文件
企业级nginx服务优化(二 )
配置nginxgzip压缩功能
gzipon;
gzip_min_length 1k; // 大于1K才压缩
gzip_buffers 432k; // 压缩缓存区大小
gzip_http_version1.1;
gzip_comp_level9; //压缩比率1-9
gzip_types text/csstext/xmlapplication/javascript ; // 压缩类型,不同版本,类型不一样,
gzip_vary on; //varyheader支持
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
测试软件unzip test_deflate.zip 火狐流量器加载yslow插件,可以查看缓存结果
include mime.types;
# cat conf/mime.types //版本不一样,类型不一样
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
apachemod_deflate//编译时--enable-deflate
nginxexpire缓存功能 在客户端本地保存多久 在server标签里
location~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 3650d;
}
location~ .*\.(js|css)?$
{
expires 30d;
}
实例:
location ~(robots.txt) { //为robots.txt 为7天并不记录404
log_not_foundoff;
expires 7d;
break;
}
apache
./configure --enable-expires
cd /httpd-2.2.22/modules/metadata/ //切到apache 软件目录
/application/apache/bin/apxs-c -i -a mod_expires.c // 已dso的方式编译到apache
ll /aplication/apache2.2.22/modules/mod_expires.so //检查
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
http://www.51cto.com/robots.txt /// 写这样的一个文件
User-agent: *
Crawl-delay: 500
Disallow: /wuyou/
Disallow: /php/
Disallow: /wuyou_admin/
Disallow: /actions/
修改nginx.conf,禁止网络爬虫的ua,返回403。
server {
listen 80;
server_name 127.0.0.1;
#添加如下内容即可防止爬虫
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403; //也可以把上面改成浏览器,禁止浏览器访问
}
配置nginx 日志切割脚本 sysog日志分析 crontlog
日志切割
cd /application/nginx/logs&&\
/bin/mv www_access.logwww_access_$(date+%F-d-1day).log
/application/nginx/sbin/nginx -s> crontab -e
00 00 * * * /bin/sh/server/scripts/cut_nginx_log.sh >/dev/null 2&1
不记录不需要的访问日志
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
{ access_logoff;
}
drwxr-xr-x 2 root root 4096 5月 5 21:09 logs/ //主进程是root 是可以写入logs
最小化apache目录及文件权限
[*] 所有站点目录的用户和组都应为root
[*] 所有目录权限是755
[*] 所有文件权限是644
网站服务的用户不能用root
http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
限制ip访问
locaion ~^/hequan/ {
allow 192.168.10.0/24;
deny all;
}
报错显示
error_page 500 502 503 504/50x.html;
location = /50x.html {
root html;
}
-rw-r--r-- 1 root root 537 4月 3 19:53 50x.html
tmpfs是一种基于内存的文件系统
vim/etc/rc.local
mount -t tmpfs -o>
vim /etc/fstab
df -hT
Filesystem Type SizeUsed Avail Use% Mounted on
tmpfs tmpfs100M 0100M 0% /tmp
php_flagengineoff//apache不解析PHP
企业级nginx服务优化(三 )Apache+防盗链
apache worker/prefork
/application/apache/bin/apachectl -l | sed -n '/worker\|prefork/p'
worker.c
Server MPM: Worker
./configure --with-mpm=worke//编译时指定,,默认是prefork
prefork默认
使用多个子进程,每个子进程只有一个线程
效率高,稳定,安全,比worker消耗资源多
vim/application/apache/conf/extra/httpd-mpm.conf
StartServers 10 //服务启动进程数
MinSpareServers 10 //最小空闲数量
MaxSpareServers 15 //最大空闲数量
ServerLimit 2000
MaxClients 1000 //最大进程并发
MaxRequestsPerChild 5000 //子进程处理的最大进程数 0=不限制
ps -ef | grep http| grep -v grep | wc -l //查看并发连接
6
worker 线程与进程的结合
vim/application/apache/conf/extra/httpd-mpm.conf
StartServers 2
MaxClients 2000//并发的客户端连接数量
ServerLimit 25 //总进程数
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100 // 持续的每个服务器的工作线程数量
MaxRequestsPerChild 0 // 单个子进程累计最多处理到少个请求,默认0,不限制的意思
MaxClient
页:
[1]