Nginx study notes (b) - Detailed compiler installation step, Nginx tuning, Nginx main configuration file structure

A, Nginx related issues

1.1 Nginx is how to deal with a request it?

(1) nginx at startup, it parses the configuration file to give the desired listening port and ip address; then inside nginx's master process, initialized good this monitor socket, then listen; then fork out multiple sub-processes out , the child will compete accept new connections.

(2) In this case, the client can initiate a connection to nginx. When the client with nginx be three-way handshake to establish a connection with nginx good;

(3) In this case, a sub-process will accept successful, then Nginx creation of a package connections, i.e. ngx_connection_t structure. Next, call the appropriate events of the event processing module, such as http client module and data exchange.

(4) Finally, Nginx or turn off the active client connected to this, a connection is completed.

1.2 Nginx Why not use multithreading? Why such a high performance? nginx is how to achieve high concurrency?

(1) works such as the Apache server, create multiple processes or threads, and each thread or process will assign cpu and memory, but the concurrent drain server resources over the General Assembly. Due to the nature of work web server determines a large part of life of each request is in network transmission, the piece actually spent much time on the server machine. This is a process to solve the secret of a few high concurrency. That is just part of a network webserver io-intensive applications, not a computationally intensive.

(2) so that the Nginx, single thread non-blocking asynchronous request processing, the use of epoll model, provides a queue, the queuing solution, without requesting allocation of resources for each cpu and memory, saves a lot of resources, but also reduces the amount of CPU context switching. So that makes Nginx support higher concurrency.

(3) Nginx master process have a master, a master working processes corresponding to the plurality of worker, each worker working process can handle multiple requests, each come in a request, there will be a worker process to handle. But not the whole process, the process to where blocking can occur, such as the back-end server forwards the request and waiting for a request to return. So, this process worker continues to process other requests, and once the back-end server returns, it will trigger this event, worker will take over, this request will then go down.

Two, Nginx installation and tuning compile

2.1 compile and install

1, extract:
1) extract the tar package: tar zxf nginx-1.15.7.tar.gz
2) Hide nginx version, and prevent attacks :vim nginx-1.15.7/src/core/nginx.h
Here Insert Picture Description

nginx-1.15.9 directory content


