关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

云服务器安装Nginx并进行性能调试和优化

发布时间:2022-09-09 15:11:02

云服务器就选第八云,稳定、安全、价格低!


Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,其静态处理和高并发远超同级别web服务器,下面接为大家介绍一下nginx在centos云服务器中安装,性能调试和优化!


1.下载nginx源码包

下载地址:https://nginx.org/ 下载完成之后上传nginx软件包



2.检查依赖库是否安装,没有安装则安装依赖库



[root@centos8 ~]# rpm -q zlib-devel pcre-devel  package zlib-devel is not installed package pcre-devel is not installed [root@centos8 ~]# dnf -y install zlib-devel pcre-devel gcc gcc-c++



3.创建nginx运行用户

[root@centos8 ~]# useradd -s /sbin/nologin nginx -M [root@centos8 ~]# tail -1 /etc/passwd nginx:x:1001:1001::/home/nginx:/sbin/nologin



4.编译安装

root@centos8 ~]# tar zxf nginx-1.21.6.tar.gz -C /usr/src [root@centos8 ~]# cd /usr/src/nginx-1.21.6/ [root@centos8 nginx-1.21.6]# ls auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src [root@centos8 nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module [root@centos8 nginx-1.21.6]# make && make install


nginx使用yum源安装,可能导致业务上很多需要的功能模块没有开启,还是按需自己编译比较合适。


./configure --help #查看./configure 支持哪些参数

                             print this message   --prefix=PATH                      set installation prefix    ##安装路径,一般填写这个路径即可,下面的各个路径会默认在--prefix目录下面创建对应的目录   --sbin-path=PATH                   set nginx binary pathname   --modules-path=PATH                set modules path   --conf-path=PATH                   set nginx.conf pathname   --error-log-path=PATH              set error log pathname   --pid-path=PATH                    set nginx.pid pathname   --lock-path=PATH                   set nginx.lock pathname   --user=USER                        set non-privileged user for                                      worker processes   --group=GROUP                      set non-privileged group for                                      worker processes   --build=NAME                       set build name   --builddir=DIR                     set build directory    #--with-xxx-xxx参数,就是默认没有开启安装的模块,如果需要安装,就要加在./configure的参数里面   --with-http_random_index_module    enable ngx_http_random_index_module   --with-http_secure_link_module     enable ngx_http_secure_link_module   --with-http_degradation_module     enable ngx_http_degradation_module   --with-http_slice_module           enable ngx_http_slice_module   --with-http_stub_status_module     enable ngx_http_stub_status_module #--without-xxx-xxx参数,就是默认已经开启安装的,如果不需要安装该模块,就要加在./configure的参数里面   --without-http_charset_module      disable ngx_http_charset_module   --without-http_gzip_module         disable ngx_http_gzip_module   --without-http_ssi_module          disable ngx_http_ssi_module   --without-http_userid_module       disable ngx_http_userid_module   --without-http_access_module       disable ngx_http_access_module


源码文件个目录解释

