nginx 開發第三方模組的時調式日誌的方法(終端printf輸出日誌)

後開啟撒打發了發表於2018-02-06

最近在利用nginx做web後端服務開發,做第三方http模開發,無聊記錄一哈操作nginx的方法(日誌debug,啟動,ngin.conf配置等等)

  1. 開啟除錯日誌: 要開啟除錯日誌,首先需要在配置Nginx時開啟除錯功能,然後編譯:
./configure --with-debug ...

2.修改nginx.conf檔案

daemon off;
master_process off;

daemon off; 作用是關閉nginx在後臺啟動(守護程式的模式)
master_process off; 關閉建立子程式

以上都是設定好了就可以和平時一樣啟動nginx了,在控制檯(終端上)顯示你程式碼中格式printf列印日誌的方法,輸出變數的值除錯程式碼了。

nginx的啟動:

./bin/sbin/nginx

nginx的關閉:
第一種方法:

ps -ef |grep nginx     
kill -9  程式id

第二方法:

./bin/sbin/nginx  -s stop

我的nginx.conf檔案

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
daemon off;
master_process off;

events {
    worker_connections  1024;
}


http {
    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;

    #post server
    server {
        listen       8018;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log  logs/post.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /v1 
        {
            if ($request_method = GET ) 
            {
                    return 403;
            }
            mytest;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
        {
            root   html;
        }
    }

    #get service
    server {
        listen       8017;
        server_name  localhost;

        access_log  logs/get.log  main;
        #access_log off;

        location / 
        {
            root   html;
            index  index.html index.htm;
        }

        location /hello 
        {
            if ($request_method = POST )
            {
                return 403;
            }
            mytest;
        }

       error_page   500 502 503 504  /50x.html;
       location = /50x.html 
       {
            root   html;
       }
    }

}

相關文章