Objective:
The previous article (http://blog.csdn.net/xlgen157387/article/details/49781487) has introduced the basic features of Nginx, but also introduces the installation under Windows and the simple implementation of load balancing, Below the main study of Nginx source structure.
Nginx Source SRC directory structure (not compiled installation)
Environment: nginx-1.8.0+centos7.0
(can be used yum install tree to install the tree command, you can display the structure of the file)
[Email protected] nginx-1.8. 0]# tree src/Src/├──core│├──nginx. C│├──nginx. h│├──ngx_array. C│├──ngx_array. h│├──ngx_buf. C│├──ngx_buf. h│├──ngx_conf_file. C│├──ngx_conf_file. h│├──ngx_config. h│├──ngx_connection. C│├──ngx_connection. h│├──ngx_core. h│├── (omitted part) │├──ngx_times. C│└──ngx_times. h├──event│├──modules││├──ngx_aio_module. C││├──ngx_devpoll_module. C││├──ngx_epoll_module. C││├──ngx_eventport_module. C││├──ngx_kqueue_module. C││├──ngx_poll_module. C││├──ngx_rtsig_module. C││├──ngx_select_module. C││└──ngx_win32_select_module. C│├──ngx_event_accept. C│├──ngx_event. C│├──ngx_event_connect. C│├──ngx_event_connect. h│├── (omitted part) │└──ngx_event_timer. h├──http│├──modules││├──ngx_http_access_module. C││├──ngx_http_addition_filter_module. C││├──ngx_http_auth_basic_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 part) ││├──ngx_http_uwsgi_module. C││├──ngx_http_xslt_filter_module. C││└──perl││├──makefile. PL││├──nginx. PM││├──nginx. XS││├──ngx_http_perl_module. C││├──ngx_http_perl_module. h││└──typemap│├──ngx_http. C│├──ngx_http_cache. h│├──ngx_http_config. h│├──ngx_http_header_filter_module. C│├── (omitted part) │├──ngx_http_variables. h│└──ngx_http_write_filter_module. C├──mail│├──ngx_mail_auth_http_module. C│├──ngx_mail. C│├── (omitted part) │├──ngx_mail_ssl_module. C│└──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 part) ├──ngx_udp_recv. C├──ngx_user. C├──ngx_user. h└──ngx_writev_chain. CTenDirectories,265Files[[email protected] nginx-1.8. 0]#
From the above source can be seen in a total of ten directories, 265 Files,nginx main module is core, event, HTTP, Mail, misc (Miscellaneous, including a variety of functions), the parts of the OS, And according to the name of the source code can also roughly guess the function that it represents.
Suggest that you download its source code, a general look, so that, can better understand the function of Nginx.
As a simple example, the first file in the core module, NGINX.C, is a part of the code below:
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 }, 。。。
As can be seen from the above, ngx_core_commands[] This array defines all the setup commands used under the core module (this is what you need to know when learning the core module from behind).
And there are event–modules below a clear list of several events model, but also back in learning the module when the need to learn the place.
Because the Shell scripting language and C mastery of a limited degree, do not do too much explanation of the source code.
Compiling the source code
If you use CentOS, you need to download some basic software, you can use the command to download:
1, in order to support rewrite function, we need to install Pcre
# yum install pcre//如过你已经装了,请跳过这一步
2. Installing OpenSSL
Requires SSL support, skip this step if SSL support is not required
# yum install openssl*
3.gzip Class Library Installation
install zlib zlib-devel
(Note: If it is Ubuntu, you can download it directly using the command sudo apt-get install nginx )
4. Prepare the source code for decompression tar -zxvf nginx-1.8.0.tar.gz
5, compile and install, execute 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 nginx status Query
–with-http_ssl_module: Support HTTPS
–with-http_spdy_module: Support Google's spdy, want to know please Baidu Spdy, this must have SSL support
–with-pcre: In order to support the rewrite rewrite function, it is necessary to develop pcre
(If you are prompted to install additional packages, install them)
After it is set, execution make finishes after executionmake install
Start command under/usr/local/nginx-1.8.0/sbin file
-s-s reload
(If in Ubuntu, it may be in the/usr/sbin directory)
Parse the compiled file:
/usr/local/nginx-1.8.0 directory: This is the configuration generated after the compilation and other files
[Email protected] 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│├── -x. html│└──index. html├──logs│├──access. Log│└──error. Log├──proxy_temp├──sbin│└──nginx├──scgi_temp└──uwsgi_temp9Directories, -Files[[email protected] nginx-1.8. 0]#
There are several configuration files under the Conf directory, which is used to control the basic functions of the Nginx server, where nginx.conf is:
#user nobody; #运行的用户Worker_processes1;#允许work线程的数目#error_log logs/error. log;#error_log logs/error. log notice;#error_log logs/error. log info;#pid logs/nginx.pid;Events {Worker_connections1024x768;#每个work子进程允许最大的连接数为1024}http {include mime.types;#用于载入配置文件Default_type Application/octet-stream;#log_format main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sent ' $http _referer ' # ' "$http _user_agent" "$http _x_forwarded_for" '; #access_log Logs/access.log main;Sendfile on;#tcp_nopush on; #keepalive_timeout 0;Keepalive_timeout $;#gzip on;server {Listen the; 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 static page/50x.html #Error_page - 502 503 504/ -x.html; Location =/ -x.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 to. 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_session_cache shared:ssl:1m; # ssl_session_timeout 5m; # ssl_ciphers high:!anull:! MD5; # ssl_prefer_server_ciphers on; # location/{ # root HTML; # index index.html index.htm; # } #}}
As can be seen from the above content, the meaning of each configuration attribute can be seen, this will be in the back of the sub-module learning detailed description.
Similarly, after compiling, a OBJS directory is generated in the original Nginx code package, where the generated NGX_MODULES.C file is re-centralized (using the extern keyword) for all the modules of nginx configuration. These modules can be configured by the pre-compilation Configure command, which sets which modules need to be compiled and which are not compiled.
As follows. Contains the contents of the execution of the compilation process:
#include <ngx_config.h> Span class= "hljs-comment" > #include <ngx_core.h> 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 part) extern ngx_module_t Ngx_http_upstream_hash_module;extern ngx_module_t ngx_http_upstream_ip_hash_module;ngx_ module_t *ngx_modules[] = {&ngx_core_module, &ngx_errlog_module, &ngx_conf_module, &ngx_event S_module, &ngx_event_core_module, &ngx_epoll_module, &ngx_openssl_module, &ngx_regex_module, &ngx_http_module, &ngx_http_core_module, (omitted part) &ngx_http_range_body_filter_module, &ngx_http_not_modified_filter_module, null };
These modules are declared here with extern to indicate that the other modules are accessible, and that the definition and initialization of the ngx_module_t structure is in its corresponding. c file. For example, the Ngx_core_module module is defined in the./src/core/nginx.c file and is statically initialized. In fact, Ngx_core_module is a global structure 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};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nginx source code structure and module initialization