[root@centos8 nginx-1.21.6]# ll total 808 drwxr-xr-x. 6 nginx nginx   4096 Apr 23 16:26 auto -rw-r--r--. 1 nginx nginx 316961 Jan 25 23:04 CHANGES -rw-r--r--. 1 nginx nginx 484308 Jan 25 23:03 CHANGES.ru drwxr-xr-x. 2 nginx nginx    168 Apr 23 16:26 conf -rwxr-xr-x. 1 nginx nginx   2590 Jan 25 23:03 configure drwxr-xr-x. 4 nginx nginx     72 Apr 23 16:26 contrib drwxr-xr-x. 2 nginx nginx     40 Apr 23 16:26 html -rw-r--r--. 1 nginx nginx   1397 Jan 25 23:03 LICENSE -rw-r--r--. 1 root  root     438 Apr 23 16:39 Makefile drwxr-xr-x. 2 nginx nginx     21 Apr 23 16:26 man drwxr-xr-x. 3 root  root     125 Apr 23 16:39 objs -rw-r--r--. 1 nginx nginx     49 Jan 25 23:03 README drwxr-xr-x. 9 nginx nginx     91 Apr 23 16:26 src

  • auto目录:用于编译时的文件,以及相关lib库,编译时对对操作系统的判断等,都是为了辅助./configure命令执行的辅助文件。


  • CHANGES文件:就是当前版本的说明信息,比如新增的功能,修复的bug,变更的功能等
  • CHANGES.ru文件:作者是俄罗斯人,生成了一份俄罗斯语言的CHANGE文件
  • conf目录:是nginx编译安装后的默认配置文件或者示列文件,安装时会拷贝到安装的文件夹里面。
  • configure文件:编译安装前的预备执行文件。
  • contrib目录:该目录是为了方便vim编码nginx的配置文件时候,颜色突出显示,可以将该目录拷贝到自己的~/.vim目录下面 cp -rf contrib/vim/* ~/.vim/ 这样vim打开nginx配置文件就有突出的颜色显示。
  • html目录:编译安装的默认的2个标准web页面,安装后会自动拷贝到nginx的安装目录下的html下。
  • man目录:nginx命令的帮助文档,linux上可以使用man命令查看帮助,
  • src:nginx的源码文件**

5.关闭防火墙和SELinux,做软链接

[root@centos8 nginx-1.21.6]# systemctl stop firewalld.service  [root@centos8 nginx-1.21.6]# setenforce 0 [root@centos8 nginx-1.21.6]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/


6.检测语法并启动nginx

[root@centos8 nginx-1.21.6]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos8 nginx-1.21.6]# nginx [root@centos8 nginx-1.21.6]# netstat -anlpt | grep nginx tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7883/nginx: master


7.访问验证

出现下图则安装成功 

8.隐藏版本号

有些版本存在漏洞,如果不隐藏版本号,会有被攻击的风险,所以最好隐藏版本号 

[root@centos8 ~]# vim /usr/local/nginx/conf/nginx.conf

[root@centos8 ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos8 ~]# nginx -s reload

9.调试优化

9.1优化工作进程数量

worker_processes:nginx对外提供Web服务的工作进程数,一般设置CPU的核心或者核心数x2


 ##运行用户(一般保持默认配置,也可以更改) worker_processes  1;



查看cpu核心数

[root@centos8 ~]# lscpu Architecture:        x86_64 CPU op-mode(s):      32-bit, 64-bit Byte Order:          Little Endian CPU(s):              4 On-line CPU(s) list: 0-3 Thread(s) per core:  1 Core(s) per socket:  4 Socket(s):           1 NUMA node(s):        1


CPU(s)后面就是cpu的核心数 或者

[root@centos8 ~]# more /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 158 model name : Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz stepping : 10 microcode : 0xb4 cpu MHz : 2208.002 cache size : 9216 KB physical id : 0 siblings : 4 core id : 0 cpu cores : 4


cpu cores就是cpu的核心数


9.2Nginx运行CPU亲和力

比如4核配置: worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000 比如8核配置: worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 0000100000010000 00100000 01000000 10000000;

worker_processes:最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了


9.3Nginx最大打开文件数

worker_rlimit_nofile 65535;

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

注:文件资源限制的配置可以在/etc/security/limits.conf设置,针对root/user等各个用户或者*代表所有用户来设置。

*   soft nofile   65535 *   hard nofile   65535

用户重新登录生效(ulimit -n)


9.4优化事件处理模型

nginx的连接处理机制在于不同的操作系统会采用不同的I/O模型,Linux下,nginx使用epoll的I/O多路复用模型,我们使用的是Centos,因此将nginx的事件处理模型调整为epoll模型,处理效率高 在不指定事件处理模型时,nginx默认会自动选择最佳的事件处理模型服务


events {     worker_connections  1024; #打开一个进程可以运行多少个链接     multi_accept on; #收到一个新连接的通知后接受尽可能多的链接     use epoll; }

work_connections是单个worker进程允许客户端最大连接数,这个数值一般根据服务器性能和内存来制定,实际最大值就是worker进程数乘以work_connections


multi_accept 告诉nginx收到一个新连接通知后接受尽可能多的连接,默认是on,设置为on后,多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态,设置为off后,多个worker按并行方式来处理连接,也就是一个连接会唤醒所有的worker,直到连接分配完毕没有取得接的继续休眠。当你的服务器连接数不多时,开启这个参数会让负载有一定的降低,但是当服务器的吞吐量很大时,为了效率,可以关闭这个参数.


9.5开启高效传输模式

http {     server_tokens off;     include       mime.types;     default_type  application/octet-stream;     ........     sendfile        on;     tcp_nopush      on;     ................
  • Include mime.types : 媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令。
  • default_type application/octet-stream :默认媒体类型足够。
  • sendfile on:开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
  • tcp_nopush on:必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(将响应头和正文的开始部分一起发送,而不一个接一个的发送。
  • server_tokens:并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。


9.6连接超时时间

主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的

keepalive_timeout 60; tcp_nodelay on; client_header_buffer_size 4k; open_file_cache max=102400 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 1; client_header_timeout 15; client_body_timeout 15; reset_timedout_connection on; send_timeout 15; server_tokens off; client_max_body_size 10m;
  • keepalived_timeout :客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接。


  • tcp_nodelay:也是防止网络阻塞,不过要包涵在keepalived参数才有效。告诉nginx不要缓存数据,而是一段一段的发送,当需要及时发送数据时,应该设置一个属性,这样发送一小块数据信息时就不能立即得到返回值


  • client_header_buffer_size 4k:客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过 1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。


  • open_file_cache max=102400 inactive=20s :这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。


  • open_file_cache_valid 30s:这个是指多长时间检查一次缓存的有效信息。


  • open_file_cache_min_uses 1 :open_file_cache指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。


  • client_header_timeout: 设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误。


  • client_body_timeout:设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示。


  • reset_timeout_connection :告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
  • send_timeout:响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接。


  • client_max_body_size:上传文件大小限制



/template/Home/D8Cloud/PC/Static