nginx日志疑问

zhzh 发布于 2011/04/13 16:03
阅读 1K+
收藏 1

我的配置文件中日志的格式为:

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';

当从客户端访问时,nginx日志如下:

192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET / HTTP/1.1" 200 12233 "-" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /tomcat.css HTTP/1.1" 304 0 "http://192.168.1.9/" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /asf-logo.png HTTP/1.1" 304 0 "http://192.168.1.9/" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /tomcat.png HTTP/1.1" 304 0 "http://192.168.1.9/" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /favicon.ico HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /bg-nav.png HTTP/1.1" 304 0 "http://192.168.1.9/tomcat.css" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /bg-upper.png HTTP/1.1" 304 0 "http://192.168.1.9/tomcat.css" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /bg-middle.png HTTP/1.1" 304 0 "http://192.168.1.9/tomcat.css" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.181 - - [13/Apr/2011:15:19:10 +0800] "GET /bg-button.png HTTP/1.1" 304 0 "http://192.168.1.9/tomcat.css" "Mozilla/5.0 (Windows NT 6.1; rv:2.0) Gecko/20100101 Firefox/4.0" "-"
192.168.1.114 - - [13/Apr/2011:15:19:37 +0800] "GET / HTTP/1.0" 200 12220 "-" "-" "-"
192.168.1.114 - - [13/Apr/2011:15:20:22 +0800] "GET / HTTP/1.0" 200 12220 "-" "-" "-"

为什么x_forwarded_for的值始终为NULL?

加载中
0
张宋付
张宋付
x-forwarded-for是显示你客户端是否有做代理。有代理的话。这个就起作用。没有的的话这个应该是没有什么东西。有关这个你可以尝试一下把你browse上面设一下代理,你的这个就会出现你的代理ip的,如果是代理的代理的话,这个显示就是 proxy1-ip,proxy2-ip,......,proyx*-ip
0
红薯
红薯

为什么 x_forwarded_for 不为 NULL 呢?浏览器又不会送着东西过来,这个只用于多个代理之间嵌套的

例如 Browser -> Nginx -> Nginx -> Nginx -> 应用服务器

0
z
zhzh

不好意思,我不太理解您说的,192.168.1.181是我的本机ip,那么x_forwarded_for所对应的值不应该为192.168.1.181吗

0
sunyh
sunyh

引用来自#2楼“红薯”的帖子

为什么 x_forwarded_for 不为 NULL 呢?浏览器又不会送着东西过来,这个只用于多个代理之间嵌套的

例如 Browser -> Nginx -> Nginx -> Nginx -> 应用服务器

我想问您一个和这个问题不相关的问题,我怎么设置 让我可以知道每次nginx 都发给哪个机器了。  可以设置日志吗?让我看到每个请求都发给了哪个服务器

0
z
zhzh

我的配置文件如下:


#user  nobody;
worker_processes  3;

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

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
}



http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;


    client_body_buffer_size 512k;
    proxy_connect_timeout 5;
    proxy_read_timeout 60;
    proxy_send_timeout 5;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_temp_path /data/nginx/proxy_temp_dir;
    proxy_cache_path /data/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
    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/test.log main;
    access_log  /data/nginx/logs/$host.access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay on;

    upstream backend
    {
                server   127.0.0.1:8010 max_fails=1  fail_timeout=60s;
                server  127.0.0.1:8020 max_fails=1  fail_timeout=60s;
    }

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {
            root   html;
                index  index.html index.htm;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_cache cache_one;
                proxy_cache_valid 200 304 12h;
                proxy_cache_key $host$request_uri$is_args$args;
                proxy_set_header       Host $host;
                proxy_set_header  X-Real-IP $remote_addr; #X-Real-IP
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass  http://backend;
                expires 10s;
        }

        #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 /city {
#           city on;
#        }     

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

还有,我看了下http://www.oschina.net/bbs/thread/16260中有关日志的问题,

想问下红薯:我这是本机访问打出的日志,如果我换台机器登陆,nginx请求时的那个WEB地址,及记录客户端源地址会显示吗?

0
z
zhzh

我换了台机器访问,日志如下:

192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET / HTTP/1.1" 200 12233 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /tomcat.css HTTP/1.1" 200 5711 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /asf-logo.png HTTP/1.1" 200 17811 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /tomcat.png HTTP/1.1" 200 5103 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /bg-nav.png HTTP/1.1" 200 1401 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /bg-upper.png HTTP/1.1" 200 3103 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /bg-button.png HTTP/1.1" 200 713 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /bg-middle.png HTTP/1.1" 200 1918 "http://192.168.1.9/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
192.168.1.253 - - [13/Apr/2011:16:28:08 +0800] "GET /favicon.ico HTTP/1.1" 200 21630 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)" "-"
客户端源地址还是没有显示出来

0
红薯
红薯

客户端的源地址是 $remote_addr 不是这个 $x_forward_for

0
红薯
红薯

proxy_set_header  X-Forwarded-For $remote_addr;

实在要的话可以这样,也不知道你为什么一根筋非要用 $x-forwarded_for 来取客户端地址

0
z
zhzh

谢谢红薯,我知道了。

还有个疑问,我该怎样配置,才能使得x-forwarded-for的值为所访问的客户端的ip

返回顶部
顶部