nginx learning notes

nginx learning notes (3)

1, Install nginx using yum or up2date

Visit nginx's official website: http://www.nginx.org/

Configure the official website of the Yum source: http://nginx.org/en/linux_packages.html

# Turn off fire protection and selinux

[root@nginx ~]# yum install yum-utils -y
[root@nginx ~]# vim /etc/yum.repo.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@nginx ~]# yum makecache
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# nginx -V
[root@nginx ~]# systemctl start nginx 
[root@nginx ~]# systemctl enable nginx 
[root@nginx ~]# repoquery -ql nginx

2, Source installation

[root@nginx ~]# yum -y install gcc gcc-c++  pcre pcre-devel gd-devel  openssl openssl-devel openssl openssl-devel 
[root@nginx ~]# useradd nginx
[root@nginx ~]# passwd nginx
[root@nginx ~]# yum -y install wget
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@nginx ~]# tar xzf nginx-1.20.1.tar.gz -C /usr/local/
[root@nginx ~]# cd /usr/local/nginx-1.20.1/
[root@nginx ~]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
**# View modules installed by nginx**
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V
--prefix=/usr/local/nginx                        //Point to the installation directory
--conf-path=/etc/nginx/nginx.conf                //Specify profile
--http-log-path=/var/log/nginx/access.log        //Specify access log
--error-log-path=/var/log/nginx/error.log        //Specify the error log
--lock-path=/var/lock/nginx.lock                 //Specify lock file
--pid-path=/run/nginx.pid                        //Specify pid file
--http-client-body-temp-path=/var/lib/nginx/body    //Set http client request temporary file path
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     //Set http fastcgi temporary file path
--http-proxy-temp-path=/var/lib/nginx/proxy         //Set http proxy temporary file path
--http-scgi-temp-path=/var/lib/nginx/scgi           //Set http scgi temporary file path
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         //Set http uwsgi temporary file path
--with-debug                                        //Enable debug logging
--with-ipv6                                         //Enable ipv6 support
--with-http_ssl_module                              //Enable ssl support
--with-http_stub_status_module                      //Gets the status of nginx since it was last started
--with-http_realip_module                 //It is allowed to change the IP address value of the client from the request header. The default is off
--with-http_auth_request_module           //Implement client authorization based on the result of a sub request. If the 2xx response code returned by the sub request, the access is allowed. If it returns 401 or 403, access is denied with the corresponding error code. Any other response code returned by the subrequest is considered an error.
--with-http_addition_module               //As an output filter, it supports incomplete buffering and partial response to requests
--with-http_dav_module                    //Add put, delete and mkcol: to create a set, the COPY and MOVE methods are closed by default and need to be compiled
--with-http_geoip_module                  //Use the precompiled MaxMind database to parse the client IP address and get the variable value
--with-http_gunzip_module                 //It decompresses the response with the "content encoding: gzip" header for clients that do not support the "gzip" encoding method.
--with-http_gzip_static_module            //Online real-time compression of output data stream
--with-http_spdy_module                   //SPDY can shorten the loading time of web pages
--with-http_sub_module                    //Allows you to replace some text in the nginx response with some other text
--with-http_xslt_module                   //Filter transform XML requests
--with-mail                               //Enable POP3/IMAP4/SMTP proxy module support
--with-mail_ssl_module                    //Enable ngx_mail_ssl_module supports enabling external module support
--with  Indicates that you need to nginx Added module
--without Representation compilation nginx The module is added by default. When this parameter is used, it means that the module compiled by default is removed
[root@nginx ~]# make && make install
[root@nginx ~]# /usr/local/nginx/sbin/nginx -V
# Check whether the nginx configuration file is correct after changing the configuration file
[root@nginx ~]# /usr/local/nginx/sbin/nginx -t
[root@nginx ~]# mkdir -p /tmp/nginx
# Absolute path service
[root@nginx ~]#  /usr/local/nginx/sbin/nginx
# Heat load
[root@nginx ~]#  /usr/local/nginx/sbin/nginx -s reload     
[root@nginx ~]# 
[root@nginx ~]# 
[root@nginx ~]# vim  /usr/local/nginx/conf/nginx.conf
 Profile:

