Nginx - Status page, third-party module, built-in variable

Posted by raimis100 on Sat, 15 Jan 2022 20:49:17 +0100

nginx status page

Module: - with http_ stub_ status_ module

Syntax:    stub_status;
Default:    —
Context:    server, location

to configure

location /nginx_status {
 stub_status;
 allow 192.168.0.0/16;
 allow 127.0.0.1;
 deny all;
}

visit

Active connections: 291
server accepts handled requests
 16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106

Active connections: the number of currently active client connections, including the number of connections waiting for idle connections.

accepts: Statistics total value, the total number of client requests accepted by Nginx since startup.

handled: the total statistical value. The total number of client requests that have been processed by Nginx since it was started. It is usually equal to accepts, unless there is a worker_connections restrictions and other rejected connections.

Requests: counts the total number of requests sent by the client since Nginx was started.

Reading: current status, the number of connections reading the header of the client request message.

Writing: current status, the number of connections in the process of sending response message to the client.

Waiting: current status, the number of idle connections waiting for the client to send a request. When keep alive is enabled, this value is equal to active – (reading+writing),

Nginx third party module

The third module is to expand the function of nginx. The third party module needs to use parameter --add-module=PATH to specify the path when compiling and installing Nginx. Some modules are customized development by the company's developers for business needs. Some modules are uploaded to GitHub for open source modules after open source enthusiasts are developed. Nginx supports third-party modules, which need to be recompiled from the source code, such as the open source echo module https://github.com/openresty/echo-nginx-module
The module path -- add module = / root / echo nginx module needs to be added for compilation and installation

location /main {
          index index.html;
          default_type text/html;
          echo "hello world,main-->";
          echo_reset_timer;
          echo_location /sub1;
          echo_location /sub2;
          echo "took $echo_timer_elapsed sec for total.";
        }

        location /sub1 {
          echo_sleep 1;
          echo sub1;
        }

        location /sub2 {
          echo_sleep 1;
          echo sub2;
        }

test

]# curl http://www.magedu.net/main
hello world,main-->
sub1
sub2
took 2.006 sec for total.

Nginx variable use

Nginx variables can be referenced in the configuration file for use in scenarios such as function judgment or log. Variables can be divided into built-in variables and user-defined variables. Built in variables are brought by the nginx module. Through variables, many values related to client access can be obtained.

Built in variable

Print the variable value using the echo module

location /about {
          root /data/nginx/html/pc;
          index index.html;
          limit_rate 10240;
          #try_files $uri /about/default.html;
          try_files $uri $uri/index1.html $uri.html /default.html;
          #try_files $uri $uri/index.html $uri.html =489;
          echo $remote_addr;          #The address of the client is stored. Note that the public IP of the client, that is, if a family visits a website, it will be displayed as the public IP of the router.
          echo $args;                 #Variable stores the instructions in the URL, for example http://www.magedu.net/main/index.do?id=20190221&partner=search Id = 20190221 & partner = search in
          echo $document_root;        #The system root directory that holds the request for the current resource, such as / apps/nginx/html
          echo $document_uri;         #The URI that does not contain instructions in the current request is saved. Note that the requested instructions are not included, such as
          echo $host;                 #The host name where the request is stored
          echo $http_user_agent;      #Client browser details
          echo $http_cookie;          #cookie information of the client
          echo $limit_rate;           #If the nginx server uses limit_ If the rate is configured to display the network rate, it will be displayed. If it is not set, 0 will be displayed
          echo $remote_port;          #The port opened randomly when the client requests the Nginx server. This is each client's own port
          echo $remote_user;          #User name authenticated by Auth Basic Module
          echo $request_body_file;    #The name of the local resource sent to the back-end server when acting as a reverse proxy
          echo $request_method;       #Methods of requesting resources, such as GET/PUT/DELETE, etc
          echo $request_filename;     #The path name of the currently requested resource file, and the absolute path of the file generated by the root or alias instruction and the URI request
          echo $request_uri;          #The original URI containing the request parameters, excluding the host name,
          echo $scheme;               #The requested protocol, such as ftp, https, http, etc
          echo $server_protocol;      #It saves the version of the protocol used by the client to request resources, such as HTTP/1.0, HTTP/1.1, HTTP/2.0, etc
          echo $server_addr;          #Saved the IP address of the server
          echo $server_name;          #The host name of the requested server
          echo $server_port;          ##The port number of the requested server
        }

Test access

Google http://www.magedu.net/about/index.html?id=20190221&partner=search

192.168.64.1 
id=20190221&partner=search
/data/nginx/html/pc
/about/index.html
www.magedu.net Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
10240
50243


GET
/data/nginx/html/pc/about/index.html
/about/index.html?id=20190221&partner=search
http
HTTP/1.1
192.168.64.130
www.magedu.net
80

Custom variable

If you need to customize the variable name and value, use the instruction set $variable value;, The method is as follows:
Syntax: set $variable value; Default: — Context: server, location, if

set $name magedu;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";

 

Topics: Nginx