Nginx source code structure and module initialization

Source: Internet
Author: User

Nginx source code structure and module initialization
Preface:

The previous article (http://www.bkjia.com/ OS /201511/449771.html) has introduced the basic functions of Nginx, also introduced the installation and simple implementation of load balancing in Windows, below the main learning Nginx source code structure.

Nginx source code src directory structure (not compiled and installed)

Environment: nginx-1.8.0 + CentOS7.0
(You can use the yum install tree Command to install the tree Command to display the file tree structure)

[Root @ iZ94sni08orZ nginx-1.8.0] # tree src/── core │ ├ ── nginx. c │ ── nginx. h │ ── │ ├ ── ngx_array.h │ ── ngx_buf.c │ ── ngx_buf.h │ ── ngx_config.h │ ── ngx_connection.c │ ── ngx_connection.h │ ── ─ ngx_core.h │ ── (omitted) │ ── ngx_times.c │ ── ngx_times.h ├ ── event │ ── les │ ── ngx_aio_module.c │ ── │ ngx_devpoll_module.c ── ngx_epoll_module.c │ ── ngx_eventport_module.c │ ── ngx_kqueue_module.c │ ── │ ├ ── ngx_select_module.c │ ── ngx_win32_select_module.c │ ├ ── ngx_event_accept.c │ ── ngx_event.c │ ── ngx_event_event_connect.c │ ── (omitted) │ ── ngx_event_timer.h ── http │ ── modules │ ── ngx_http_access_module.c │ ── ngx_http_addition_filter_module.c │ ── ngx_http_auth_request_module.c │ ── ngx_http_autoindex_module.c │ ── ngx_http_browser_module.c │ ── ngx_http_charset_filter_module.c │ ── ngx_http_chunked_filter_module.c │ ── (omitted) │ ── ngx_http_uwsgi_module.c │ ── ngx_http_xslt_filter_module.c │ ── perl │ ── Makefile. PL │ ── nginx. pm │ ── nginx. xs │ ├ ── │ ng── ngx_http_perl_module.h │ type── typemap │ ── ngx_http.c │ ── ngx_http_cache.h │ ── ngx_http_config.h │ ── ngx_http_header_filter_module.c │ ├ (omitted) │ ── ngx_http_variables.h │ ── ngx_http_write_filter_module.c ── mail │ ── ngx_mail_auth_http_module.c │ ── ngx_mail.c │ ├ (omitted) │ ── ngx_mail_ssl_module.h misc │ ├ ── ngx_cpp_test_module.cpp │ ── ngx_google_perftools_module.c OS ── unix ── ngx_aio_read.c ── ngx_aio_read_chain.c ├ ── ngx_aio_write.c ── (omitted) ── ngx_udp_recv.c ├ ── ngx_user.c ── ngx_user.h ── ngx_writev_chain.c10 directories, 265 files [root @ iZ94sni08orZ nginx-1.8.0] #

From the source code above, we can see that there are 10 directories and 265 files in total. The main modules of Nginx are Core, event, http, mail, and misc (Miscellaneous, including multiple functions) and OS, and according to the source code naming, You can roughly guess the functions it represents.
We suggest you download the source code and take a look at it. In this case, you can better understand the composition of Nginx functions.

For example, part of the code of nginx. c In the first file of the Core module is as follows:

static ngx_command_t ngx_core_commands[] = { { ngx_string("daemon"), NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, 0, offsetof(ngx_core_conf_t, daemon), NULL }, { ngx_string("master_process"), NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, 0, offsetof(ngx_core_conf_t, master), NULL }, 。。。

From the above, we can see that the ngx_core_commands [] array defines all the setting commands used in the Core module (this is also required to be introduced later when learning the Core module ).

In addition, the event-modules section clearly lists several event models, which are also the places to learn when learning this module.
Due to the limited understanding of shell scripting language and C, the source code is not explained too much.

Compile the source code

If CentOS is used, you need to download some basic software first. You can use the command to download it:
1. To support rewrite, We need to install pcre

# Yum install pcre * // if you have installed it, skip this step.

2. Install openssl
Ssl support is required. skip this step if ssl support is not required.

# yum install openssl*

3. Install the gzip class library

yum install zlib zlib-devel

(Note: For Ubuntu, run the sudo apt-get install nginx command to download it)

4, prepare the source code, extract tar-zxvf nginx-1.8.0.tar.gz
5. Compile and install the SDK by running the following command:

# cd nginx-1.8.0# ./configure --prefix=/usr/local/nginx-1.7.0 \--with-http_ssl_module --with-http_spdy_module \--with-http_stub_status_module --with-pcre

-With-http_stub_status_module: Support for nginx status query
-With-http_ssl_module: Support for https
-With-http_spdy_module: support google spdy, would like to know Baidu spdy, this must have ssl support
-With-pcre: To support rewrite, you must specify a pcre

(If you are prompted to install other packages, install them)

After setting, execute make install

The startup command is in the/usr/local/nginx-1.8.0/sbin File

Start:./nginx close:./nginx-s stop restart:./nginx-s reload

(If it is in Ubuntu, it may be in the/usr/sbin directory)

Analyze the compiled files:

/Usr/local/nginx-1.8.0 Directory: This is the configuration file generated after compilation

[root@iZ94sni08orZ nginx-1.8.0]# tree.├── client_body_temp├── conf│ ├── fastcgi.conf│ ├── fastcgi.conf.default│ ├── fastcgi_params│ ├── fastcgi_params.default│ ├── koi-utf│ ├── koi-win│ ├── mime.types│ ├── mime.types.default│ ├── nginx.conf│ ├── nginx.conf.default│ ├── scgi_params│ ├── scgi_params.default│ ├── uwsgi_params│ ├── uwsgi_params.default│ └── win-utf├── fastcgi_temp├── html│ ├── 50x.html│ └── index.html├── logs│ ├── access.log│ └── error.log├── proxy_temp├── sbin│ └── nginx├── scgi_temp└── uwsgi_temp9 directories, 20 files[root@iZ94sni08orZ nginx-1.8.0]# 

The conf directory contains several configuration files used to control the basic functions of the Nginx server. The nginx. conf file is:

# User nobody; # Running worker_processes 1; # Number of allowed work threads # error_log logs/error. log; # error_log logs/error. log notice; # error_log logs/error. log info; # pid logs/nginx. pid; events {worker_connections 1024; # the maximum number of connections allowed by each work sub-process is 1024} http {include mime. types; # used to load the configuration file default_type application/octet-stream; # log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"' # '$ status $ body_bytes_sent "$ ht Tp_referer "'#'" $ http_user_agent "" $ http_x_forwarded_for "'; # access_log logs/access. log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # gzip on; server {listen 80; server_name localhost; # charset koi8-r; # access_log logs/host. access. log main; location/{root html; index index.html index.htm;} # error_page 404/404 .html; # redirect server error pages to the stati C page/50x.html # error_page 500 502 503 x.html; location =/50x.html {root html;} # proxy the PHP scripts to Apache listening on 127.0.0.1: 80 # location ~ \. Php $ {# proxy_pass http: // 127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1: 9000 # location ~ \. Php $ {# root html; # fastcgi_pass 127.0.0.1: 9000; # fastcgi_index index. php; # fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; # include fastcgi_params; #}# deny access. htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\. Ht {# deny all ;#}# another virtual host using mix of IP-, name-, and port-based configuration # server {# listen 8000; # listen somename: 8080; # server_name somename alias another. alias; # location/{# root html; # index index.html index.htm; #}#}# HTTPS server # server {# listen 443 ssl; # server_name localhost; # ssl_certificate cert. pem; # ssl_certificate_key cert. key; # ssl_ses Sion_cache shared: SSL: 1 m; # ssl_session_timeout 5 m; # ssl_ciphers HIGH :! ANULL :! MD5; # ssl_prefer_server_ciphers on; # location/{# root html; # index index.html index.htm ;#}#}}

From the content above, we can see that the meaning of each Configuration Attribute can be roughly seen, which will be detailed in the subsequent module learning.

After compilation, an objs directory will be generated in the original Nginx code package. In the generated ngx_modules.c file, the statement is re-centralized (the extern keyword is used) all modules configured by nginx can be configured through the configure command before compilation, that is, to set which modules need to be compiled and which are not compiled.
As follows. Contains the content during the compilation process:

# Include  # Include  Extern ngx_module_t ngx_core_module; extern ngx_module_t ngx_errlog_module; extern ngx_module_t ngx_conf_module; extern ngx_module_t ngx_events_module ;... (Omitted) extern functions failed; optional * ngx_modules [] = {& ngx_core_module, & Environment, & ngx_conf_module, & ngx_events_module, & Environment, & ngx_epoll_module, & ngx_openssl_module, & ngx_regex_module, & ngx_http_module, & ngx_http_core_module ,... (Omitted) & ngx_http_range_body_filter_module, & ngx_http_not_modified_filter_module, NULL };  

These modules are declared with extern here to indicate that other modules can be accessed, and the definition and initialization of ngx_module_t structure are carried out in the corresponding. c file. For example, the ngx_core_module module defines and performs static initialization in the./src/core/nginx. c file. In fact, ngx_core_module is a global struct object, and other modules are similar. As follows.

ngx_module_t ngx_core_module = { NGX_MODULE_V1, &ngx_core_module_ctx, /* module context */ ngx_core_commands, /* module directives */ NGX_CORE_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 }; 

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.