在主配置文件中已经定义了配置文件的格式

[root@LNMP ~]# vim /usr/local/nginx/conf/nginx.conf  # nginx 中的配置修改 日志命名格式


 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

  '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';


$remote_addr # 远程的IP  $http_x_forwarded_for #代理的IP  

[$time_local] # 时间  '$host  域名  "$request_uri # 访问的地址    $status'# 状态


combined_realip 这个格式名是可以自定义的。此处楼主改为了 “linux” ,在虚拟配置文件中使用


[root@LNMP ~]# vim /usr/local/nginx/conf/vhosts/linux.conf 

server

{

    listen 80;

    server_name www.linux.com www.test.com;

    if ($host !='www.linux.com')

    {

       rewrite ^/(.*)$ http://www.linux.com/$1 permanent;

    }

    index index.html index.htm index.php;

    root /data/dis;

    access_log /usr/local/nginx/logs/access.log linux;


access_log /usr/local/nginx/logs/access.log linux;  在linux.conf 中加入这一行代码。

wKioL1g-2_-BVHuKAACs1NDRqLw857.jpg

[root@LNMP ~]# /usr/local/nginx/sbin/nginx -t


[root@LNMP ~]# /usr/local/nginx/sbin/nginx -s reload



[root@LNMP ~]# curl -x127.0.0.1:80 www.test.com/kkkiiiillll  # 产生访问日志,查看日志


wKioL1g-3auwrJvwAASM7tmPG44573.jpg

上图中 箭头所示的 png,gif 等等这些网站的素材图片是可以不需要记录访问日志的,因为没有做限制,所以可以看到访问日志。那么接下来,我们可以根据需要设置,记录我们需要的日志。

 location 是有先后顺序的,要注意,有可能先的匹配了,就不再往下执行了。

[root@LNMP ~]# vim /usr/local/nginx/conf/vhosts/linux.conf #在 用户认证下面加入以下代码

 

 location ~ .*\.(gif|jpeg|jpg|png|bmp|swf)$

    {

          access_log off;

    }

    location ~(static|cache)

    {

             access_log off;

    }


保存退出后,检测配有没有问题,然后重新加载nginx .


[root@LNMP ~]# > /usr/local/nginx/logs/access.log   #清空log 日志记录,以便区分

再次刷新论坛网页。已经png ,gif ,jpg 等等图片文件的访问日志已经没有了,同上图比较。

wKioL1g_6TWD1S1uAATZ6pQt3_0235.jpg