今天决定写一篇LNMP的深刻调优,

 LNMP=Linux Nginx Mysql PHPjavascript

LNMP的调优着重体如今Nginx服务器上的调优php

 Nginx 是一个高性能的 HTTP 和 反向代理 服务器,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名,其特色是占有内存少,并发能力强,百度BWS、新浪、网易、腾讯等都在使用。css

[root@xuegod68 ~]#tar xvf nginx-1.8.0.tar.gz -C /usr/local/html

[root@xuegod68 ~]#cd /usr/local/nginx-1.8.0/java

编译前的优化主要是用来修改程序名等node

 

[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: bytesnginx

咱们须要修改一下配置文件:c++

[root@xuegod68 nginx-1.8.0]# vim src/core/nginx.h                               //目的更改源码隐藏软件名称和版本号sql

#define NGINX_VERSION      "9.9.9"                 #此行修改的是你想要的版本号vim

#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的优化,我会开新篇讲解。

相关文章
相关标签/搜索