1)auto目录:里面有4个子目录,cc是编译使用的,os是判断操作系统类型的,其他都是辅助configure编译的,也就是操作系统有什么特性供nginx使用
2)CHANGES:各版本的改变,bug修复等信息;CHANGES.ru:nginx作者是俄罗斯人,这是俄罗斯版本
3)conf: 为了方便运维配置,conf目录里有示例文件,安装好后会拷贝到安装目录
4)configure 
5)contrib:提供nginx语法检测字体
   cp -r contrib/vim/* ~/.vim	##家目录下如果没有.vim目录,手动新建
6)html:默认发布目录,50x.html是报500错误时的页面
7) src:源码目录

Modify the file /nginx-1.15.9/src/core/nginx.h

#define NGINX_VER          "nginx/" NGINX_VERSION
改为:
#define NGINX_VER          "nginx/"	 

Here Insert Picture Description

3) In Nginx source / auto / cc / gcc file:  Vim Auto / CC / GCC

  • In this mode Debug mode will insert a lot of information to track and ASSERT like, the end of the normal compilation process will produce a few megabytes the size of the package, we can turn off debug mode before compiling, so that compilation end, will only produce a few hundred about packet size K.
# debug
#CFLAGS="$CFLAGS -g" ##本行注释掉,关闭debug日志模式,  

Here Insert Picture Description
Here Insert Picture Description

2, compile and install nginx
1) installed gcc compiler and devel-OpenSSL : OpenSSL-devel yum the install gcc -Y
2) in /root/nginx/nginx-1.15.9 compiler directory:

./configure \
###自定义配置:
--prefix=/usr/local/nginx  \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
--prefix=PATH 
	## 指定nginx的安装目录。默认 /usr/local/nginx
--with-http_realip_module 
	##通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For)
	##意义在于能够使得后台服务器记录原始客户端的IP地址
--with-http_ssl_module 
	##使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
--without-http_rewrite_module 
	##perl正则表达式使用在location指令和ngx_http_rewrite_module模块中。

Found error:
Here Insert Picture Description
The reason: lack of dependency: gd-devel-2.0.35-26.el7.x86_64.rpm (what missing equipment)

3) Installation:yum install gd-devel-2.0.35-26.el7.x86_64.rpm,再次编译。

4)make && make install 安装

3、启动脚本做软链接: ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Here Insert Picture Description

nginx -t	##检测配置文件语法
nginx		##启动nginx

4、修改配置

1)查看nginx的默认配置文件:vim //usr/local/nginx/conf/nginx.conf
2)修改用户,让nginx以nginx用户和用户组启动。
Here Insert Picture Description
3)检测配置文件语法 :nginx -t 修改了nginx用户和组后检测会报错,需要手动创建nginx用户
Here Insert Picture Description
4)添加nginx用户useradd nginx,此时就不报错了。

  • useradd -s /sbin/nologin -M -d /usr/local/nginx/ nginx 创建用户

Here Insert Picture Description
再次加载nginx的配置可能会出现报错:

nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
#解决:删除/usr/local/nginx/logs/nginx.pid,再重新启动

5)此时 ps aux,可看到两个nginx进程,一个是root用户开启的master进程,一个是由nginx用户开启的worker进程
Here Insert Picture Description

2.2调优

5、升级nginx的配置,调优
开启的work进程数是由cpu的总核心数决定的(有几核就有几个work进程)
1) 修改虚拟机cpu核心数(虚拟机关机)
Here Insert Picture Description

2)查看逻辑CPU的个数:cat /proc/cpuinfo | grep “processor” | wc -l 发现是4核的

Here Insert Picture Description
3)修改nginx开启的work进程数:vim //usr/local/nginx/conf/nginx.conf,并且重置nginx nginx -s reload

user  nginx     nginx;
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
  • nginx默认没有开启利用多核cpu,这样配置后,在计算最费时的资源时,使用的cpu核数越多,越快,'0001’从左到右一次表示第四,第三,第二,第一个cpu核心,0表示关闭,1表示开启
    Here Insert Picture Description
    Here Insert Picture Description

此时可以看到开启了4个work进程。
Here Insert Picture Description

4)修改并发连接数:vim //usr/local/nginx/conf/nginx.conf,并且重置nginx nginx -s reload
Here Insert Picture Description

  • worker_connections 65535 : 单个后台worker process进程的 最大并发链接数 And also limit the maximum number of open files, you need to modify 'ulimit -n 65536' set to take effect by the linux system processes.
  • epollOne way is multiplexed IO (I / O Multiplexing) is , but only for kernel linux2.6 above, can greatly improve the performance of the nginx, Nginx is thereby IO model for highly concurrent. Nginx supports select, poll, epoll, kqueue, resig, / dev / poll.kqueue efficient IO model corresponding BSD systems, epoll efficient model corresponds to the Linux system.
  • I/O多路复用: If an I / O stream in, we will start a process to handle the I / O stream. Now, I assume there are now one million I / O stream in, then we need to turn one million one correspondence process to deal with these I / O streams (- this is the multi-process concurrent processing in the traditional sense). Think about one million process, your CPU usage will be high, the implementation and unreasonable. So people put forward the I / O multiplexing model,A thread, by recording the status of I / O stream to manage a plurality of I / O, can increase throughput capacity of the server

Here Insert Picture Description

ps aux to see the process, cat / proc / 2212 / limitsHere Insert Picture Description
Here Insert Picture Description

If not, we expect 65535,

ulimit -a #查看系统的最大打开文件数
ulimit -n 65535 #更改系统配置,立即生效

Here Insert Picture Description
nginx -s reloadAnd then check again.

Here Insert Picture Description

Three, Nginx main configuration file structure

user www www;
# (1)程序运行用户和组-->nginx
worker_processes auto;
# (2)启动进程,指定 nginx 启动的工作进程数量;建议按照 cpu 数目来指定,一般等于cpu核心数目
error_log /home/wwwlogs/nginx_error.log crit;
# (3)nginx全局错误日志
pid  /usr/local/nginx/logs/nginx.pid;
# (4)主进程 PID 保存文件
worker_rlimit_nofile 51200;
# (5)文件描述符数量
 
# (6)事件区块开始-->events
events {
        use epoll;
        # (1)使用 epoll 模型,对于 2.6 以上的内核,建议使用 epoll 模型以提高性能
        worker_connections 51200;
        # (2)一个工作进程能处理的最大连接数量
       }
 
# (7)http区块的开始
http   {
        # 解决中文-->后续补充
    # server里面是一些网站优化参数
    server {
        # (1)具体的某一网站的配置信息
        listen 80;
        # (2)监听端口-->-->可以写成IP:Port的形式
        root html;
        # (3)网页根目录(/usr/local/nginx/html)
        server_name www.wzj.com;
        # (4)提供服务的域名主机名!
        index index.html;
        # (5)默认加载页面-->多个优先级!
        access_log logs/access.log; 
        # (6)访问日志保存位置
          location (.*)\.php$ {
            # 用正则匹配具体的访问对象-->php结尾的
            }
          location {
                # 跳转等规则-->rewrite
            }
    }  # server的匹配结束
 
      server {
        # 虚拟主机;
    }
}       # http结束区块
Published 102 original articles · won praise 21 · views 5310

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/104027881