1. 程式人生 > >nginx 日誌配置

nginx 日誌配置



nginx日誌配置示例:
log_format  main  '$server_name $remote_addr - $remote_user [$time_local] "$request" '
                        '$status $uptream_status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for" '
                        '$ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';
上面紅色部分為Nginx預設指定的格式樣式,每個樣式的含義如下:
$server_name:虛擬主機名稱。
$remote_addr:遠端客戶端的IP地址。$remote_user:遠端客戶端使用者名稱稱,用於記錄瀏覽者進行身份驗證時提供的名字,如登入百度的使用者名稱scq2099yt,如果沒有登入就是空白。
$time_local:訪問的時間與時區,比如18/Jul/2012:17:00:01 +0800,時間資訊最後的"+0800"表示伺服器所處時區位於UTC之後的8小時。
$request:請求的URI和HTTP協議,這是整個PV日誌記錄中最有用的資訊,記錄伺服器收到一個什麼樣的請求
$status:記錄請求返回的http狀態碼,比如成功是200。
$uptream_status:upstream狀態,比如成功是200.
$request_body:獲取post提交引數
$body_bytes_sent:傳送給客戶端的檔案主體內容的大小,比如899,可以將日誌每條記錄中的這個值累加起來以粗略估計伺服器吞吐量。
$http_referer:記錄從哪個頁面連結訪問過來的。 
$http_user_agent:客戶端瀏覽器資訊
$http_x_forwarded_for:客戶端的真實ip,通常web伺服器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理伺服器的iP地址。反向代理伺服器在轉發請求的http頭資訊中,可以增加x_forwarded_for資訊,用以記錄原有客戶端的IP地址和原來客戶端的請求的伺服器地址。
$ssl_protocol:SSL協議版本,比如TLSv1。
$ssl_cipher:交換資料中的演算法,比如RC4-SHA。 
$upstream_addr:upstream的地址,即真正提供服務的主機地址。 
$request_time:整個請求的總時間。 

$upstream_response_time:請求過程中,upstream的響應時間。




http {
    include       mime.types;
    default_type  application/octet-stream;

##輸出日誌格式
log_format  main  '$remote_addr - $request_time - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent $request_body "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for" ';


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       80;//訪問埠
listen       [::]:80;
        server_name  localhost;//訪問地址本機


        #access_log  logs/host.access.log  main;
add_header "X-UA-Compatible" "IE=Edge, chrome=1";
location / {
proxy_pass  http://127.0.0.1:8084;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log off;//關閉access_log日誌記錄
        }
        location /front/product/toProduct {
proxy_pass  http://127.0.0.1:8084;
access_log  logs/host.access.log  main;//指定日誌檔案
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
location /front/productList/toProductList {
proxy_pass  http://127.0.0.1:8084;
access_log  logs/host.access.log  main;//指定日誌檔案
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;//開啟記錄客戶端真實IP,使用反向代理,沒有此句,則系統無法獲得客戶端真實IP,只能獲得區域網IP
        }
location /front/productList/queryProducts {
proxy_pass  http://127.0.0.1:8084;
access_log  logs/host.access.log  main;//指定日誌檔案
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
location /customer/login {
proxy_pass  http://127.0.0.1:8084;
access_log  logs/host.access.log  main;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }



        #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;
        }


        # 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;
    #    }
    #}


}