Using nginx + fastcgi to implement image recognition server

Using nginx + fastcgi to implement image recognition server

background

A specific device is used to perform inference on the deep learning model. The machine only provides a C++ encapsulated API for loading, starting and inferring the model. Caffe is still used for model training. The model needs to be converted into a format supported by the device. The conversion of the model will not be introduced here. In order to make the model reasoning into a service, we had to use C++ to build an HTTP service, so that the user could post a picture through the HTTP service, the server would start the model reasoning, implement the model prediction, and return the result to the client.

Overall framework

The service content of the short service is to pre-process the received images and then perform model inference. What needs to be done now is to introduce HTTP service

Preliminary research

For a C++ novice, the initial research is of course to search first. The results of http server c++ search processing are also varied. Some teach you how to implement an http server, some use a third-party library, and some directly deal with a bunch of code. . . I saw this on stackoverflow:

why

not try NGINX with fcgi-function mapping?

Implementation steps

Nginx is a magic tool for proxy and is often used for load balancing. As long as the content of my client is sent to nginx, and then nginx forwards the data to fcgi-related applications, all I need to do is combine fcgi with my reasoning program.

nginx

Simply put, nginx is the middleman. The client sends the request to the middleman, who goes to the source of goods to pick up the goods and then responds to the customer:

The customer tells nginx that I want to buy ** product, and nginx goes to the corresponding service provider to get the corresponding service and returns it to the customer.

What we need now is to implement the fcgi part, so what is fcgi?

cgi

Common Gateway Interface (CGI) is an important Internet technology that allows a client to request data from a web browser to a program executed on a network server. CGI describes a standard for transferring data between a server and a request processing program.

The standard input and output here correspond to some environment variables, which mainly include three categories: request-related environment variables, server-related environment variables, and client-related environment variables.

fastcgi

FastCGI is actually CGI with some extended functions added. It is an improvement of CGI. It is also a standard for describing data transmission between client and Web server programs.

FastCGI aims to reduce the overhead of interaction between Web servers and CGI programs, allowing Web servers to handle more Web requests simultaneously. Unlike CGI, which creates a new process for each Web request, FastCGI uses persistent processes to handle a series of Web requests, which are managed by the FastCGI process manager instead of the Web server.

Why do we say that it reduces the cost of interaction? This depends on the difference between the two treatment methods!

The workflow of cgi:

Whenever a client sends a new request, a CGI child process must first be created. After CGI processes the request, as many CGI child processes as there are connections will be started. When the number of requests is large, a large amount of system resources will be occupied.

fastcgi

Fastcgi uses continuous processes to handle a series of requests. These processes are managed by the fastcgi process manager. The specific process is as follows:

It can also be compared like this:

cgi is selling egg-filled pancakes. When customers want to eat, he starts to light the fire, beat the eggs, spread the pancakes, and then turns off the fire. Then wait for the next customer

fastcgi is the old version of a breakfast shop. It employs a group of waiters to prepare meals that need to be cooked on the spot. The boss only needs to arrange the orders, and the waiters are responsible for serving porridge and pancakes.

Specific steps

  • Building a C++ development environment
  • Build nginx
  • Install fastcgi
  • Install fastcgi's process manager spawn-cgi
  • Write and run programs
  • Compile and run

If you want to do your job well, you must first sharpen your tools and build the environment first!

By reading a lot of blog content, I found the simplest installation steps. Many of them are by downloading the source code and then compiling it through make. However, for these more commonly used libraries, they are already integrated in the software package.

C++ Development Environment Installation

apt-get install build-essential

nginx

apt-get install nginx

fastcgi

sudo apt-get install libfcgi-dev

spawn-fcgi

apt-get install spawn-fcgi

Write and run programs

#include <iostream>
#include "fcgio.h"
 
using namespace std;
 
int main(void) {
 // Backup the stdio streambufs
 streambuf * cin_streambuf = cin.rdbuf();
 streambuf * cout_streambuf = cout.rdbuf();
 streambuf *cerr_streambuf = cerr.rdbuf();
 
 FCGX_Request request;
 
 FCGX_Init();
 FCGX_InitRequest(&request, 0, 0);
 
 while (FCGX_Accept_r(&request) == 0) {
  fcgi_streambuf cin_fcgi_streambuf(request.in);
  fcgi_streambuf cout_fcgi_streambuf(request.out);
  fcgi_streambuf cerr_fcgi_streambuf(request.err);
 
  cin.rdbuf(&cin_fcgi_streambuf);
  cout.rdbuf(&cout_fcgi_streambuf);
  cerr.rdbuf(&cerr_fcgi_streambuf);
 
  cout << "Content-type: text/html\r\n"
    << "\r\n"
    << "<html>\n"
    << " <head>\n"
    << " <title>Hello, World!</title>\n"
    << " </head>\n"
    << " <body>\n"
    << " <h1>Hello, World!</h1>\n"
    << " </body>\n"
    << "</html>\n";
 
 }
 cin.rdbuf(cin_streambuf);
 cout.rdbuf(cout_streambuf);
 cerr.rdbuf(cerr_streambuf);
 return 0;

Compiling the Program

g++ cgi.cpp -o cgidemo -lfcgi

Modify the nginx configuration file

vi /usr/local/nginx/conf/nginx.conf 

Start nginx

nginx -c /usr/local/nginx/conf/nginx.conf

Verify that nginx is started normally through the browser http://*******:80

Start the spwan-cgi process

spawn-fcgi -a 127.0.0.1 -C 20 -p 7070 ./cgidemo

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Configure Nginx+PHP5 FastCGI server configuration on ubuntu
  • Configuration optimization of Nginx 0.7.x + PHP 5.2.6 (FastCGI) + MySQL 5.1 on a 128M small memory VPS server
  • Nginx+PHP (FastCGI) builds a high-concurrency WEB server (automatic installation script) second edition
  • Nginx0.5.33+PHP5.2.5 (FastCGI) builds a web server that is 10 times better than Apache
  • Deploy nginx, php (including fastcgi), and virtual host configuration in CentOS 6.1 environment
  • Solve the 502 Bad Gateway error encountered by Nginx + PHP (FastCGI)
  • PATH_INFO issue with Nginx (PHP/fastcgi)

<<:  Detailed explanation of custom instructions for Vue.js source code analysis

>>:  Analysis of MySQL joint index function and usage examples

Recommend

Introduction to Docker Quick Deployment of SpringBoot Project

1. Install Docker First open the Linux environmen...

Detailed introduction to CSS font, text, and list properties

1. Font properties color, specifies the color of ...

Learn javascript iterator

Table of contents Introduction What does an itera...

A summary of some of the places where I spent time on TypeScript

Record some of the places where you spent time on...

How to find out uncommitted transaction information in MySQL

A while ago, I wrote a blog post titled "Can...

How to use a game controller in CocosCreator

Table of contents 1. Scene layout 2. Add a handle...

...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...

Web page image optimization tools and usage tips sharing

As a basic element of a web page, images are one ...

How to optimize MySQL performance through MySQL slow query

As the number of visits increases, the pressure o...

Vue implements mobile phone verification code login

This article shares the specific code of Vue to i...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...