Nginx core modules and configuration practices


Summary:

  1. Nginx profile
  2. Nginx architecture description
  3. Nginx basic configuration and use

1. Introduction and installation of Nginx

Knowledge:

  1. Nginx profile
  2. Nginx compilation and installation
  3. Nginx module updated

1.1 introduction of Nginx

Nginx is a high-performance WEB server. In addition to Apache, Tomcat, Jetty and IIS, they are all WEB servers, or World Wide WEB (WWW) servers, and have the basic functions of WEB servers accordingly. What advantages does Nginx have over other WEB services?

  1. Tomcat and Jetty are heavyweight WEB servers for the Java language, and their performance is not comparable to Nginx.

  2. IIS runs only on Windows operating systems. Windows is less stable as a server than uniX-like operating systems in terms of stability and other performance, so IIS is not superior in situations where a high-performance Web server is required.

  3. Apache development period is long, and it is now arguably the world’s first big Web server, it has many advantages, such as stability, open source, cross-platform, but it appears the time is too long, the rise of the s, the Internet industry scale than today, so it is designed to become a leading, the Web server does not support high concurrency. On the Apache server, if tens of thousands of concurrent HTTP requests are accessed at the same time, the server will consume a lot of memory. Switching between hundreds of Apache processes by the operating system kernel will consume a lot of CPU resources and reduce the average response speed of HTTP requests. All of this made It impossible for Apache to become a high-performance Web server, which led to the emergence of Lighttpd and Nginx. The chart below shows the strong growth momentum for 18 years.

1.2 Compilation and Installation

Installation environment Preparations:

(1) Linux kernel 2.6 and later:

Epool was only supported after 2.6. Prior to that, the USE of SELECT or Pool multiplexing IO models did not solve the problem of high concurrency pressures. To view the information, run the uname -a command.

# Check the Linux kernel uname -aCopy the code

(2) GCC compiler

GNU Compiler Collection (GCC) is used to compile C programs. Nginx does not provide binary executables directly, only the source code can be downloaded and compiled.

(3) PCRE library

PCRE (Perl Compatible Regular Expressions) is a library developed by Philip Hazel that supports Regular Expressions.

(4) Zlib library

The zlib library is used to compress the contents of HTTP packages in gzip format, if we configure gzip on in nginx.conf and specify gzip for some content-type HTTP responses to reduce network traffic.

(5) OpenSSL development library

If our server is not only going to support HTTP, but also needs to transport HTTP over the more secure SSL protocol, then we need to have OpenSSL. Also, if we want to use hash functions such as MD5 and SHA1, we need to install it. The above libraries are all necessary for the basic functionality of Nginx. For simplicity, we can install them using the yum command.

#yum install make zlib zlib-devel GCC c++ libtool openssl openssl-devel pcre pcre-devel #yum install zlib-devel GCC c++ libtool openssl openssl-devel pcreCopy the code

Source code:

Nginx download page: nginx.org/en/download… .

Nginx # download the latest stable version of wget http://nginx.org/download/nginx-1.19.1.tar.gz # extract tar - ZXVF nginx - 1.19.1. Tar. GzCopy the code

Easiest installation:

/configure make && make installCopy the code

The nginx run file is installed in /usr/local/nginx.

Parameter-based construction

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-debug
Copy the code

Control command:

# default boot mode: /sbin/nginx -c/TMP /nginx.conf # Specify the nginx program directory to start./sbin/nginx -p /usr/local/nginx /sbin/nginx -s reload # Reopen the file./sbin/nginx -s reload # reopen the file./sbin/nginx -s reloadCopy the code

1.3 Module Updates

The following uses the third-party ngx_HTTP_google_filter_module module as an example.

Nginx modules are required to recompile Nginx, rather than configuration files referencing.so like Apache

Download the third-party extension module ngx_http_google_filter_module

# cd /data/software/
# git clone https://github.com/cuber/ngx_http_google_filter_module
Copy the code

See which modules are installed when nginx is compiled and installed

Switch the command line to the directory where the Nginx executable resides and type./ Nginx -v as follows:

