├── README.md ├── chapter3 ├── helloworld │ ├── README.md │ ├── config │ └── ngx_http_mytest_module.c └── sendfile │ ├── README.md │ ├── config │ └── ngx_http_mytest_module.c ├── chapter4 ├── README.md ├── config ├── nginx.conf └── ngx_http_mytest_module.c ├── chapter5 ├── subrequest │ ├── config │ ├── nginx.conf │ └── ngx_http_mytest_module.c └── upstream │ ├── README.md │ ├── config │ ├── nginx.conf │ └── ngx_http_mytest_module.c └── chapter6 ├── config ├── nginx.conf └── ngx_http_myfilter_module.c /README.md: -------------------------------------------------------------------------------- 1 | # diveintoNginx 2 | 《深入理解Nginx:模块开发与架构解析》示例代码 3 | 4 | -------------------------------------------------------------------------------- /chapter3/helloworld/README.md: -------------------------------------------------------------------------------- 1 | # 编译 2 | ./configure --add-module=/你的路径/chapter3/helloworld/ 3 | 4 | # nginx.conf 5 | ``` 6 | server { 7 | listen 8080; 8 | 9 | location / { 10 | mytest; 11 | } 12 | } 13 | ``` 14 | 15 | # 测试 16 | * 请求 17 | 18 | `curl 127.0.0.1:8080/` 19 | * 返回 20 | 21 | `HelloWorld` 22 | -------------------------------------------------------------------------------- /chapter3/helloworld/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_mytest_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c" 4 | -------------------------------------------------------------------------------- /chapter3/helloworld/ngx_http_mytest_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter3/helloworld/ngx_http_mytest_module.c -------------------------------------------------------------------------------- /chapter3/sendfile/README.md: -------------------------------------------------------------------------------- 1 | # 编译 2 | 3 | `./configure --add-module=/你的路径/chapter3/sendfile/` 4 | # nginx.conf 5 | ``` 6 | server { 7 | listen 8080; 8 | 9 | location / { 10 | mytest; 11 | } 12 | } 13 | ``` 14 | # 测试 15 | 16 | * 请求 17 | `curl 127.0.0.1:8080/` 18 | 19 | * 返回 20 | 21 | /tmp/test.txt文件内容。*这个文件必须存在* 22 | -------------------------------------------------------------------------------- /chapter3/sendfile/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_mytest_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c" 4 | -------------------------------------------------------------------------------- /chapter3/sendfile/ngx_http_mytest_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter3/sendfile/ngx_http_mytest_module.c -------------------------------------------------------------------------------- /chapter4/README.md: -------------------------------------------------------------------------------- 1 | # 编译 2 | ./configure --add-module=/你的路径/chapter4/ 3 | 4 | # nginx.conf 5 | 6 | ``` 7 | server { 8 | test_str svrstr; 9 | location /test1 { 10 | test_str_array array1; 11 | test_num 100; 12 | test_size 2m; 13 | test_off 1g; 14 | test_msec 1h; 15 | test_sec 1h; 16 | test_bufs 2 8k; 17 | test_enum banana; 18 | test_bitmask better; 19 | test_access user:rw group:rw all:r; 20 | test_path html/www; 21 | test_myconfig str 1; 22 | } 23 | 24 | location /test2 { 25 | test_flag on; 26 | test_str locstr; 27 | test_str_array array2; 28 | test_keyval key value; 29 | } 30 | } 31 | ``` 32 | 33 | # 测试 34 | 启动Nginx 35 | -------------------------------------------------------------------------------- /chapter4/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_mytest_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c" 4 | -------------------------------------------------------------------------------- /chapter4/nginx.conf: -------------------------------------------------------------------------------- 1 | 2 | #user nobody; 3 | worker_processes 1; 4 | 5 | error_log logs/error.log debug; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | 12 | http { 13 | include mime.types; 14 | default_type application/octet-stream; 15 | 16 | #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 17 | # '$status $body_bytes_sent "$http_referer" ' 18 | # '"$http_user_agent" "$http_x_forwarded_for"'; 19 | 20 | #access_log logs/access.log main; 21 | 22 | keepalive_timeout 65; 23 | test_str SUPERMAN; 24 | 25 | server { 26 | server_name HOST1; 27 | 28 | location /HOST1_loc1 { 29 | test_str loc1_BANANA; 30 | } 31 | 32 | location /HOST1_loc2 { 33 | } 34 | } 35 | 36 | server { 37 | server_name HOST2; 38 | listen 8088; 39 | server_name localhost; 40 | test_str CAR; 41 | 42 | location /index { 43 | root html; 44 | test_str FOCUS; 45 | index index.html index.htm; 46 | } 47 | 48 | location /test { 49 | test_flag on; 50 | test_str PLANE; 51 | test_num 4; 52 | } 53 | 54 | error_page 500 502 503 504 /50x.html; 55 | location = /50x.html { 56 | root html; 57 | } 58 | 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /chapter4/ngx_http_mytest_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter4/ngx_http_mytest_module.c -------------------------------------------------------------------------------- /chapter5/subrequest/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_mytest_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c" 4 | -------------------------------------------------------------------------------- /chapter5/subrequest/nginx.conf: -------------------------------------------------------------------------------- 1 | 2 | #user nobody; 3 | worker_processes 1; 4 | 5 | error_log logs/error.log debug; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | 12 | http { 13 | include mime.types; 14 | default_type application/octet-stream; 15 | 16 | #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 17 | # '$status $body_bytes_sent "$http_referer" ' 18 | # '"$http_user_agent" "$http_x_forwarded_for"'; 19 | 20 | #access_log logs/access.log main; 21 | 22 | keepalive_timeout 65; 23 | 24 | server { 25 | listen 8080; 26 | 27 | location /list { 28 | proxy_pass http://hq.sinajs.cn; 29 | proxy_set_header Accept-Encoding ""; 30 | } 31 | 32 | location /query { 33 | mytest; 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /chapter5/subrequest/ngx_http_mytest_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter5/subrequest/ngx_http_mytest_module.c -------------------------------------------------------------------------------- /chapter5/upstream/README.md: -------------------------------------------------------------------------------- 1 | # 编译 2 | ./configure --add-module=/你的路径/chapter5/upstream/ 3 | 4 | # nginx.conf 5 | ``` 6 | server { 7 | listen 8080; 8 | 9 | location / { 10 | mytest; 11 | } 12 | } 13 | ``` 14 | 15 | # 测试 16 | * 请求 17 | 18 | `curl 127.0.0.1:8080/` 19 | * 返回 20 | 21 | `HelloWorld` 22 | -------------------------------------------------------------------------------- /chapter5/upstream/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_mytest_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c" 4 | -------------------------------------------------------------------------------- /chapter5/upstream/nginx.conf: -------------------------------------------------------------------------------- 1 | 2 | #user nobody; 3 | worker_processes 1; 4 | 5 | error_log logs/error.log debug; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | 12 | http { 13 | include mime.types; 14 | default_type application/octet-stream; 15 | 16 | #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 17 | # '$status $body_bytes_sent "$http_referer" ' 18 | # '"$http_user_agent" "$http_x_forwarded_for"'; 19 | 20 | #access_log logs/access.log main; 21 | 22 | keepalive_timeout 65; 23 | 24 | server { 25 | listen 8080; 26 | location /test { 27 | mytest; 28 | } 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /chapter5/upstream/ngx_http_mytest_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter5/upstream/ngx_http_mytest_module.c -------------------------------------------------------------------------------- /chapter6/config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_myfilter_module 2 | 3 | HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_myfilter_module" 4 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_myfilter_module.c" 5 | 6 | -------------------------------------------------------------------------------- /chapter6/nginx.conf: -------------------------------------------------------------------------------- 1 | 2 | #user nobody; 3 | worker_processes 1; 4 | 5 | error_log logs/error.log debug; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | 12 | http { 13 | include mime.types; 14 | default_type application/octet-stream; 15 | 16 | #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 17 | # '$status $body_bytes_sent "$http_referer" ' 18 | # '"$http_user_agent" "$http_x_forwarded_for"'; 19 | 20 | #access_log logs/access.log main; 21 | 22 | keepalive_timeout 65; 23 | 24 | server { 25 | listen 8080; 26 | 27 | location / { 28 | root /; 29 | add_prefix on; 30 | } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /chapter6/ngx_http_myfilter_module.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/russelltao/diveintonginx/a2fb9c64dfd020b4cfa118c855c04fa66f23cfa8/chapter6/ngx_http_myfilter_module.c --------------------------------------------------------------------------------