Beginners must understand Nginx from introduction to actual combat ~

Posted by SuperCam on Fri, 24 Dec 2021 02:01:38 +0100

Author: Island
Source: https://segmentfault.com/a/1190000014893012

1, Environment

Server version: CentOS 7.2

In order to ensure that there are no strange things in the learning stage, please ensure the following four points (the great God selectively ignores)

  1. Confirm system network
  2. Confirm that yum is available
  3. Confirm to close iptables
  4. Confirm to disable selinux
#View iptables status
systemctl status firewalld.service
#Turn off the firewall (temporarily)
systemctl stop firewalld.service
#Viewing SELinux status 
getenforce
#Temporarily shut down SELinux 
setenforce 0

Install some basic system tools. Normally, the system will come with it (not installed)

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim

2, What is Nginx?

Nginx is an open source, high-performance and reliable HTTP middleware, proxy service and other HTTP services:

  1. Httpd Apache Foundation
  2. IIS Microsoft
  3. GWS Google (not open to the public)

In recent years, the market share of Nginx has become higher and higher, once soaring. Why? Then we'll know!

3, Why did we choose Nginx?

1. IO multiplexing epoll (IO multiplexing)

How to understand? Take an example! There are three teachers A, B and C. they all encounter A problem and want to help students in A class solve their homework. Teacher A answers questions one by one from the first row. Teacher A wastes A lot of time, and some students haven't finished their homework yet. The teacher comes again and again, and the efficiency is very slow. Teacher B is A ninja. He found that teacher A's method didn't work, so he used the shadow separation technique. He divided several himself to help several students answer questions at the same time. Before he finished answering, teacher B consumed all his energy and collapsed. Teacher C was smart. He told the students who had finished their homework and raised their hands. He went to guide the students who raised their hands. He asked the students to speak on their own initiative and separated the "concurrency". This teacher C is Nginx.

2. Lightweight

  • Few functional modules - Nginx only retains the modules required by HTTP, and others are added in the form of plug-ins the day after tomorrow
  • Code modularization - more suitable for secondary development, such as Alibaba Tengine

3. CPU affinity

Bind the CPU core to the Nginx working process, and fix each worker process to execute on one CPU to reduce the cache miss of switching CPUs, so as to improve performance.

3, Installation and directory

I used brother bird's lnmp integration package, simple and convenient - recommended!

#Execute this statement. According to the instructions, nginx php mysql will be installed. You can go to the lnmp official website to view the more detailed process
#Default installation directory / usr/local
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp

#Default installation directory
/usr/local

4, Basic configuration

#Open the main configuration file if you are installing in lnmp environment
vim /usr/local/nginx/conf/nginx.conf

----------------------------------------

user                    #Set the system user of nginx service
worker_processes        #The number of working processes is generally consistent with the number of CPU cores
error_log               #Error log for nginx
pid                     #pid at nginx startup

events {
    worker_connections    #Maximum number of connections allowed per process
    use                   #Kernel model used by nginx
}

We use nginx's http service in the configuration file nginx In the http area of conf, numerous servers are configured, and each server corresponds to this virtual host or domain name

http {
    ... ...        #http configuration items will be described in detail later

    server {
        listen 80                          #Listening port;
        server_name localhost              #address

        location / {                       #Access home page path
            root /xxx/xxx/index.html       #default directory
            index index.html index.htm     #Default file 
        }        

        error_page  500 504   /50x.html    #When the above status code appears, it is newly defined to 50x html        
        location = /50x.html {             #When accessing 50x HTML time
            root /xxx/xxx/html             #50x.html page location
        }        
    }

    server {
        ... ... 
    } 
}

Multiple location s can appear on a server. We configure different access paths in different situations. Let's take a look at the configuration details of http

http {
    sendfile  on                  #The mode of efficient file transfer must be turned on
    keepalive_timeout   65        #Client server request timeout
    log_format  main   XXX        #Define the log format code as main
    access_log  /usr/local/access.log  main     #Log saving address format code main
}

4, Module

Check the modules that nginx has opened and linked in. There are too many modules, so it's not a long speech. You need to Baidu yourself~

#Use uppercase V to view all modules and lowercase V to view versions
nginx -V
# Check this profile for syntax errors
nginx -tc /usr/local/nginx/conf/nginx.conf

Recent hot article recommendations:

1.1000 + Java interview questions and answers (2021 latest version)

2.Stop playing if/ else on the full screen. Try the strategy mode. It's really fragrant!!

3.what the fuck! What is the new syntax of xx ≠ null in Java?

4.Spring Boot 2.5 heavy release, dark mode is too explosive!

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don't forget to like + forward!