Nginx installation and basic configuration

Posted by tshafer on Tue, 08 Feb 2022 00:23:13 +0100

Nginx installation

Download nginx

http://nginx.org/en/download.html Download nginx tar package from the official website.
Nginx version: nginx-1.20.1

nginx dependent installation

Installing nginx

  1. Unzip nginx
tar -zxvf nginx-1.20.1.tar.gz
  1. Enter the decompression directory and execute/ configure
  2. Execute make & & make install in the extracted directory

Firewall configuration

  1. View open firewall ports
[root@localhost nginx-1.20.1]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client ssh
  ports: 27017/tcp 80/tcp 8121-8124/tcp 8848/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
  1. If port 80 is not in ports, execute the open command
[root@localhost nginx-1.20.1]# firewall-cmd --add-service=http --permanent
success
[root@localhost nginx-1.20.1]# firewall-cmd --add-port=80/tcp  --permanent
Warning: ALREADY_ENABLED: 80:tcp
success
[root@localhost nginx-1.20.1]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client http ssh
  ports: 27017/tcp 80/tcp 8121-8124/tcp 8848/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
# service iptables restart 
[root@localhost nginx-1.20.1]# firewall-cmd --reload
success

Nginx configuration

nginx common commands

  1. Start command
    /usr/local/nginx/sbin/nginx
  2. close command
    /usr/local/nginx/sbin/nginx -s stop
  3. Reload command
    /usr/local/nginx/sbin/nginx -s reload

nginx.conf configuration file

  1. Under the nginx installation directory, the default configuration files are placed in the conf directory of this directory, and the main configuration file is nginx Conf is also included. The subsequent use of nginx is basically to modify the configuration file accordingly.
[root@localhost conf]# ll
total 40
-rw-r--r--. 1 1001 1001 1077 May 25 20:35 fastcgi.conf
-rw-r--r--. 1 1001 1001 1007 May 25 20:35 fastcgi_params
-rw-r--r--. 1 1001 1001 2837 May 25 20:35 koi-utf
-rw-r--r--. 1 1001 1001 2223 May 25 20:35 koi-win
-rw-r--r--. 1 1001 1001 5231 May 25 20:35 mime.types
-rw-r--r--. 1 1001 1001 2656 May 25 20:35 nginx.conf
-rw-r--r--. 1 1001 1001  636 May 25 20:35 scgi_params
-rw-r--r--. 1 1001 1001  664 May 25 20:35 uwsgi_params
-rw-r--r--. 1 1001 1001 3610 May 25 20:35 win-utf

nginx.conf overall introduction

# Number of worker threads
worker_processes  1;
# events block
events {
    worker_connections  1024;
}
# http
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
  1. Part I: Global block
    From the beginning of the configuration file to the events block, some configuration instructions affecting the overall operation of the nginx server will be set, mainly including configuring the users (groups) running the nginx server, the number of worker process es allowed to be generated, the process PID storage path, log storage path and type, and the introduction of the configuration file.
    For example, the configuration in the first line above:
worker_processes  1;

The key is to configure the worker server for concurrent processing_ The larger the processes value, the more concurrent processing can be supported, but it will be restricted by hardware, software and other devices.

  1. Part II: events block
    For example, the above configuration:
events {
	worker_connections  1024;
}

The events block relates to the network connection between the client and the Nginx server. Common settings include whether to open the network connection under multiple worker processes for serialization, whether to receive multiple network connections at the same time, which event driven model to select to process the connection request, the maximum number of connections that each worker process can indicate at the same time, etc.
The above example shows that the maximum number of connections supported by each work process is 1024.
The configuration of this part has a great impact on the performance of Nginx, so it should be configured flexibly in practice.

  1. Part III: http block
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

This is the most frequently configured part of the Nginx server. Most functions such as proxy, cache and log definitions and the configuration of third-party modules are here. It should be noted that the http block includes http global block and server block.

  • http global block
    http global block configuration instructions include file introduction, MIME-TYPE definition, log customization, connection timeout, maximum number of single link requests, etc.
  • server block
    This is closely related to the virtual host. From the perspective of users, the virtual host is exactly the same as an independent hardware host. This technology is produced to save the hardware cost of Internet server.
    Each http block can include multiple server blocks, and each server block is equivalent to a virtual host.
    Each server block is also divided into global server blocks and can contain multiple location blocks at the same time.
    • server global block
      The most common configurations are the listening configuration of this virtual machine and the name or ip configuration of this virtual machine.
    • location block
      A server block can be configured with multiple location blocks.
      The main function of this part is to match the strings other than the virtual host name (or IP alias) (such as the previous / URI string) based on the request string received by the Nginx server (for example, server_name / URI string), and process specific requests. Address orientation, data caching, response control and other functions, as well as the configuration of many third-party modules are also carried out here.

test

Using the default configuration file, start the nginx server for testing.

# start-up
[root@localhost conf]# /usr/local/nginx/sbin/nginx -c /opt/nginx/nginx-1.20.1/conf/nginx.conf
# Accessing nginx server
[root@localhost conf]# curl http://localhost/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost conf]#

Topics: server