[root@hjh sbin]# ./nginx -V nginx version: Nginx /1.19.1 BUILT by GCC 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) Built with OpenSSL 1.0.2 22 Jan 2015 TLS SNI support enabled configure arguments: -- prefix = / usr/local/nginx 1.19.1 - with - openssl = / usr/local/SRC/openssl - 1.0.2 - with - pcre = / usr/local/SRC/pcre - 8.37 - with - zlib = / usr/local/SRC/zlib - 1.2.8 - with - http_ssl_module [root @ HJH sbin] #Copy the code

You can see that the following parameters are used to compile and install Nginx:

-- prefix = / usr/local/nginx 1.19.1 - with - openssl = / usr/local/SRC/openssl - 1.0.2 - with - pcre = / usr/local/SRC/pcre - 8.37 - with - zlib = / usr/local/SRC/zlib - 1.2.8 - with - http_ssl_moduleCopy the code

Add the modules you want to install and recompile

Add –add-module=/data/software/ngx_http_google_filter_module

. / configure -- prefix = / usr/local/nginx 1.19.1 - with - openssl = / usr/local/SRC/openssl - 1.0.2 - with - pcre = / usr/local/SRC/pcre - 8.37 - with - zlib = / usr/local/SRC/zlib - 1.2.8 - with - http_ssl_module - - add - the module = / data/software/ngx_http_google_filter_moduleCopy the code

–add-module=/data/software/ngx_http_google_filter_module –add-module=/data/software/ngx_http_google_filter_module

# make // Do not make installCopy the code

Note here: Do not execute the make install command.

Replace nginx binaries

/usr/local/nginx-1.19.1/sbin/nginx /usr/local/nginx-1.19.1/sbin/nginx.bak Copy the newly compiled nginx executable to /usr/local/nginx-1.19.1/sbin/ # cp /opt/ nginx-sbin/nginx /usr/local/nginx-1.19.1/sbin/Copy the code

2. Nginx architecture description

2.1 Nginx architecture Diagram


2.2 Architecture Description

  1. When nginx starts, it generates two types of processes: a Master process, one (currently only one on Windows), and multiple Worker processes. The main process does not handle network requests, but schedules worker processes, which are the three shown here: loading configuration, starting worker processes, and non-stop upgrades. So, after nginx is started, if you look at the operating system process list, you can see that there are at least two Nginx processes.

  2. The server actually handles network requests and responds to worker processes. On unix-like systems, Nginx can be configured with multiple workers, and each worker process can handle thousands of network requests at the same time.

  3. Modular design. Nginx worker includes core and functional modules. The core module is responsible for maintaining a run-loop and performing module functions at different stages of network request processing, such as network read and write, storage read and write, content transfer, outgoing filtering, and sending requests to upstream servers. The modular design of its code also makes it possible for us to select and modify the functional modules appropriately according to needs and compile them into servers with specific functions.

  4. Event-driven, asynchronous, and non-blocking are key to nGINx’s high concurrency and high performance, as well as the adoption of event notification and I/O performance enhancements such as Kqueue, Epoll, and Event ports in Linux, Solaris, and BSD-like operating system kernels.

2.3 Nginx core module


3. Configure and use Nginx

knowledge

  1. Configure the syntax format of the file
  2. Configure the first static WEB service
  3. Configuration case
  4. Static and static separation implementation
  5. Preventing hotlinking
  6. Multi-domain site
  7. Download speed limit
  8. IP blacklist
  9. User-agent-based shunt
  10. The log configuration

3.1 Syntax of the Configuration file

Let’s start with a simple Nginx configuration

worker_processes  1;

events {

    worker_connections  1024;

}

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;

        }

        location /nginx_status {

        stub_status on;

        access_log   off;

       }

    }

}

Copy the code

Events, HTTP, server, location and upstream in the above configuration belong to the configuration item block. Worker_processes, worker_connections, include, and LISTEN are attributes in the configuration item block. / nginx_STATUS specifies a parameter parameter that belongs to the configuration block. The Server block is nested in the HTTP block and can directly inherit and access the parameters in the HTTP block.

