1. Some functions related to file operations
(1) ngx_file_info
Macro definition: # define ngx_file_info (file, Sb) Stat (const car *) file, Sb)
Stat function: specific usage see http://wenku.baidu.com/view/31777dc1d5bbfd0a795673b1.html
(2) ngx_open_file (name, mode, create, access)
Macro definition: # define ngx_open_file (name, mode, create, access) Open (const char *) Name, mode | create, access );
OPEN function: http://baike.baidu.com/view/26337.htm
2. How does nginx read cached files?
Previously, I had a rough idea of upstream's workflow, but when talking about the details, it was still confused.
For example, how does nginx read cached files? Continue to read the source code today, hoping to find the answer.
(1) Open the cache file
In ngx_http_file_cache.c
Ngx_http_file_cache_open (){
... // Open or create a memory Node
If (ngx_open_cached_file (clcf-> open_file_cache, & C-> file. Name, & of, R-> pool )! = Ngx_ OK ){
}
C-> file. FD = of. FD; // save the file handle of the cached File
C-> uniq = of. uniq; // Save the inode node number of the cached File
C-> length = of. size; // Save the size of the cached file (in bytes)
C-> Buf = ngx_create_temp_buf (R-> pool, C-> body_start) // What is stored in this Buf ????
}
This ngx_open_cached_file seems to be related to opening the cache file, so let's take a closer look.
Ngx_open_cached_file (ngx_open_file_cache_t * cache, ngx_str_t * Name, ngx_open_file_info_t * Of, ngx_pool_t * Pool ){
If (Cache = NULL ){
If (of-> test_only) {} // This If statement is not executed
Rc = ngx_open_and_stat_file (name-> data, of, pool-> log );
If (rc = ngx_ OK &&! Of-> is_dir ){
Clnf-> FD = of-> FD; // Save the handle to open the file
Clnf-> name = Name-> data; // save the file path
}
}
}
Check ngx_open_and_stat_file (){
......
FD = ngx_open_file (name,...) // open the cached File
......
Of-> FD = FD; // save the file handle
}
(2) Use cached files to respond to the client
Ngx_http_upstream_cache (){
......
Case ngx_ OK:
Rc = ngx_http_upstream_cache_send (R, U );
}
Ngx_http_upstream_cache_send (){
......
Ngx_http_cache_send ();
}
Ngx_http_cache_send (){
Ngx_buf_t * B;
Ngx_chain_t out;
C = r-> cache;
B = ngx_pcalloc (R-> pool, sizeof (ngx_buf_t ));
If (B = NULL ){
Return ngx_http_internal_server_error;
}
B-> file = ngx_pcalloc (R-> pool, sizeof (ngx_file_t ));
If (B-> file = NULL ){
Return ngx_http_internal_server_error;
}
B-> file_pos = C-> body_start;
B-> file_last = C-> length;
B-> in_file = 1;
B-> last_buf = (r = r-> main )? 1:0;
B-> last_in_chain;
B-> file-> FD = C-> file. FD;
B-> file-> name = C-> file. Name;
B-> file-> log = r-> connection-> log;
Out. Buf = B;
Out. Next = NULL;
Return ngx_http_output_filter (R, & out );
}
(3) Use cached files to respond to customers
A. Structure of the cached file:
- The first part of the cache file stores data about the ngx_http_file_cache_header_t structure.
Typedef struct {
Time_t valid_sec;
Time_t last_modified;
Time_t date;
Uint32_t CRC32;
U_short valid_msec;
U_short header_start; // the starting position of the HTTP header in the cache file
U_short body_start; // start position for storing the HTTP body
- The second part of the cached file is the key, for example, key: http: // host IP Address/relative path/file name. If the client has
If the key is the same, the client uses the same cache.
- The third part is the response header of the source server, which can be stored in the structure.
- The fourth part is the response body of the source server.
B. cache file name: R-> cache-> file. Name. data. You can use this file name to open the cache.
C. Open the file and obtain the file handle.
Ngx_fd_t FD;
FD = ngx_open_file (R-> cache-> file. Name. Data, ngx_file_rdonly | ngx_file_nonblock, ngx_file_open, 0 );
If (FD = ngx_invalid_file ){
// Opening failed
}
D. Get File Information
Ngx_file_info_t fi;
If (ngx_fd_info (FD, & FI) = ngx_file_error ){
// An error occurred while obtaining the information.
Ngx_close_file (FD );
}
E. Obtain the file information and extract valid information.
Int length;
If (ngx_is_dir (& FI )){
Ngx_close_file (FD); // If a folder is opened
} Else {
Length = ngx_file_size (& FI); // number of bytes to open the file
}
F. Apply for space to read cached files
U_char * Buf = ngx_palloc (pool, U-> conf-> buffer_size );
Ssize_t N;
# If (ngx_have_pread)
N = pread (FD, Buf, U-> conf-> buffer_size, 0 );
# Else
N = read (FD, Buf, U-> conf-> buffer_size );
# Endif
If (n =-1 ){
Ngx_close_file (FD); // read error
}
G. Analyze the first part of the cached File
Ngx_http_file_cache_header_t * h;
H = (ngx_http_file_cache_header_t *) BUF;
Size_t body_start = H-> body_start;
Size_t header_start = H-> header_start;
H. Read the cache body
U_char * sbuf = ngx_palloc (pool, length-body_start );
N = pread (FD, cbuf, length-body_start, body_start );
I. Use ngx_http_send_header () and ngx_http_output_filter () to send http data.