nginx基础优化

  • 调整参数隐藏nginx版本信息
[root@web01 extra]# curl -I www.suffergtf.com
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3      #####可以查看到nginx版本号
Date: Tue, 05 Jun 2018 17:02:56 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="auth test" [root@web01 extra]# vim www.conf server { server_tokens off;  ####添加该字段,隐藏版本号 listen
80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; auth_basic_user_file /application/nginx/conf/htpasswd; } } server { listen 80; server_name suffergtf.com; rewrite ^/(.*) http://www.suffergtf.com/$1 permanent; }
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# curl -I www.suffergtf.com
HTTP/1.1 401 Unauthorized
Server: nginx        ####不显示版本号了
Date: Tue, 05 Jun 2018 17:04:40 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="auth test"
  • 修改源码隐藏使用的软件名
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/core/nginx.h   ####编辑源码文件
13 #define NGINX_VERSION      "1.6.3"    #####在第13行修改如下
14 #define NGINX_VER          "nginx/" NGINX_VERSION     ####修改如下
16 #define NGINX_VAR          "NGINX"      ####修改如下
13 #define NGINX_VERSION      "2.2.2"     ####2.2.2或者其他,自己定义
14 #define NGINX_VER          "apache/" NGINX_VERSION     #####自己定义
16 #define NGINX_VAR          "APACHE"    ####自己定义
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c
49 static char ngx_http_server_string[] = "Server: nginx" CRLF; ####修改如下
49 static char ngx_http_server_string[] = "Server: apache" CRLF;   
[root@web01 extra]# vim /server/tools/nginx-1.6.3/src/http/ngx_http_special_response.c
22 "<hr><center>" NGINX_VER "</center>" CRLF    ####修改如下
29 "<hr><center>nginx</center>" CRLF        ####修改如下
22 "<hr><center>" NGINX_VER "<http://www.suffergtf.com>" CRLF
29 "<hr><center>apache</center>" CRLF
重新编译安装到其他的目录
[root@web01 ~]# cd /server/tools/nginx-1.6.3
[root@web01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3.test/ --with-http_stub_status_module --with-http_ssl_module  
[root@web01 nginx-1.6.3]# make && make install
[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -s stop
[root@web01 nginx-1.6.3]# /application/nginx-1.6.3.test/sbin/nginx
[root@web01 nginx-1.6.3]# curl -I localhost
HTTP/1.1 200 OK
Server: apache/2.2.2
Date: Tue, 05 Jun 2018 17:41:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 05 Jun 2018 17:36:39 GMT
Connection: keep-alive
ETag: "5b16ca27-264"
Accept-Ranges: bytes
  • 更改Nginx服务的默认用户

[root@web01 nginx-1.6.3]# /application/nginx/sbin/nginx -V
nginx version: apache/2.2.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module  ###编译时指定
[root@web01 nginx-1.6.3]# vim /application/nginx/conf/nginx.conf.default   ###因为nginx.conf已经做过配置,这里拿nginx.conf.default默认配置参考

  1
  2 #user  nobody;    ####如果已经编译安装,没有加上面的参数,可以修改配置文件,将注释去掉改为 user nginx;
  • 优化nginx服务的worker进程个数
[root@web01 ~]# grep processor /proc/cpuinfo |wc -l  ####查看cpu核心数
2
[root@web01 ~]# vim /application/nginx/conf/nginx.conf

  1 worker_processes  1;  ###修改如下
 1 worker_processes  2;    ####nginx进程数,根据服务器的核心数来修改,如本服务器为单cpu双核心,所以进程数修改为2;
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
[root@web01 ~]# ps aux|grep nginx
root       1456  0.0  0.3  45172  1132 ?        Ss   02:10   0:00 nginx: master process /application/nginx/sbin/nginx
nginx      1457  0.0  0.4  45616  1728 ?        S    02:10   0:00 nginx: worker process          
nginx      1458  0.0  0.4  45616  1784 ?        S    02:10   0:00 nginx: worker process        
root       1460  0.0  0.2 103260   840 pts/0    S+   02:10   0:00 grep nginx
  • 绑定不同的nginx进程到不同的CPU上
默认情况下,nginx的多个进程有可能在同一CPU或CPU的某一核上,导致Nginx进程资源分配不均匀
[root@web01 ~]# vim /application/nginx/conf/nginx.conf

  1 worker_processes  2;
  2 worker_cpu_affinity 0001 0010;  ###添加这个字段。worker_cpu_affinity是配置nginx进程与cpu亲和力的参数,0001 0010是掩码,表示1、2号CPU
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
  • 事件处理模型优化

默认情况下,nginx会自动选择最佳的事件处理模型服务,这里我们手动指定
[root@web01 ~]# vim /application/nginx/conf/nginx.conf

  1 worker_processes  2;
  2 worker_cpu_affinity 0001 0010;
  3 error_log       logs/error.log  error;
  4 events {
  5     use epoll;    ####添加此行内容
  6     worker_connections  1024;
  7 }
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
  • 调整nginx单个进程允许的客户端最大连接数

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
6     worker_connections  1024;    ###修改第6行,如下
6     worker_connections  20480;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
  • 配置nginx进程最大打开文件数

[root@web01 ~]# vim /application/nginx/conf/nginx.conf

  1 worker_processes  2;
  2 worker_rlimit_nofile 65535;  ###添加此行内容
  3 worker_cpu_affinity 0001 0010;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
  • 开启高效文件传输模式

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
9 http {
 10     include       mime.types;
 11     default_type  application/octet-stream;
 12     sendfile        on;    ###1.6.3默认开启
 13     tcp_nopush      on;    ###添加此行
 14     keepalive_timeout  65;
[root@web01 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 ~]# /application/nginx/sbin/nginx -s reload
  • 优化nginx连接参数,调整连接超时时间

连接超时:当服务器建立的连接没有接收处理请求时,让此连接自动退出,当nginx和fastcgi服务建立连接请求php时,如果fastcgi服务无法给nginx返回数据时,自动退出连接,不让用户一直等

猜你喜欢

转载自www.cnblogs.com/suffergtf/p/9141572.html