Because of my Private Cloud FaaS (function calculation) project Navagraha use nginx as a realization of Service Mesh API Gateway and the basic framework, it requires a deep understanding of Nginx, so here Nginx official translation of documents, developed to provide assistance after the reference .
Introduction
Code organizational structure
- auto - build script compiler
- src
- core - and base-type functions - including string, array, log, pool etc.
- event - the core of the event type
- modules - event notification module, including epoll, kqueue, select etc.
- http - HTTP modules and core generic code
- modules - other HTTP module
- v2 - HTTP/2
- mail - spam module
- os - specific platform code
- unix
- win32
- stream - a data flow module
head File
Nginx in each source file, the file must contain two heads were
#include <ngx_config.h>
#include <ngx_core.h>
If you need to implement an HTTP module, you need to add the relevant HTTP headers:
#include <ngx_http.h>
Mail module add the header file:
#include <ngx_mail.h>
Stream header module needs to be added:
#include <ngx_stream.h>
Integer type
In the normal development process nginx module, generally two integral type ngx_int_t
or ngx_uint_t
the corresponding type of pointer intptr_t
and uintptr_t
.
Generic return type
In nginx module, many functions using a common return type:
- NGX_OK - representatives operation executed successfully.
- NGX_ERROR - representatives operation failed.
- NGX_AGAIN - represents the operation is not finished; asked to re-call the function.
- NGX_DECLINED - Representative operation was refused.
- NGX_BUSY - represents the resources are not available
- NGX_DONE - represents the completion of the operation or execution notice the caller continues to perform other operations.
- NGX_ABORT - represents the operation to be performed is interrupted.
Error Handling
Call the macro definition ngx_errno
will return the last system error code. If the platform is POSIX, then this macro definition will return errno
, if it is to return Windows platform GetLastError()
. ngx_socket_errno
Socket error was returned error code last seen. If you want to set errno error code, you can use ngx_set_errno(errno)
or ngx_set_socket_errno(errno)
.
Examples of use ngx_errno follows:
void
ngx_my_kill(ngx_pid_t pid, ngx_log_t *log, int signo)
{
ngx_err_t err;
if (kill(pid, signo) == -1) {
err = ngx_errno;
ngx_log_error(NGX_LOG_ALERT, log, err, "kill(%P, %d) failed", pid, signo);
if (err == NGX_ESRCH) {
return 2;
}
return 1;
}
return 0;
}