且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nginx日志问题解决方法记录

更新时间:2022-09-22 16:16:15

nginx版本信息如下

#nginx -V
nginx version: nginx/1.2.0  最新稳定版本
built by gcc 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-ath=/var/log/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=www --group=www --with-http_ssl_module --with-ttp_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_stub_status_module --with-google_perftools_module --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-client-body-temp-path=/var/tmp/nginx/client --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi

 nginx的配置如下


  1. user  www; 
  2. worker_processes  2; 
  3. google_perftools_profiles /var/tmp/nginx/tcmalloc/tcmalloc; 
  4. events { 
  5.     use epoll; 
  6.     worker_connections  51000; 
  7. http 
  8.     include       mime.types; 
  9.     default_type  application/octet-stream; 
  10.     keepalive_timeout     65; 
  11.     sendfile              on; 
  12.     tcp_nopush            on; 
  13.     tcp_nodelay           on; 
  14.     client_header_timeout 10; 
  15.     client_body_timeout   10; 
  16.     send_timeout          10; 
  17.     gzip  on; 
  18.     gzip_min_length       1k; 
  19.     gzip_buffers       4 16k; 
  20.     gzip_http_version    1.1; 
  21.     gzip_comp_level        2; 
  22.     gzip_types  text/plain application/x-javascript text/css applocation/xml; 
  23.     server 
  24.         listen       80; 
  25.         server_name  localhost; 
  26.         location / { 
  27.             root   html; 
  28.             index  index.html index.htm; 
  29.          } 
  30.         error_page   500 502 503 504  /50x.html; 
  31.         location = /50x.html { 
  32.             root   html; 
  33.          } 
  34.         location ~ \.php$ { 
  35.             root           html; 
  36.             fastcgi_pass   127.0.0.1:9000; 
  37.             fastcgi_index  index.php; 
  38.             fastcgi_param  SCRIPT_FILENAME  /usr/html$fastcgi_script_name; 
  39.             include        fastcgi_params; 
  40.          }   
  41.     log_format  welog  '$remote_addr - $remote_user [$time_local] "$request" ' 
  42.                '$status $body_bytes_sent "$http_referer" ' 
  43.                 '"$http_user_agent" "$http_x_forwarded_for"'; 
  44.      access_log  /var/log/nginx/access.log  weblog; 
  45.        } 

  1. 启动nginx的时候,出现警告信息
  2. #service nginx restart 
  3. nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/nginx.conf:97 
  4. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
  5. nginx: configuration file /etc/nginx/nginx.conf test is successful 
  6. Stopping nginx:                                            [  OK  ] 
  7. Starting nginx: nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/nginx.conf:97 
  8.                                                            [  OK  ] 
  9. 这里的警告意思是日志需要放在http内,所以做如下更改即可

 修改后的nginx.conf文件如下


  1. user  www;  
  2. worker_processes  2;  
  3. google_perftools_profiles /var/tmp/nginx/tcmalloc/tcmalloc;  
  4. events {  
  5.     use epoll;  
  6.     worker_connections  51000;  
  7.         }  
  8. http  
  9. {  
  10.     include       mime.types;  
  11.     default_type  application/octet-stream;  
  12.     keepalive_timeout     65;  
  13.     sendfile              on;  
  14.     tcp_nopush            on;  
  15.     tcp_nodelay           on;  
  16.     client_header_timeout 10;  
  17.     client_body_timeout   10;  
  18.     send_timeout          10;  
  19.     gzip  on;  
  20.     gzip_min_length       1k;  
  21.     gzip_buffers       4 16k;  
  22.     gzip_http_version    1.1;  
  23.     gzip_comp_level        2;  
  24.     gzip_types  text/plain application/x-javascript text/css applocation/xml;  
  25.     server  
  26.         listen       80;  
  27.         server_name  localhost;  
  28.         location / {  
  29.             root   html;  
  30.             index  index.html index.htm;  
  31.                     }  
  32.         error_page   500 502 503 504  /50x.html;  
  33.         location = /50x.html {  
  34.             root   html;  
  35.                               }  
  36.         location ~ \.php$ {  
  37.             root           html;  
  38.             fastcgi_pass   127.0.0.1:9000;  
  39.             fastcgi_index  index.php;  
  40.             fastcgi_param  SCRIPT_FILENAME  /usr/html$fastcgi_script_name;  
  41.             include        fastcgi_params;  
  42.                             } 
  43. }    注意,下面的日志是放在server之外的,即出现警告是放的位置不对    
  44.     log_format  welog  '$remote_addr - $remote_user [$time_local] "$request" '  
  45.                        '$status $body_bytes_sent "$http_referer" '  
  46.                        '"$http_user_agent" "$http_x_forwarded_for"';  
  47.      access_log  /var/log/nginx/access.log  weblog;  
  48.                 
  49.  }  

重启nginx服务,则OK了,


  1. service nginx restart      
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful 
  4. Stopping nginx:                                            [  OK  ] 
  5. Starting nginx:                                            [  OK  ] 

如果我需要在server里面定义日志,该怎么实现呢,如下


  1. user  www;   
  2. worker_processes  2;   
  3. google_perftools_profiles /var/tmp/nginx/tcmalloc/tcmalloc;   
  4. events {   
  5.     use epoll;   
  6.     worker_connections  51000;   
  7.         }   
  8. http   
  9.   
  10.     include       mime.types;   
  11.     default_type  application/octet-stream;   
  12.     keepalive_timeout     65;   
  13.     sendfile              on;   
  14.     tcp_nopush            on;   
  15.     tcp_nodelay           on;   
  16.     client_header_timeout 10;   
  17.     client_body_timeout   10;   
  18.     send_timeout          10;   
  19.     gzip  on;   
  20.     gzip_min_length       1k;   
  21.     gzip_buffers       4 16k;   
  22.     gzip_http_version    1.1;   
  23.     gzip_comp_level        2;   
  24.     gzip_types  text/plain application/x-javascript text/css applocation/xml;   
  25.     server   
  26.         listen       80;   
  27.         server_name  localhost;   
  28.         location / {   
  29.             root   html;   
  30.             index  index.html index.htm;   
  31.                     }   
  32.         error_page   500 502 503 504  /50x.html;   
  33.         location = /50x.html {   
  34.             root   html;   
  35.                               }   
  36.         location ~ \.php$ {   
  37.             root           html;   
  38.             fastcgi_pass   127.0.0.1:9000;   
  39.             fastcgi_index  index.php;   
  40.             fastcgi_param  SCRIPT_FILENAME  /usr/html$fastcgi_script_name;   
  41.             include        fastcgi_params;
  42.  }   
  43.             access_log /var/log/nginx/access2.log;  此处定义的日志格式,不需要在后面加日志格式    
  44.     注意,下面的日志是放在server之外的,即出现警告是放的位置不对     
  45.      log_format  welog  '$remote_addr - $remote_user [$time_local] "$request" '   
  46.                         '$status $body_bytes_sent "$http_referer" '   
  47.                         '"$http_user_agent" "$http_x_forwarded_for"';   
  48.      access_log  /var/log/nginx/access.log  weblog;   
  49.                  
  50.  }  

本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/851716,如需转载请自行联系原作者