Nginx connection timeout, process management optimization (load balancing)

Keywords: Nginx vim yum zlib

Connection timeout for Nginx

In corporate websites, avoid wasting resources by occupying connections with the same customer for a long time
Connection timeout parameters can be set to control connection access time

Configure nginx

[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##Install Environment Package

[root@localhost ~]# useradd -M -s /sbin/nologin nginx  ##Create procedural users

[root@localhost ~]# mkdir /chen  ##Create mount point
[root@localhost ~]# mount.cifs //192.168.100.23/LNMP/chen ##Mount
Password for root@//192.168.100.23/LNMP:  

[root@localhost chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/  ##decompression

[root@localhost chen]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

./configure \  ##Install nginx components
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

[root@localhost nginx-1.12.2]# make && make install ##Compile

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##Make soft links to allow the system to recognize nginx owner commands
[root@localhost nginx-1.12.2]# nginx -t  ##Check for grammatical errors
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

Write nginx scripts in system startup scripts to facilitate service manager management

[root@localhost nginx-1.12.2]# cd /etc/init.d/ ##To System Startup Script

[root@localhost init.d]# vim nginx   ##Write a nginx script

#!/bin/bash
#chkconfig: - 99 20  #annotation
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"  #This variable points to my command file
PIDF="/usr/local/nginx/logs/nginx.pid"  #This variable, the process number to nginx
case "$1" in
    start)
        $PROG                                              
        ;;
    stop)
        kill -s QUIT $(cat $PIDF) 
        ;;
    restart)                                                  
        $0 stop
        $0 start
        ;;
    reload)                                                  
        kill -s HUP $(cat $PIDF)
        ;;
    *)                                                           
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost init.d]# chmod +x nginx  ##Promote permissions to Nginx
[root@localhost init.d]# chkconfig --add nginx  ##Add nginx
[root@localhost init.d]# service nginx start 
[root@localhost init.d]# netstat -ntap | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17544/nginx: master 

[root@localhost init.d]# systemctl stop firewalld.service
[root@localhost init.d]# setenforce 0

Configure Connection Timeout

Modify nginx configuration file

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

Add in http protocol level zone

31     keepalive_timeout  65 180; ##65 is server timeout, 180 is client timeout
 32     client_header_timeout 80;  ##80 is the header timeout in a web page
 33     client_body_timeout 80;  ##80 is the timeout for the body on the web
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start

nginx process management optimization

View process

[root@localhost conf]# ps aux | grep nginx
root      51524  0.0  0.0  20544   600 ?        Ss   20:05   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     51525  0.0  0.0  23072  1388 ?        S    20:05   0:00 nginx: worker process
root      51531  0.0  0.0 112728   972 pts/3    S+   20:06   0:00 grep --color=auto nginx

master serves as a supervisor and does not handle requests
Worker is the worker process, handling requests
We only have one working process here and all requests will be handed over to it
If we want to optimize we need to expand, we're expanding directly in the virtual machine

If we have this nginx on Ali Cloud, we can either expand it manually or automatically. Auto-expansion is about writing an auto-compilation tool. We need to trigger it to something like insufficient CPU, then we need zbbix monitoring.
cpu memory hard disk network bandwidth These are called vertical extensions
The number of server instances is called horizontal expansion

[root@localhost conf]# init 0 ##Shut down the server to expand

Go back to the server and modify the configuration file

[root@localhost ~]# cd /proc/ ##View your cpu, memory information
[root@localhost proc]# ls
1     1536  1790  305  40   526  626  717        devices      mtrr
10    1542  18    306  41   53   628  72         diskstats    net
100   1545  189   307  416  54   63   723        dma          pagetypeinfo
1082  1557  19    31   417  55   630  724        driver       partitions
1084  1562  2     319  418  558  651  73         execdomains  sched_debug
1085  1565  20    32   42   560  652  732        fb           schedstat
1089  1581  21    320  43   575  654  733        filesystems  scsi
1096  1594  22    33   433  576  666  74         fs           self
11    16    23    333  44   577  667  75         interrupts   slabinfo
1106  1613  24    334  443  578  668  76         iomem        softirqs
1107  1629  25    335  45   579  669  763        ioports      stat
12    1633  26    336  450  580  671  77         irq          swaps
1228  1637  27    337  451  581  674  78         kallsyms     sys
126   1640  28    338  456  582  675  79         kcore        sysrq-trigger
13    1648  288   339  47   583  679  8          keys         sysvipc
131   1651  289   34   48   584  685  80         key-users    timer_list
1335  1660  29    340  486  585  686  888        kmsg         timer_stats
1337  1668  290   341  49   586  687  9          kpagecount   tty
1338  1680  298   342  5    587  690  99         kpageflags   uptime
136   1687  299   343  50   588  697  acpi       loadavg      version
14    1689  3     35   51   589  698  buddyinfo  locks        vmallocinfo
1438  1696  30    36   516  590  7    bus        mdstat       vmstat
1449  17    300   360  52   593  700  cgroups    meminfo      zoneinfo
1478  1739  301   37   522  6    701  cmdline    misc
1479  1741  302   38   523  60   703  consoles   modules
15    1749  303   39   524  61   71   cpuinfo    mounts
1532  1768  304   4    525  62   716  crypto     mpt
[root@localhost proc]# cat cpuinfo  ##Look at the CPU, there are eight cores
processor   : 7
vendor_id   : GenuineIntel
cpu family  : 6
model       : 94
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
stepping    : 3
microcode   : 0xcc
cpu MHz     : 2591.567
cache size  : 6144 KB
physical id : 1
siblings    : 4
core id     : 3
cpu cores   : 4
apicid      : 7
initial apicid  : 7
fpu     : yes
fpu_exception   : yes
cpuid level : 22
wp      : yes

Modify nginx configuration file

[root@localhost proc]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
 3 worker_processes 8; ##The third line was modified with only one core number, but now we have 8
 1024 requests per core by default

Allocate all requests to these eight cores for load balancing

 4 worker_cpu_affinity 01 10 
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# ps aux | grep nginx ##View process
root       1951  0.0  0.0  20544   660 ?        Ss   23:24   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1952  0.0  0.0  23072  1396 ?        S    23:24   0:00 nginx: worker process
nginx      1953  0.0  0.0  23072  1384 ?        S    23:24   0:00 nginx: worker process
nginx      1954  0.0  0.0  23072  1388 ?        S    23:24   0:00 nginx: worker process
nginx      1955  0.0  0.0  23072  1396 ?        S    23:24   0:00 nginx: worker process
nginx      1956  0.0  0.0  23072  1388 ?        S    23:24   0:00 nginx: worker process
nginx      1957  0.0  0.0  23072  1396 ?        S    23:24   0:00 nginx: worker process
nginx      1958  0.0  0.0  23072  1396 ?        S    23:24   0:00 nginx: worker process
nginx      1959  0.0  0.0  23072  1396 ?        S    23:24   0:00 nginx: worker process
root       1984  0.0  0.0 112728   972 pts/0    S+   23:25   0:00 grep --color=auto nginx

That's all we have. Thank you for watching

Posted by gibs on Thu, 14 Nov 2019 11:33:25 -0800