Nginx 记录请求分发日志

转载自:http://greatwqs.iteye.com/blog/1885420

在nginx接收到请求之后, 需把请求分发到后端WEB服务集群.

在这里需要记录分发日志, 来分析后端每台WEB服务器处理的请求数目.

Text代码   收藏代码
  1. http {  
  2. log_format  main    
  3.   ' $remote_user [$time_local]  $http_x_Forwarded_for $remote_addr  $request '  
  4.  '$http_x_forwarded_for '                       
  5.  '$upstream_addr '                        
  6.  'ups_resp_time: $upstream_response_time '                        
  7.  'request_time: $request_time';  
  8.   
  9. access_log  logs/access.log  main;  
  10.   
  11. server{}  
  12. ...  
  13. }  

 在日志显示的信息为:

Text代码   收藏代码
  1. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.010 request_time: 0.011  
  2. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.16:8188 ups_resp_time: 0.006 request_time: 0.006  
  3. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.013 request_time: 0.013  
  4. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.17:8188 ups_resp_time: 0.003 request_time: 0.003  
  5. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.18:8188 ups_resp_time: 0.004 request_time: 0.004  
  6. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.012 request_time: 0.013  
  7. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.18:8188 ups_resp_time: 0.005 request_time: 0.005  
  8. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.16:8188 ups_resp_time: 0.011 request_time: 0.011  
  9. - [31/May/2013:00:01:03 -0700]  - xxx.ip.addr.xxx  GET /portal/index.html HTTP/1.1 - 192.168.100.15:8188 ups_resp_time: 0.447 request_time: 0.759  

 全部配置文件nginx.conf.

Text代码   收藏代码
  1. # greatwqs@163.com Install on 2012-08-11 linux  
  2. # user  devwqs;  
  3.   
  4. 2 intel(R) xeon(R) CPU  
  5. worker_processes  4;  
  6.   
  7. worker_cpu_affinity 00000001 00000010 00000100 00001000;  
  8.   
  9. # error_log  logs/error.log;  
  10. # error_log  logs/error.log  notice;  
  11. error_log   logs/error.log  error;  
  12.   
  13. pid        logs/nginx.pid;  
  14.   
  15. # allow openning file nums  
  16. worker_rlimit_nofile 25600;  
  17.   
  18. events {  
  19.     # linux 2.6 upper version.  
  20.     use epoll;  
  21.     worker_connections  51200;  
  22. }  
  23.   
  24. http {  
  25.     include       mime.types;  
  26.     default_type  application/octet-stream;  
  27.   
  28.     # log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  29.     #                   '$status $body_bytes_sent "$http_referer" '  
  30.     #                   '"$http_user_agent" "$http_x_forwarded_for"'  
  31.     #                   '"$upstream_addr"' '"$upstream_response_time"';  
  32.     log_format  main  ' $remote_user [$time_local]  $http_x_Forwarded_for $remote_addr  $request '  
  33.                       '$http_x_forwarded_for '  
  34.                       '$upstream_addr '  
  35.                       'ups_resp_time: $upstream_response_time '  
  36.                       'request_time: $request_time';  
  37.   
  38.     access_log  logs/access.log  main;  
  39.   
  40.     sendfile                     on;  
  41.     # tcp_nopush                 on;  
  42.                                    
  43.     keepalive_requests           200;  
  44.     keepalive_timeout            20;  
  45.     gzip  on;                      
  46.     client_body_buffer_size      128k;  
  47.     client_body_timeout          60s;  
  48.     client_max_body_size         10m;  
  49.     # proxy_buffer_size          8k;  
  50.     # proxy_busy_buffers_size    64k;  
  51.     proxy_temp_file_write_size   64k;  
  52.   
  53.     # portal-cluster  
  54.     upstream portal-cluster {  
  55.         # http://192.168.100.15:8188/portal/  
  56.         server 192.168.100.15:8188 weight=5 max_fails=5 fail_timeout=30s;  
  57.   
  58.         # http://192.168.100.16:8188/portal/  
  59.         server 192.168.100.16:8188 weight=5 max_fails=5 fail_timeout=30s;  
  60.           
  61.         # http://192.168.100.17:8188/portal/  
  62.         server 192.168.100.17:8188 weight=5 max_fails=5 fail_timeout=30s;  
  63.           
  64.         # http://192.168.100.18:8188/portal/  
  65.         server 192.168.100.18:8188 weight=5 max_fails=5 fail_timeout=30s;  
  66.     }  
  67.   
  68.     # manage-cluster  
  69.     upstream manage-cluster {  
  70.         # http://192.168.100.25:8189/manage/  
  71.         server 192.168.100.25:8189 weight=4 max_fails=5 fail_timeout=30s;  
  72.   
  73.         # http://192.168.100.26:8189/manage/  
  74.         server 192.168.100.26:8189 weight=6 max_fails=5 fail_timeout=30s;  
  75.     }  
  76.   
  77.     # External Internet.  
  78.     server {  
  79.         listen       80;  
  80.         server_name  www.huaxixiang.com;  
  81.         access_log   logs/host.access.log  main;  
  82.   
  83.         location /portal/ {  
  84.             # root   html;  
  85.             # index  index.html index.htm;  
  86.             # nginx http header send to tomcat app.  
  87.             # proxy_set_header Host  $host;  
  88.             # proxy_set_header X-Forwarded-For  $remote_addr;  
  89.             proxy_set_header Host $host;  
  90.             proxy_set_header X-Real-Ip $remote_addr;  
  91.             proxy_set_header X-Forwarded-For $remote_addr;  
  92.   
  93.             proxy_pass http://portal-cluster;  
  94.         }  
  95.   
  96.         # nginx status  
  97.         location /nginx_status {  
  98.             # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/  
  99.             stub_status  on;  
  100.             access_log   off;  
  101.             allow        192.168.100.100;  
  102.             #deny all;  
  103.         }  
  104.   
  105.         location / {  
  106.             root   html;  
  107.             index  index.html index.htm;  
  108.         }  
  109.         error_page  404              /404.html;  
  110.   
  111.         # redirect server error pages to the static page /50x.html  
  112.         error_page   500 502 503 504  /50x.html;  
  113.         location = /50x.html {  
  114.             root   html;  
  115.         }  
  116.     }  
  117.   
  118.     # External Internet.  
  119.     server {  
  120.         listen       80;  
  121.         server_name  manage.huaxixiang.com;  
  122.   
  123.         access_log  logs/host.access.log  main;  
  124.   
  125.         location /manage/ {  
  126.             proxy_pass http://manage-cluster;  
  127.         }  
  128.   
  129.         location / {  
  130.             root   html;  
  131.             index  index.html index.htm;  
  132.         }  
  133.   
  134.         error_page  404              /404.html;  
  135.   
  136.         # redirect server error pages to the static page /50x.html  
  137.         error_page   500 502 503 504  /50x.html;  
  138.         location = /50x.html {  
  139.             root   html;  
  140.         }  
  141.     }  
  142. }  

nginx status查看:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值