Home

About

Nginx study

26 Nov 2016

Reference

  1. Nginx开发从入门到精通
  2. Lenky
  3. nginx核心讲解
  4. nginx debug

Compile

  1. sudo auto/configure

    This step will configure some variables and create objs/Makefile

    Install PCRE, zlib if you haven’t

  2. sudo make -f objs/Makefile

Add new module

add a new folder containing config and ngx_http_xxx_module.c (for example, hello/config, hello/ngx_http_hello_module.c), then configure with this argument:

sudo auto/configure --add-module=./src/http/modules/hello

sudo auto/configure --add-module=./src/http/modules/hello --add-module=./src/http/modules/post

then

sudo make -f objs/Makefile

change nginx.conf if necessary, and reload it:

sudo objs/nginx -s reload

Debug

sudo gdb
(gdb) attach 16288 
master_process off;

this will leave only one process for nginx

daemon off;

daemon off means it is no longer a daemon process. Use this setting when debug so we can use

sudo gdb objs/nginx

and comment out this setting when run nginx.

Misc

ngx_cycle_modules() initialize cycle->modules from ngx_modules

ngx_preinit_modules() initialize ngx_modules from ngx_module_names

handle request

ngx_http_read_request_header(), then ngx_http_parse_request_line()

ngx_http_process_request_headers(), ngx_http_process_request_line()

upload module

ngx_http_core_main_conf_t

ngx_http.c, ngx_http_init_phase_handlers()

Questions

  1. How is the settings in config file related with the code?

  2. Will all the modules be linked in runtime?

No, in the function ngx_load_module(), it uses the system function dlsym() to load modules from binary. So only the modules linked during compiling will be involved.

How to work with PHP

Refer this link

sudo apt-get install php5-fpm

add the following lines in the “server” block:

index index.php index.html index.htm;

location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;                
        }

Notice: If you build nginx from source code, you need to copy fastcgi.conf and fastcgi_params into the conf/ folder manually.

sudo vim /usr/local/nginx/html/info.php

its contents:

<?php
phpinfo();
?>

and check it out.