Tian Jiale's Blog

Nginx getting started guide.

Introduction

Nginx is a lightweight HTTP server that uses an event-driven, asynchronous, non-blocking processing framework, which gives it excellent IO performance and is often used for server-side reverse proxying and load balancing.

Quick Start

Install nginx

sudo apt-get install nginx

Uninstall nginx

sudo apt-get uninstall nginx

Start nginx

nginx

Stop nginx

nginx -s stop

Hot restart nginx

nginx -s reload

install in CentOS

Install compilation tools and library files

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

Install Download PCRE

PCRE is used to enable Nginx to support Rewrite.

There are two very problematic areas here, the first is the version of PCRE to install, in PCRE official website we learned that PCRE2 has been released in 2015, according to the law of software updates, now NGINX should be able to support PCRE2, but after the actual installation trial and error found that it does not support PCRE2, and on StackOverflow it even says you need to use PCRE.

The second problem is the compilation parameters when compiling NGINX, -with-pcre and -with-pcre= are added without any problem, the problem lies in the directory of -with-pcre=, as the title of this section says, it is not possible to install PCRE normally and assign the library directory to NGINX, when specifying both When specifying both openssl and pcre, you must specify the source code of both projects, because the openssl system is already available, so we need to specify the source code of pcre, the final version of which is 8.45 after it no longer supports version 1.

  1. Download PCRE source files,Download Links

    Then transfer the file to the server.

  2. Unzip the code package.

    unzip pcre-8.45.zip
    

Install Nginx

  1. Download Nginx,Download Links

    cd /usr/local/src/
    wget https://nginx.org/download/nginx-1.20.1.tar.gz
    
  2. Unzip the installation package

    tar zxvf nginx-1.20.1.tar.gz
    
  3. Compile and install

    # Adding Users and Groups
    groupadd www
    useradd -g www www
    
    # Go to the installation directory
    cd nginx-1.20.1/
    
    # Configuration
    ./configure \
    --user=www \
    --group=www \
    --prefix=/usr/local/nginx \
    --with-stream \
    --with-stream_ssl_preread_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-http_dav_module \
    --with-http_gzip_static_module \
    --with-http_v2_module \
    --with-pcre \
    --with-pcre=/usr/local/src/pcre-8.45
    
    # Compile and install
    make && make install
    

Modify configuration

Set boot-up

  1. Create the nginx.service file in the system services directory

    vi /usr/lib/systemd/system/nginx.service
    
  2. Write in the following

    [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
    

    [Unit]: name of the service

    Description: Description of the service

    After: Describe the service category

    [Service]: Setting of service operation parameters

    Type=forking: It is the form that runs in the background

    ExecStart Specific run commands for the service

    ExecReload for the reboot command

    ExecStop For the stop command

    PrivateTmp=True Indicates that a separate temporary space is allocated to the service

    Caution.[Service] requires absolute paths for all start, restart and stop commands.

    [Install] Settings related to service installation under runlevel can be set to multi-user, i.e. system runlevel is 3

  3. Set boot-up

    systemctl enable nginx.service
    

    A reboot of the server is required after this step for successful configuration.

General Catalog (in docker)

  • /etc/nginx/nginx.conf Configuration file path
  • /usr/share/nginx/html The default root directory of the server
  • /var/log/nginx The default log directory of the nginx server

Configuration file structure

...              #Global block

events {         #events block
   ...
}

http      #http block
{
    ...
    server        #server blocks
    {
        ...
        location [PATTERN]   #location blocks
        {
            ...
        }
        location [PATTERN]
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...
}
  • Global block:Configure directives that affect nginx globally. There are generally user groups running the nginx server, nginx process pid storage paths, log storage paths, configuration file introduction, number of worker processes allowed to be generated, etc.
  • events block:Configure network connections that affect the nginx server or to users. There is a maximum number of connections per process, which event-driven model is chosen to handle connection requests, whether to allow multiple network connections to be accepted at the same time, turn on serialization of multiple network connections, and so on.
  • http block:Multiple servers can be nested. yong’y configures proxy, cache, log definition, and most other features and third-party module block configurations. Such as file introduction, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, number of single connection requests, etc.
  • server blocks:Configure the parameters of the virtual host, which can have multiple servers in one http.
  • location blocks:Configure the routing of requests, and the processing of various pages.

Official documentation: Link