The configuration block A name begins with a large number wrapped around its corresponding property
attribute Based on the whitespace split attribute name and attribute value, the attribute value may have multiple entries split by Spaces, such as access_log logs/host.access.log main
parameter It is configured between the block name and braces, and is separated by Spaces if there are more than one value

Notice If the value of a configuration item contains a syntax symbol, such as a space character, use single or double quotation marks to enclose the value. Otherwise, Nginx will report a syntax error. Such as:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
Copy the code

3.2 Configuring the first static WEB service

Basic site demo:

  • Create the site directory mkdir -p /usr/www/test
  • Writing a static file
  • Configure nginx. Conf
  • Configure the server
  • Configure the location

Basic configuration description:

(1) Listening port

Syntax: Listen address:

Default: listen 80;

Configuration block: Server

Server_name name[…] ;

Default: server_name “”;

Configuration block: Server

Server_name can be followed by multiple host names, such as server_name www.testweb.com and download.testweb.com. . Supports wildcard and re characters

(3) the location

Grammar: the location [= | | | ~ ~ * ^ ~ | @] / uri / {… }

Configuration block: Server

  1. = indicates that the URI is taken as a string to match exactly the URI in the parameter.
  2. / Based on uri directory matching.
  3. ~ indicates that the re is case-sensitive when matching urIs.
  4. ~ * indicates that case is ignored when matching urIs.
  5. ^ ~ indicates that only the first half of the re matches the URI parameter.

Static and static separation demonstration:

  • Creating a Static site
  • Configure the location/static
  • Configuration ~ *. (GIF | PNG | | js, CSS) $

Dynamic and static separation based on directory

server { listen 80; server_name *.test.com; root /usr/www/test; location / { index test.html; } location /static { alias /usr/www/static; }}Copy the code

Based on regular static and static separation

location ~* \.(gif|jpg|png|css|js)$ {
      root /usr/www/static;
}
Copy the code

Anti-theft chain configuration demonstration:

Valid_referers None blocked *.test.com; if ($invalid_referer) { return 403; }Copy the code

Download speed limit:

location /download {
    limit_rate 1m;
    limit_rate_after 30m;
}
Copy the code

Creating an IP Address Blacklist

Echo 'deny 192.168.0.132; '>> balck. IP # HTTP configuration block include black.ip;Copy the code

3.3 Log Configuration

Log format:

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 logs/access.log main; Access_log /$host.access.log main;Copy the code

Error log Settings

Syntax: error_log /path/file level;

Default: error_log logs/error.log error;

Level indicates the log output level. The value can be DEBUG, INFO, NOTICE, WARN, error, crit, Alert, or emerg.

Generates debug logs for the specified client

Grammar: debug_connection [IP | CIDR]

Events {debug_connection 192.168.0.147; Debug_connection 10.224.57.0/200; }Copy the code

This article is complete with nginx.conf

user root; worker_processes 2; worker_cpu_affinity 10 01; worker_priority -10; worker_rlimit_nofile 20; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; Debug_connection 192.168.0.1. use epoll; } http { include mime.types; include black.ip; default_type application/octet-stream; log_format main '$host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name *.test.com default; root /usr/www/test; #access_log logs/$server_name.access.log main; access_log logs/$host.access.log main; location / { index test.html; } location /static { alias /usr/www/static; } location ~* \.(gif|jpg|png|css|js)$ { root /usr/www/static; valid_referers none blocked *.test.com; if ($invalid_referer) { return 403; } } location /download { limit_rate 1m; limit_rate_after 30m; } } server { listen *:80; server_name www.tl.com ; #charset koi8-r; #access_log logs/host.access.log main; location ~ \.(gif|jpg|jpeg|png|bmp|ico|css)$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; Proxy_pass http://127.0.0.1:8010; } location =/baidu.html { proxy_pass http://www.baidu.com; } location /nginx_status { stub_status on; access_log off; } location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php${# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php${# root HTML; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:! aNULL:! MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # #}}Copy the code

Copy the code

}


Some pictures from the network, copyright to the original author, delete.Copy the code