Report monitoring data from a non-Alibaba Cloud host via an NGINX proxy

Updated at:

This topic describes how to use an NGINX proxy server to report monitoring data from a non-Alibaba Cloud host to Cloud Monitor.

Step 1: Deploy an NGINX proxy

Since Cloud Monitor is deployed on Linux servers, we recommend using a Linux server as the proxy server. This topic uses CentOS as an example.

Important

You must use the administrator account (root) to deploy the NGINX proxy server. Improper use of an administrator account can cause system instability or data security issues. Proceed with caution.

  1. Download and decompress the NGINX installation package. This topic uses nginx-1.19.6 as an example.

    1. Navigate to the installation directory and download the installation package. To download other versions, visit the official NGINX website.

      cd /usr/local
      wget http://nginx.org/download/nginx-1.19.6.tar.gz
    2. Decompress the installation package.

      tar -zxvf nginx-1.19.6.tar.gz
  2. Install dependencies.

    yum install -y git patch pcre pcre-devel gcc zlib zlib-devel openssl openssl-devel
  3. Download and install the NGINX patch package. This topic uses proxy_connect_rewrite_1018.patch as an example.

    1. Navigate to the decompressed nginx-1.19.6 directory.

    2. Run the following command to download the NGINX patch package.

      git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
      Important

      Download the patch package version that corresponds to your NGINX installation. This topic uses NGINX 1.19.6, so the corresponding patch package is proxy_connect_rewrite_1018.patch. For more information, visit GitHub.

    3. Install the NGINX patch package. Make sure you are in the /usr/local/nginx-1.19.6 directory.

      patch -p1 < ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
      Note

      patch: Applies changes from a patch file to a source file.

      -p1: This parameter strips a specified number of leading slashes from the file paths in the patch file. A value of 1 strips the first leading slash and its directory.

  4. Compile, install, and then start NGINX.

    1. In the /usr/local/nginx-1.19.6 directory, run the compile command.

      ./configure --prefix=/usr/local/nginx-1-19 --with-http_stub_status_module --with-http_ssl_module --add-module=ngx_http_proxy_connect_module
      Note

      The --prefix=/usr/local/nginx-1-19 parameter specifies the installation directory for NGINX.

    2. Run the installation command.

      make && make install
    3. Run the command to start NGINX.

      /usr/local/nginx-1-19/sbin/nginx
  5. To verify the installation, enter <IP_address_of_the_proxy_server>:80 in your browser's address bar. A Welcome to nginx! page indicates that the NGINX proxy server is installed and running correctly.

  1. Configure the NGINX proxy.

    1. In the conf directory of your NGINX installation, create a forward.conf configuration file.

      cd /usr/local/nginx-1-19/conf
      vi forward.conf
    2. Configure the forward proxy and reverse proxy in the forward.conf file.

      1. The following code provides a sample forward proxy configuration:

        server {
             listen                         8080; # Set a custom port for the NGINX proxy server.
             server_name                    xxx.xx.xx.xxx; # Set the IP address of the proxy server.
             # dns resolver used by forward proxying
             resolver                       114.114.114.114;  # Set the DNS address.
             # forward proxy for CONNECT request
             proxy_connect;
             proxy_connect_allow            443;
             proxy_connect_connect_timeout  10s;
             proxy_connect_read_timeout     10s;
             proxy_connect_send_timeout     10s;
             # forward proxy for non-CONNECT request
             location / {
                 proxy_pass http://$http_host$request_uri;  # Set the protocol and address of the proxy server. Use the default values.
                 proxy_set_header Host $host;
             }
         }
        Note

        Forward proxy: A forward proxy acts as an intermediary for clients within a private network to access resources on the public internet.

        By default, the NGINX forward proxy does not support HTTPS. You must apply a patch to enable HTTPS support.

      2. The following code provides a sample reverse proxy configuration:

        server {
            listen              443 ssl;
            server_name         192.168.XX.XX; # Set the IP address of the proxy server.
            ssl_certificate     XXXX.pem; # The SSL certificate.
            ssl_certificate_key XXXX.key; # The key for the SSL certificate.
            location / {
                proxy_pass  https://www.aliyun.com; # The URL that the proxy server accesses.
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        }
        Note

        Reverse proxy: A reverse proxy acts as an intermediary for clients on the public internet to access resources within a private network.

        Important

        For more information about applying for an SSL certificate, see Apply for a free DV single-domain trial certificate or Submit a certificate application.

    3. Press the Esc key, enter :wq, and then press Enter to save and exit the forward.conf configuration file.

    4. Open the main nginx.conf configuration file.

      cd /usr/local/nginx-1-19/conf
      vi nginx.conf
    5. In the main nginx.conf file, add an include directive for forward.conf.

      http {
        ......
          include /usr/local/nginx-1-19/conf/forward.conf;
        ......
      }
    6. Press the Esc key, enter :wq, and then press Enter to save and exit the nginx.conf file.

    7. Restart the NGINX proxy server.

      /usr/local/nginx-1-19/sbin/nginx -s reload
  2. Test the NGINX proxy server.

    1. To test the forward proxy, run the following command. A successful connection indicates that the configuration is correct.

      curl -x <proxy_server_ip>:<proxy_server_port> http://example.com
    2. To test the reverse proxy, run the following command. The configuration is successful if you are redirected to the URL specified in the forward.conf file, regardless of the URL you request.

      curl -x <proxy_server_ip>:<proxy_server_port> https://example.com

Step 2: Install and configure the Cloud Monitor agent

Important

You can manage the Cloud Monitor agent only by using the root user account (an administrator account). However, using an administrator account may introduce certain risks, such as system stability or data security issues. Proceed with caution.

  1. Install the Cloud Monitor agent on the non-Alibaba Cloud host.

    For more information, see Install and uninstall the C++ agent.

  2. Configure the Cloud Monitor agent to use the NGINX proxy server.

    1. Log on as root to the host where the Cloud Monitor agent is installed.

    2. Run the following commands to open the agent.properties file.

      cd /usr/local/cloudmonitor/conf
      vi agent.properties
    3. Add the NGINX proxy server information to the agent.properties file.

      http.proxy.auto=false
      # Manually configure the proxy.
      http.proxy.host=192.168.XX.XX
      # The IP address of the NGINX proxy server.
      http.proxy.port=8080
      # The port of the NGINX proxy server.
      #http.proxy.user=user  # The HTTP service of the NGINX proxy server does not require a username.
      #http.proxy.password=password  # The HTTP service of the NGINX proxy server does not require a password.
    4. Press the Esc key, enter :wq, and then press Enter to save and exit the agent.properties file.

    5. Run the following command to restart the Cloud Monitor agent.

      ./cloudmonitorCtl.sh restart

Step 3: View the monitoring data

  1. Log on to the Cloud Monitor console.

  2. In the left-side navigation pane, click Host Monitoring.

  3. On the Host Monitoring page, click the name of the target host, or click Monitoring Charts in the Actions column.

    View the monitoring data of the non-Alibaba Cloud host.