Red de conocimiento informático - Conocimiento del nombre de dominio - Cómo usar nginx para generar helloworld

Cómo usar nginx para generar helloworld

Flujo de procesamiento del módulo nginx:

A. El cliente envía mand_t *cmd, void *conf);

/*command*/

Estático ngx_command_t ngx_mands[ ] = {

{ ngx_string("hola_mundo "),

NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,

ngx_command

};

static u _ char ngx _ hola _ mundo[]= " hola mundo ";

static ngx_mands, /*instrucciones del módulo*/

NGX_HTTP_MODULE, /*Tipo de módulo* /

NULL, /* init master */

Vacío, /*Módulo de inicialización*/

NULL, /*Inicializar proceso*/

Vacío, /*Inicializar hilo*/

NULL, /*Salir de hilo*/

NULL, /*Salir de proceso*/

NULL, /*Salir del host*/

NGX _module_v 1_fill

};

static ngx int t

ngx_mand_t *cmd, void *conf )

{

La función ngx_mand_t se utiliza para definir una matriz estática ngx_mands que contiene instrucciones del módulo.

& ltspan style = " font-size:16px;"& gtstatic ngx_command_t ngx_mands[] = {

{ngx_string("hello_world "), //Establece la cadena de nombre del comando, tenga en cuenta que no puede contener espacios. El tipo de datos ngx_str_t se explicará en detalle más adelante.

ngx_http_loc_conf | ngx_conf_no args, // La posición legal de la instrucción de configuración, lo que indica que la posición es parcialmente legal y la instrucción no tiene parámetros.

ngx_mand_t *cmd, void *conf)

0, // Es necesario descubrir los siguientes parámetros, todavía no los he usado.

0,

NULL },

comando nulo ngx

};& lt/span>.

b. static u_charngx_hello_world[]="hello world" es la cadena de salida a la pantalla.

c.ngx_mands, /*Instrucción del módulo*/

NGX_HTTP_MODULE, /*Tipo de módulo*/

NULL, /* init master */

Vacío, /*Módulo de inicialización*/

NULL, /*Proceso de inicialización*/

Vacío, /*Subproceso de inicialización*/

NULL , /*Salir del hilo*/

NULL, /*Salir del proceso*/

NULL, /*Salir del host*/

NGX _module_ v 1 _relleno

};& lt/span>.

Contiene el contenido principal del módulo y la parte de ejecución de la instrucción, que se explicará en detalle en la siguiente sección.

E. El controlador ngx_http_hello_world_handler también es la parte central del módulo hola mundo.

& ltspan style = " font-size:16px;"& gtstaticngx int t

ngx _ http _ hola _ mundo _ handler(ngx _ http _ request _ t * r) //ngx_http_request_t*r tiene acceso a los encabezados del cliente y a los encabezados de la respuesta a enviar.

{

ngx _ int _ t rc

ngx _ buf _ t * b

ngx _ chain _ t out

/* Búfer de salida HTTP*/

r-& gt; encabezados _ fuera . contenido _ len = sizeof(" text/plain ")-1;

r->headers_out.content_type.data=(u_char *)" text/plain";

b = ngx_PC alloc(r->pool, sizeof(ngx _ buf _ t))

salida .buf = b;

salida.next = NULL

b-& gt; p>b-> último = ngx _ hola _ mundo+sizeof(ngx _ hola _ mundo

b-& gt; ;last_buf = 1;

r->headers_out. status = NGX_HTTP_OK;

r ->headers_out.content_length_n = sizeof(ngx_hello_world);

ngx_http_send_header (r);

return ngx _ http _ salida _ filter(r & amp; salida

} & lt/span>.