1.在nginx.conf配置,配置在http标签内
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; }
字段意义
$remote_addr #记录访问网站的客户端地址 $remote_user #远程客户端用户名 $time_local #记录访问时间与时区 $request #用户的http请求起始行信息 $status #http状态码,记录请求返回的状态码,例如:200、301、404等 $body_bytes_sent #服务器发送给客户端的响应body字节数 $http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。 $http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等 $http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置
2.使配置生效,配置在server标签内(针对某站点配置)
server { listen 80; server_name abc.com www.abc.com; location / { root /data/www/www; index index.html index.htm; } access_log /var/log/nginx/access.log main; }
3.验证是否正确
nginx -t
4.重启生效(pid无变化)
nginx -s reload