You got a dream, You gotta protect it.People can’t do something themselves,they wanna tell You You can’t do it want something,go get it. You got a dream, you gotta protect it. People can’t do something themselves, they wanna tell you you can’t do it. If you want something, go get it. — The Pursuit of Happyness
Basic overview
Nginx is the mainstream Internet high-performance HTTP and echo proxy Web server, Nginx can not only serve as a Web server, it also provides static resource service, cache, load balancing and other functions.
Whether you’re a front-end programmer or a back-end programmer, you’re probably familiar with Nginx. It acts as a bridge between the front end and the back end, acting as a kind of glue. Especially in the era of separation of the front and back ends, Nginx appears as often as a-list idol stars. In the age of Cloud Native, the advent of the Envoy seems to enhance our chances of technological selection. Of course both Envoy and Nginx use the multi-threaded + non-blocking + asynchronous IO (Libevent) architectural pattern.
Build Nginx
Deploy Nginx based on Centos 7
- Install nginx-related dependencies
#Install nginx to support rewrite
yum -y install pcre*
#Install nginx to support GCC-C ++
yum -y install gcc-c++
#Install nginx to support Zlib
yum -y install zlib*
#Install nginx to support OpenSSL
yum -y install openssl
Copy the code
- Download the nginx-1.19.8.tar.gz installation package and upload it to the /usr/local server directory
[root@centos-pivtoal ~]# cd /usr/local [root@centos-pivtoal local]# ls aegis bin cloudmonitor etc games include lib Lib64 libexec nginx-1.19.8.tar.gz sbin share SRC [root@centos-pivtoal local]# tar-xvf nginx-1.19.8.tar.gz lib64 libexec nginx-1.19.8.tar.gz sbin share SRC [root@centos-pivtoal local]# tar-xvf nginx-1.19.8.tar.gz [root@centos-pivtoal local]# CD nginx-1.19.8 [root@centos-pivtoal nginx-1.19.8]# ls auto CHANGES changes.ru conf Configure contrib HTML LICENSE man README SRC [root@centos-pivtoal nginx-1.19.8]# configure contrib HTML LICENSE man README SRC [root@centos-pivtoal nginx-1.19.8]#Copy the code
- Configure installation check:./configure
[root@centos-pivtoal nginx-1.19.8]#./configure [root@centos-pivtoal nginx-1.19.8]#Copy the code
- Nginx: make && make install
[root@centos-pivtoal nginx-1.19.8]# make && make install
[root@centos-pivtoal nginx-1.19.8]#
Copy the code
- Configure Nginx startup upon startup
[1]. Create nginx.service in /lib/systemd/system/ directory:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Copy the code
[Notes]: [Unit]: Description of the Service Description: describes the Service After: describes the Service category. [Service] Setting the Service running parameters. Type:forking is the background running mode ExecStop is a stop command PrivateTmp:True Specifies an independent temporary space for the Service. All commands for starting, restarting, and stopping a Service require the absolute path [Install]
[2]. Add automatic system startup
#Setting boot
systemctl enable nginx.service
#Start the nginx service
systemctl start nginx.service
#Check the nginx service status
systemctl status nginx.service
#Restarting the service
systemctl restart nginx.service
#View all services that have been started
systemctl list-units --type=service
Copy the code
Deploy Nginx based on Dokcer
- Pull the Nginx image
[root@centos-pivtoal ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@centos-pivtoal ~]# docker tag docker.io/library/nginx:latest nginx:latest
[root@centos-pivtoal ~]#
Copy the code
- Write Docker deployment scripts
docker run -itd -p 80:80 -p 443:443 --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/application/html:/usr/share/nginx/html nginx:latest
Copy the code
- Executing script commands
docker run -itd -p 80:80 -p 443:443 --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/application/html:/usr/share/nginx/html nginx:latest
Copy the code
Configure Nginx
- Set the number of Nginx processes to [worker_processes] : The default value can be the same number of CPU cores. If the number of concurrent processes is large, the value can be 2
#Check the server CPU information: cat/proc/cpuinfo | grep processor
[root@iZm5efc4cs8k6t2agr7tceZ manager]# cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
processor : 2
processor : 3
[root@iZm5efc4cs8k6t2agr7tceZ manager]#
Copy the code
[1].4 CPU (4 Core) + 4 Worker_processes (each worker_processes uses one CPU) Worker_processes 4. worker_cpu_affinity 0001 0010 0100 1000; [2].8 CPU (8 Core) + 8 Worker_processes (each worker_processes uses one CPU) worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; [3].16 CPU (16 Core) + 16 Worker_processes (each worker_processes uses one CPU) worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
- Configure event processing model [events]:
Worker_rlimit_nofile 65535; Events {# use high-performance epoll event driver, processing efficiency is high use epoll; accept_mutex on; # enable multi_accept on to accept multiple new network connection requests at the same time; Worker_connections specifies the maximum number of client connections allowed by a single process. }Copy the code
- Enable efficient transmission mode
Sendfile on; # The sendFile mode needs to be enabled to be effective to prevent network congestion and actively reduce the number of network packet segments. Send the response header with the beginning of the body rather than one after the other. tcp_nopush on; tcp_nodelay on;Copy the code
- Enable transmission compression
gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;Copy the code
Copyright notice: This article is the blogger’s original article, in accordance with the relevant copyright agreement, if reprinted or shared, please attach the original source link and link source.