LNMP=Linux Nginx Mysql PHP
LNMP的调优着重体现在Nginx服务器上的调优
Nginx 是一个高性能的 HTTP 和 反向代理 服务器,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名,其特点是占有内存少,并发能力强,百度BWS、新浪、网易、腾讯等都在使用。
[root@xuegod68 ~]#tar xvf nginx-1.8.0.tar.gz -C /usr/local/
[root@xuegod68 ~]#cd /usr/local/nginx-1.8.0/
编译前的优化主要是用来修改程序名等
[root@xuegod68 ~]# curl -I http://www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18 #我们需要的做的是将nginx修改名字,这儿默认是nginx,我们看到的百度是隐藏后的。
Date: Thu, 19 Apr 2018 08:05:56 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Connection: Keep-Alive
ETag: "575e1f60-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
我们需要修改一下配置文件:
[root@xuegod68 nginx-1.8.0]# vim src/core/nginx.h //目的更改源码隐藏软件名称和版本号
#define NGINX_VERSION "9.9.9" #此行修改的是你想要的版本号
#define NGINX_VER "sandiandian/" NGINX_VERSION #此行修改的是你想修改的软件名称
[root@xuegod68 nginx-1.8.0]# vim src/http/ngx_http_header_filter_module.c
//修改HTTP头信息中的connection字段,防止回显具体版本号
通用http头域:
通用头域包含请求和响应消息都支持的头域,通用头域包含Cache-Control、 Connection、Date、Pragma、Transfer-Encoding、Upgrade、Via。对通用头域的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头域,一般将会作为实体头域处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!
49 static char ngx_http_server_string[] = "Server:sandiandian" CRLF;
[root@xuegod68 nginx-1.8.0]# vim src/http/ngx_http_special_response.c
//这个文件定义了http错误码的返回,有时候我们页面程序出现错误,Nginx会代我们返回相应的错误代码,回显的时候,会带上nginx和版本号,我们把他隐藏起来
21 static u_char ngx_http_error_full_tail[] =
22 "<hr><center>" NGINX_VER "</center>" CRLF
23 "</body>" CRLF
24 "</html>" CRLF
25 ;
26
27
28 static u_char ngx_http_error_tail[] =
29 "<hr><center>nginx</center>" CRLF
30 "</body>" CRLF
31 "</html>" CRLF
ok ,到这个时候呢我们编译之前的优化已经完成,接下里我们开始编译安装:
[root@xuegod68 nginx-1.8.0]#[root@xuegod63 nginx-1.9.12]# yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel –y #安装依赖包,以免在编译过程中出现报错。
[root@xuegod68 ~]# tar xvf pcre-8.37.tar.bz2 -C /usr/local/ #pcre依赖 不需要编译,下载解压 就可以
[root@xuegod68 ~]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.37
各个模块功能呢,各位自行找资料脑补,我这儿就不进行普及了。 可以使用 ./configure --help
[root@xuegod68 ~]#make && make install
ok 到这儿我们已经完成了nginx的编译安装,接下来启动测试
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) #如果出现这种报错 就说明你的80端口被占用,执行一下命令查看端口,关闭就好,一般是apche
[root@xuegod68 ~]#netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2286/nginx #我这边是正常的
测试是否隐藏了版本和软件名
[root@xuegod68 ~]#curl -I 127.0.0.1
Server: sandiandian/9.9.9 #效果很明显
错误代码测试:
Nginx运行用户:
[root@xuegod68 ~]#useradd -M -s /sbin/nologin nginx
[root@xuegod68 nginx]#vim /usr/local/nginx/conf/nginx.conf #修改运行用户为nginx 默认为noboby
user nginx;
Nginx运行进程个数:
[root@xuegod68 nginx]#vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
[root@xuegod68 ~]# /usr/local/nginx/sbin/nginx -s reload
nginx 3211 0.0 0.2 22612 1448 ? S 17:27 0:00 nginx: worker process
nginx 3212 0.0 0.2 22612 1456 ? S 17:27 0:00 nginx: worker process
nginx 3213 0.0 0.2 22612 1456 ? S 17:27 0:00 nginx: worker process
nginx 3214 0.0 0.2 22612 1392 ? S 17:27 0:00 nginx: worker process
Nginx运行CPU亲和力:
这根据你的CPU线程数配置
比如4核4线程配置
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
4线程的CPU,跑两个进程,如下设置
worker_processes 2;
worker_cpu_affinity 0101 1010;
worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,8个进程已经够用了。
Nginx最多可以打开文件数:
[root@xuegod68 ~]#ulimit -n
1024
worker_rlimit_nofile 102400;
nginx分配请求并不是很均匀,所以最好与ulimit -n的值保持一致。
Nginx事件处理模型:
14 events {
15 worker_connections 1024;
16 }
Work_connections是单个进程允许客户端最大连接数,这个数值一般根据服务器性能和内存来制定,也就是单个进程最大连接数,实际最大值就是work进程数乘以这个数
开启高效传输模式:
19 http {
20 include mime.types;
21 default_type application/octet-stream;
22
23 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
24 # '$status $body_bytes_sent "$http_referer" '
25 # '"$http_user_agent" "$http_x_forwarded_for"';
26
27 #access_log logs/access.log main;
28
29 sendfile on;
30 #tcp_nopush on;
Include mime.types; 媒体类型
default_type application/octet-stream; 默认媒体类型 足够
sendfile on; 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
tcp_nopush on; 必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量
连接超时时间:
主要目的是保护服务器资源,CPU,内存,控制连接数,tcp3次握手,4次挥手,建立连接是需要耗费资源的,如果发出连接,一直没有连接,一直没有得到回应,处于等待的全部断掉
keepalive_timeout 60; # 客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
tcp_nodelay on; #防止网络阻塞
client_header_timeout 15; #客户端请求头读取超时时间,如果超过设个时间没有发送任何数据,nginx将返回request time out的错误
client_body_timeout 15; #客户端求主体超时时间,超过这个时间没有发送任何数据,和上面一样的错误提示
send_timeout 15; # 响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超哥这个时间,客户端没有任何活动,nginx关闭连接
文件上传大小限制:
http {
……
client_max_body_size 10m;
Fastcgi调优:
如果本身是一台服务器的,不配置Fastcgi 是一个失败的nginx,如果是作为代理的话 就不需要配置
在nginx配置文件中是没有Fastcgi选项的
配置之前,我们需要了解这两个概念:
Cache:写入缓存区
Buffer:读取缓存区
[root@xuegod68 ~]#free -m
total used free shared buffers cached
Fastcgi是静态服务和动态服务的一个接口
修改nginx.conf配置文件,在http标签中添加如下:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#fastcgi_temp_path /data/ngx_fcgi_tmp;
fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2
keys_zone=ngx_fcgi_cache:512m
inactive=1d max_size=40g;
在server location标签添加如下:
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
#各个标签的作用呢,由于太多 我就不在这儿一一讲解了,可以参考以下网站。
官方文档:
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache
gzip调优:
使用gzip压缩功能,可以为我们节约带宽,加快速度,节约成本。在前面编译的时候,我们已经将这个模块编译进去了
配置压缩的过程:
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
expires缓存调优:
缓存,主要针对于图片,css,js等元素更改机会比较少的情况下使用:
当我们第一次打开页面的时候,可能没什么感觉,当我们第二次打开同一个的时候,我们就会发现速度快了很多,这就是缓存的功劳。我们可以把这些缓存设置一个时间 15天,或者是更长的时间,视情况而定。设置如下:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 3650d;
}
location ~ .*\.(js|css)?$
{
expires 30d;
}
同时也可以对目录及其进行判断:
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 360d;
}
location ~(robots.txt) {
expires 7d;
break;
}
expire优点:
节约带宽,节约服务器成本,节约人力成本,提升用户体验。
expire缺点:
因为缓存可能会给客户提供的还是旧页面,反而降低用户体验,可以通过缩短缓存时间,对缓存的对象改名,来解决这个问题。
nginx的调优呢,已经进入尾声了,对于MySQL的优化,我会开新篇讲解。