# Global parameter settings
user nginx;  #Set users used by nginx
worker_processes  4;          #Set the number of nginx startup processes, which is generally the same as the number of logical CPUs 
error_log  logs/error.log;    #Specify the error log 
pid        /var/run/nginx.pid; 
events { 
    worker_connections  1024; #Set the maximum number of concurrent connections for a process 
}
# http service related settings 
http { 
    include      mime.types; #Associated mime type, media type of associated resource
    default_type  application/octet-stream; #Match the corresponding MIME type according to the suffix of the file
    log_format  main  'remote_addr - remote_user [time_local] "request" '
                      'status body_bytes_sent "$http_referer" '
                      '"http_user_agent" "http_x_forwarded_for"'; 
    access_log  /var/log/nginx/access.log  main;    #Set the location and format of the access log 
    sendfile          on; #It is used to enable the efficient file transfer mode. It is generally set to on. If nginx is used for disk IO load application, it can be set to off to reduce the system load
    tcp_nopush        on;      # Reduce the number of network message segments. When there is data, don't rush to send it first to ensure that the data packet is full of data and avoid network congestion
    tcp_nodelay       on;      # Improve I/O performance, ensure that data is sent as soon as possible, and improve data transmission efficiency 
    gzip              on;      #Whether to enable gzip compression and remove comments 
    keepalive_timeout  65;     #Set the timeout for a long connection and how long to keep the connection after the request is completed,
# Related settings of virtual server 
    server { 
        listen      80;        #Set the listening port 
        server_name  localhost;        #Set the host name, domain name, or ip address of the binding 
       charset koi8-r;        # Set encoding character 
        location / { 			# Match level
            root  /var/www/nginx;           #Set the root directory location of the default web site of the server, which needs to be created manually
          index  index.html index.htm;    #Set the default open document 
            } 
        error_page  500 502 503 504  /50x.html; #Set error message return page 
        location = /50x.html { 
            root  html;        #The absolute position here is / usr/local/nginx/html
     } 
   } 
 }

2.1. nginx. Composition of conf: nginx Conf consists of three parts: Global block, events (model) block and http block. The http block also includes http global block and multiple server blocks. Each server block contains a server global block and multiple location blocks. The configurations nested in the unified configuration block are fast, and there is no order relationship between them.

2.2. Composition of nginx:

nginx binary executable -- startup and shutdown, compiled by each module
nginx.conf configuration file -- control nginx
access.log access log -- records each http request
error.log error log -- locating problems
{{uploading-image-882799.png(uploading...)}}

Control nginx service through nginx command
nginx -c /path/nginx.conf # Start nginx with a configuration file in a specific directory:
nginx -s reload # Reload takes effect after modifying the configuration
nginx -s stop # Quick stop nginx
nginx -s quit # Normal stop nginx
nginx -t # Test whether the current configuration file is correct
nginx -t -c /path/to/nginx.conf # tests whether the specific nginx configuration file is correct

be careful:

The nginx -s reload command loads the modified configuration file. The following events occur after the command is issued

2.1.1. The master process of nginx checks the correctness of the configuration file. If there is an error, an error message is returned. Nginx continues to work with the original configuration file (because the worker is not affected)
2.1.2. Nginx starts a new worker process and adopts a new configuration file
2.1.3. Nginx assigns new requests to new worker processes
2.1.4. Nginx waits until all requests of previous worker processes have been returned, and then closes the related worker processes
2.1.5. Repeat the above process until all old worker processes are closed
For the compilation and installation of nginx source code, I wrote a script, which is applicable to the installation of gcc-c + + compilation tools only, and the rest depends on the source code installation. The use scenario is that when the Intranet environment server has just installed the system and installed deployment tools, there will be no copying and pasting here, and there is a private chat that needs to be exchanged.

Added by coffeecup on Wed, 19 Jan 2022 22:42:39 +0200