Nginx learning (1) - First introduction to Nginx (compile and install, start and stop, simple configuration)

Nginx distribution version

  1. Nginx open source version : http://nginx.org/
    is relatively clean and mainly completes the website server, proxy server, and load balancing server. There are no other extra features.
  2. Nginx plus commercial version : https://www.nginx.com
    is officially produced by F5 and adds many useful functions to the original nginx.
  3. Openresty : http://openresty.org/
    nginx+lua is perfectly integrated.
  4. Tenginehttp://tengine.taobao.org/

Compile and install Nginx (ubuntu18.04)

  1. Download the source code http://nginx.org/download/nginx-1.22.1.tar.gz

    Unzip the tarball

    tar -zxvf nginx-1.22.1.tar.gz
    
  2. Enter the unzipped directory:cd nginx-1.22.1

  3. Install required dependent libraries:

    sudo apt install gcc
    sudo apt-get install libpcre3-dev
    sudo apt install zlib1g-dev
    
  4. sudo ./configure --prefix=/usr/local/nginx
    The following information is displayed to indicate success
    Insert image description here

  5. Compile:

    sudo make
    sudo make install
    
  6. Start the service:

    cd /usr/local/nginx/sbin
    sudo ./nginx
    

    If an error is reported during startup,
    Insert image description here
    it means that port 80 is occupied
    . Solution:

    sudo netstat -natp| grep 80   	// 找到占用80端口的进程
    sudo killall -9 xxx				// 杀掉该进程
    或者
    sudo service xxx stop			// 停止占用80端口的服务
    

    Insert image description here

    Start nginx again

    Enter the IP address in the browser. If the access times out:

    systemctl stop firewalled.service	// 关闭防火墙
    或者
    firewall -cmd --zone=public --add-port=80/tcp --permanent	// 防火墙放行80端口(nginx默认是占用80端口)
    
  7. Enter the IP address in the browser to access: the following display indicates success
    Insert image description here

Nginx related operations

Nginx start stop command

sudo ./nginx			// 启动
sudo ./nginx -s stop	// 停止
sudo ./nginx -s quit	// 优雅关闭,在退出之前完成已经接收的请求任务
sudo ./nginx -s reload	// 重新加载配置文件

Install Nginx to serve the system

For Linux systems that install Nginx for the first time, the service and systemctl commands are not supported to start.
Insert image description here

The following operations can use the service and systemctl commands to start and stop nginx.

vi /usr/lib/systemd/system/nginx.service	// 没有system目录可以创建一个
systemctl daemon-reload		// 重新加载系统服务

// 输入内容如下,注意安装路径不是/usr/local/nginx的话需要修改成对应的路径
[Unit]
Description=nginx web service
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
privateTmp=true

[Install]
WantedBy=multi-user.target

Directory structure and basic principles of Nginx

Directory Structure:

Insert image description here

Basic operating principle:

Insert image description here
When nginx starts, it will start the main process, which is responsible for reading the configuration file and doing verification. After the verification is successful, it will fork() multiple child processes. After that, the main job of the main process is to coordinate the work of the child processes.
When a user request comes in, the worker sub-process first parses the request (because the sub-process knows the contents of the configuration file, so the sub-process can determine whether the requested resource exists) and finds that the request is to obtain the index.html file. Then respond.

Basic configuration:

Core configuration

Configuration in the nginx.conf file : (minimum configuration required to start nginx)

  • worker_processes 1; // Configure how many worker sub-processes need to be started when starting nginx. This mainly depends on the quality of the server hardware. If the hardware is not good enough and the configuration parameters are high, the efficiency will be lower. The basic configuration logic is mainly one CPU core corresponding to one worker_processes
  • events { worker_connections 1024; } // Event-driven module, worker_connections configures how many connections each worker_processes can create.
  • http{…}:
    • include mime.types; // Configuration include references other configuration files. (mime.types, configure the type of file according to the suffix name, such as photo files, text files, or whatever type is configured in the mime.types file. Note : The type configured in the mime.types file is returned to the client for the client to parse. of)
    • default_type application/octet-stream; //Default file type. The mime.types file cannot always configure all file types, and there will always be unseen suffixes. At this time, the default file type is returned for the client to parse.
    • sendfile on; // Zero copy of data. (Detailed understanding later)
    • keepalive_timeout 65; // Keep the connection timeout time.
    • server{…}: A server module represents a host
      • listen 80; // Refers to the listening port number.
      • server_name localhost; // The host name of the current host. This field can also configure the domain name.
      • location{…} // URI All information needed to locate the resource. A host can be configured with multiple locations.
        • root html; // There is a one-to-one correspondence with the nginx/html folder.
        • index index.html index.htm; //Default page.
      • error_page 500 502 503 504 /50x.html; // When the error code is the configured content, jump to 50x.html
      • location = 50x.html {root html}: When the error file cannot be found, the html folder will be located to find 50x.html

nginx.conf can be started with the above configuration

Guess you like

Origin blog.csdn.net/Stars____/article/details/129137175