標籤:tac remote 源碼 架構 load sed scripts img rem
/usr/local/nginx/conf/nginx.conf檔案例如以下:
#worker背景工作處理序的使用者及使用者組user weijl;#Nginx worker進程個數worker_processes 1;#error日誌的設定,預設logs/error.log error#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid檔案的路徑#pid logs/nginx.pid;events { worker_connections 1024;}http { #嵌入設定檔mime.types 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 65; #gzip on; upstream test.proxy.com { #ip_hash; server 192.168.0.7; server 192.168.0.8; } server { listen 127.0.0.1:80; server_name test.proxy.com; #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 500 502 503 504 /50x.html; location = /50x.html { root html; }#靜態圖片資源location /image/ { root /home/weijl/workspace/; autoindex on; } #反向 Proxy location /proxy_loc/ { root html; proxy_set_header Host $host; proxy_pass http://test.proxy.com; #禁用緩衝 proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; }#實現自己的HTTP模組location /test { mytest; 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; # } #}}
匹配uri,在/usr/local/nginx/html/檔案夾下建立檔案夾test:
[email protected]:/usr/local/nginx/html$ sudo mkdir test
[email protected]:/usr/local/nginx/html$ sudo chmod -R 777 test/
實現自己的HTTP模組C代碼/home/weijl/workspace/nginx-1.10.3/src/http/ngx_http_mytest_module.c例如以下:
#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>//請求包體接收完後回調的函數void ngx_http_mytest_body_handler(ngx_http_request_t *r){}//HTTP的HTTP_CONTENT_PHASE階段mytest模組介入處理http請求內容static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r){//必須時GET或者HEAD方法,否則返回405 Not Allowedif(!(r->method &(NGX_HTTP_GET | NGX_HTTP_HEAD))){ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl NGX_HTTP_NOT_ALLOWED");return NGX_HTTP_NOT_ALLOWED;}//丟棄請求中的包體ngx_int_t rc = ngx_http_discard_request_body(r);if(rc != NGX_OK){ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl rc=%d", rc);return rc;}/*設定返回的Content_Type。注意,ngx_str_t有一個非常方便的初始化宏ngx_string,它能夠把ngx_str_t的data和len成員都設定好*/ngx_str_t type = ngx_string("text/plain");//返回的包體內容ngx_str_t response = ngx_string("Hello world! Here is the first Nginx HTTP program!");ngx_str_t ress = ngx_string("<html>\r\n<head>\r\n<title>Welcome to nginx!</title>\r\n</head>\r\n<body bgcolor=\"white\" text=\"black\">\r\n<center><h1>Welcome to 192.168.0.7</h1></center>\r\n</body>\r\n</html>\r\n");//設定返回狀態代碼r->headers_out.status = NGX_HTTP_OK;//響應包是由包體內容的,須要設定Conten-Length長度r->headers_out.content_length_n = response.len + ress.len;//設定Content-Typer->headers_out.content_type = type;//發送HTTP頭部rc = ngx_http_send_header(r);if(rc == NGX_ERROR || rc > NGX_OK || r->header_only){ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl rc=%d", rc);return rc;}//構造ngx_buf_t結構體準備發送包體ngx_buf_t *b, *bs;b = ngx_create_temp_buf(r->pool, response.len);if(NULL == b){ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl b=NULL");return NGX_HTTP_INTERNAL_SERVER_ERROR;}//將Hello World拷貝到ngx_buf_t指向的記憶體中ngx_memcpy(b->pos, response.data, response.len);//注意,一定要設定好last指標b->last = b->pos + response.len;//聲明這是最後一塊緩衝區b->last_buf = 0;bs = ngx_create_temp_buf(r->pool, ress.len);if(NULL == bs){ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl bs=NULL");return NGX_HTTP_INTERNAL_SERVER_ERROR;}//將ress拷貝到ngx_buf_t指向的記憶體中ngx_memcpy(bs->pos, ress.data, ress.len);//注意,一定要設定好last指標bs->last = bs->pos + ress.len;//聲明這是最後一塊緩衝區bs->last_buf = 1;//構造發送時的ngx_chain_t結構體ngx_chain_t out, outs;out.buf = b;//設定next為NULLout.next = &outs;outs.buf = bs;outs.next = NULL;ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl 最後一步為發送包體。發送結束後HTTP架構會調用ngx_http_finalize_request方法結束請求\n");//最後一步為發送包體。發送結束後HTTP架構會調用ngx_http_finalize_request方法結束請求return ngx_http_output_filter(r, &out);}//沒有什麼工作必須在HTTP架構初始化時完畢,不必實現ngx_http_module_t的8個回調方法static ngx_http_module_t ngx_http_mytest_module_ctx ={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};//“mytest”配置項解析的回調方法static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ngx_http_core_loc_conf_t *clcf;clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);clcf->handler = ngx_http_mytest_handler;return NGX_CONF_OK;}//mytest配置項的處理static ngx_command_t ngx_http_mytest_commands[] = {{ngx_string("mytest"),NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,ngx_http_mytest,//在出現配置項mytest時調用ngx_http_mytest解析NGX_HTTP_LOC_CONF_OFFSET,0,NULL},//很多其它的配置項能夠在這裡定義ngx_null_command};//定義mytest模組ngx_module_t ngx_http_mytest_module = { NGX_MODULE_V1, &ngx_http_mytest_module_ctx, /* module context */ ngx_http_mytest_commands, /* module directives */ NGX_HTTP_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};
將自己的HTTP模組代碼編譯進Nginx
在檔案夾/home/weijl/workspace/nginx-1.10.3/src/http/下建立檔案config:
[email protected]:~/workspace/nginx-1.10.3/src/http$ sudo touch config
config檔案內容例如以下:
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
開始nginx的編譯與又一次安裝
進入Nginx源碼檔案夾/home/weijl/workspace/nginx-1.10.3下,依次運行例如以下命令:
[email protected]:~/workspace/nginx-1.10.3$ sudo ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-pcre=/home/weijl/download/pcre-8.39 --with-openssl=/home/weijl/download/openssl-1.1.0e --with-http_ssl_module --with-zlib=/home/weijl/download/zlib-1.2.11 --add-module=/home/weijl/workspace/nginx-1.10.3/src/http
[email protected]:~/workspace/nginx-1.10.3$ sudo make
[email protected]:~/workspace/nginx-1.10.3$ sudo make install
開始測實驗證
在瀏覽器中輸入localhost:80/test,結果
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNsd2ps/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
基於Nginx實現一個自己的HTTP模組