打印nginx变量来调试相关信息

2020/04/24 posted in  服务端
Tags:  #nginx #linux

实际有时候nginx服务器有很多变量我们都不清楚conf的时候配置得怎么样,实际可以安装nginx的module:echo模块来打印一下

通常我们的配置,登录fastcgi


fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

我们想打印一下定义的变量,最快捷的方法,使用nginx输出一下即可

如下

server {
    server_name 127.0.0.1;
    location /http{
        expires -1;
        
        echo [request_filename:\t$request_filename];
        echo [request_method:\t$request_method];
        echo [request_body_file:\t$request_body_file];
        
        echo [http_host:\t$http_host];
        echo [remote_addr:\t$remote_addr];
        echo [time_local:\t$time_local];
        echo [request:\t$request];
        echo [xrequest:\t$xrequest];
        echo [http_referer:\t$http_referer];
        echo [http_user_agent:\t$http_user_agent];
        echo [status:\t$status];
        echo [request_time:\t$request_time];
        echo [http_x_forwarded_for:\t$http_x_forwarded_for];
        echo [request_body:\t$request_body];
        
        #return 200 "abc";
        
    }
}

其它

顺便提一下nginx的监近模块,如果你的服务使用的是nginx,不访用来定时监控一下

ngx_http_stub_status_module

stub_status

ngx_google_perftools_module

gperftools定位性能问题