Nginx (1)

Source: Internet
Author: User
Tags install openssl

Crul Sina Weibo found that the nginx server was used on the opposite side. They also found that they also used nginx in the discussion of the World Cup in tigers (a very good football Forum, I think Alibaba's tengine is based on nginx and I think it is necessary to understand nginx.

1. nginx Installation

Nginx installation is similar to installation of Common Open-Source Software (makefile is used). The specific process is to download the source file./configure, make, and make install.

In the./configure process, you will be prompted that some dependent components cannot be found. install them on your own. You can install the corresponding components by compiling the source code. You can also use the built-in installation method. For example, when you use fedora to install the OpenSSL components,

Sudo Yum install OpenSSL-devel will add the corresponding programming Association file here. If it is just sudo Yum install OpenSSL, an error may occur.

./Some useful parameters exist in the configure process, such

./Configure -- prefix =/.../-- with-http_ssl_module -- With-OpenSSL =/.../-- add-module =/home /..

If these parameters are not added, the corresponding default values are also set.

2. Program

Nginx_http_mytest_module.c

#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);static char * ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static ngx_command_t ngx_http_mytest_commands[] = {    {        ngx_string("mytest"),        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,        ngx_http_mytest,        NGX_HTTP_LOC_CONF_OFFSET,        0,        NULL    },    ngx_null_command};static ngx_http_module_t ngx_http_mytest_module_ctx = {    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL};ngx_module_t ngx_http_mytest_module = {    NGX_MODULE_V1,    &ngx_http_mytest_module_ctx,    ngx_http_mytest_commands,    NGX_HTTP_MODULE,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NULL,    NGX_MODULE_V1_PADDING};static char * ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_core_loc_conf_t *clcf;    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);    clcf->handler = ngx_http_mytest_handler;    return NGX_CONF_OK;}static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r){    if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {        return NGX_HTTP_NOT_ALLOWED;    }    ngx_int_t rc = ngx_http_discard_request_body(r);    if (rc != NGX_OK) {        return rc;    }    ngx_str_t type = ngx_string("text/plain");    ngx_str_t response = ngx_string("miao miao");    r->headers_out.status = NGX_HTTP_OK;    r->headers_out.content_length_n = response.len;    r->headers_out.content_type = type;    rc = ngx_http_send_header(r);    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {        return rc;    }    ngx_buf_t *b;    b = ngx_create_temp_buf(r->pool, response.len);    if (b == NULL) {        return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    ngx_memcpy(b->pos, response.data, response.len);    b->last = b->pos + response.len;    b->last_buf = 1;    ngx_chain_t out;    out.buf = b;    out.next = NULL;    return ngx_http_output_filter(r, &out);}

A. ngx_command_t

Define the configuration items corresponding to the server configuration file (installation directory/CONF/nginx. conf), and define the callback function corresponding to the configuration item, which is ngx_http_mytest here.

B. ngx_http_module_t

Module context. Each null corresponds to a function pointer.

 ngx_http_module_t ngx_http_mytest_module_ctx = {        NULL,                                  /* preconfiguration */        NULL,                            /* postconfiguration */        NULL,                                  /* create main configuration */        NULL,                                  /* init main configuration */        NULL,                                  /* create server configuration */        NULL,                                  /* merge server configuration */        NULL,                                  /* create location configuration */        NULL                                   /* merge location configuration */};

C. ngx_module_t

For developing a module, You need to define a ngx_module_t type variable to describe the information of this module. In a sense, this is the most important information of this module, it tells some information about the nginx module, the configuration information defined above, and the module context information, all of which tell the nginx system through this structure, that is, the upper-Layer Code of the loading module, you must obtain this information by defining this structure.

ngx_module_t ngx_http_mytest_module = {  NGX_MODULE_V1,    /*  #define NGX_MODULE_V1    0, 0, 0, 0,NGX_DSO_ABI_COMPATIBILITY, NGX_NUMBER_MAJOR, NGX_NUMBER_MINOR    */  &ngx_http_mytest_module_ctx,  /* module context */  ngx_http_mytest_commands,  /* module directives */  NGX_HTTP_MODULE,   /* module type */  NULL,   /* init master */  NULL,   /* init module */  NULL,   /* init process */  NULL,     /* init thread */  NULL,     /* exit thread */  NULL,     /* exit process */  NULL,     /* exit master */  NGX_MODULE_V1_PADDING /*    #define NGX_MODULE_V1_PADDING  0, 0, 0, 0, 0, 0, 0, 0    */};

3. Compile and install the new module

Nginx provides a simple way to compile third-party modules into nginx. First, put all the source code files in a directory, and write a file in the directory to notify nginx how to compile the module. The file name must be config. Then, when the configure script is executed, add the parameter -- add-module = path (new module source code and config file storage directory) to complete nginx compilation during the policy compilation and installation process. To add a new module to nginx, the whole server must be re-compiled for performance.

Config File FormatThe config file is actually an executable shell script. If you only want to develop an HTTP module, You need to define three variables: (1) ngx_adon_name. Used only when configure is executed. It is generally set to the module name. (2) http_modules. Save all HTTP module names. Each module is connected by a space. When you reset this variable, do not overwrite it directly. Therefore, set "$ http_modules ngx_http_mytest_module" (3) ngx_addon_srcs. Specifies the source code of the new module. Multiple source codes to be compiled can be connected by spaces. Note: You can use the $ ngx_addon_dir variable when setting this variable. It is equivalent to the path parameter -- add-module = path when configure is executed.

Ngx_addon_name = ngx_http_mytest_module
Http_modules = "$ http_modules ngx_http_mytest_module"
Ngx_addon_srcs = "$ ngx_addon_srcs $ ngx_addon_dir/ngx_http_mytest_module.c"

The command for compiling and installing the new module is as follows:

./Configure -- prefix =/usr/local/nginx (specify the root directory after installation and deployment) -- add-module =/home/nginx (new module storage directory) makesudo make install

4. recommended content

Nginx development from entry to proficient http://tengine.taobao.org/book/index.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.