nginx 笔记小结

nginx日志

nginx的日志有两种:
error_log是错误日志
access_log是使用日志

日志级别: debug > info > notice > warn > error > crit > alert > emerg

error_log: nginx处理http请求错误状态,以及nginx服务本身运行错误的状态,以不同错误级别记录到error_log内.

Syntax:error_log file [level];

Default: error_log logs/error.log error;

Context:main,http,mail,stream,server,location

access_log: 记录nginx每一次的http请求的访问状态,主要用于去分析每一次http的请求和客户端的交互以及对行为的一些分析

Syntax:access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

access_log off;

Default:access_log logs/access.log combined;

Context:http,server,location,if in location,limit_except

nginx日志配置参考

log_format nginx_upload_log '$time_local,$msec,$request_length,$request_time,"$upstream_response_time",$body_bytes_sent,$status,$remote_addr-"$http_x_forwarded_for",$request,"$request_body",UPS/"$upstream_addr","$http_user_agent",$http_referer,$host,"$http_s_cid","$http_s_pid","$http_s_ppid","$http_p_appid",$upstream_response_length,$upstream_status';

access_log  /opt/logs/nginx/access_${host}.log  nginx_upload_log;
#access_log  /opt/logs/nginx/access_${server_name}.log  nginx_upload_log;

nginx忽略favicon.ico日志

location = /favicon.ico {
	log_not_found off;
	access_log off;
}
//log_not_found on|off,默认为on:启用或禁用404错误日志,
这个指令可以用来禁止nginx记录找不到rebots.txt或favicon.ico这类文件的错误信息。

负载均衡策略

  • Round-Robin 默认
  • ip_hash
    ip_hash; #保证每个访客固定访问一个后端服务器
  • 一致性hash
    hash consistent
  • least_conn
    least_conn; #把请求转发给连接数较少的后端服务器

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!