配置 Nginx
先来看一个实际的配置文件:
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr|||$remote_user|||$time_local|||$request_uri' '|||$status|||$body_bytes_sent|||$http_referer' '|||$http_user_agent|||$http_x_forwarded_for'; access_log off; #access_log logs/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 81; server_name localhost; #charset koi8-r; access_log /usr/local/nginx/nginxlog/access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
nginx 日志相关指令主要有两条,
log_format,用来设置日志格式,
access_log,用来指定日志文件的存放路径、格式和缓存大小
1、log_format 格式
log_format name( 格式名字) 格式样式(即想要得到什么样的日志内容)
默认的示例:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_s ent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
注释:
$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
$remote_user :用来记录客户端用户名称;
$time_local : 用来记录访问时间与时区;
$request : 用来记录请求的url与http协议;
$request_uri: /stat.php?id=1585378&web_id=1585378
$uri /stat.php
$document_uri: /stat.php
$status : 用来记录请求状态;成功是200,
$body_bytes_s ent :记录发送给客户端文件主体内容大小;
$http_referer :用来记录从那个页面链接访问过来的;
$http_user_agent :记录客户毒啊浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址;
log_format mylogformat ' $http_x_forwarded_for- $remote_user [$time_local] '
' "$request" '$status $body_bytes_s ent '
' "$http_referer" "$http_user_agent" ';
2、用access_log指令日志文件存放路径;
用了log_format 指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;
access_log path(存放路径) format (自定义日志名称)
示例:
#access_log logs/access.log main;
我们用log_format 定义了一个mylogformat的日志 我们可以写成这样
access_log logs/access.log mylogformat ;
如果不想启用日志 :
access_log off ;
在定义日志目录中要注意的是,
1、nginx进程设置的用户和组必须有对该路径 创建文件的权限,假设nginx的usr指令设置的用户名 和用户组都是www,而logs 目录的用户名和组是root,那么日志文件将无法被创建;
2、一般情况下,如果nginx.conf配置文件里的http里面没设置access_log off;默认是会把所有访问日志输出到/var/log/nginx目录下面,现在要把不同网站的日志区分开来,所以添加了access_log off。
按照之前很多网上的配置方法是在各个虚拟主机配置文件的server段里添加日志格式和日志输出路径,如下
log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /home/wwwlogs/ddhow.com.log access; error_log /home/wwwlogs/ddhow.com-error.log;
lua记录日志:
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format tick "$msec|||$u_t|||$remote_addr|||$u_domain|||$u_url|||$u_title|||$u_referrer|||$u_sh|||$u_sw|||$u_cd|||$u_lang|||$http_user_agent|||$u_utrace|||$u_account|||$u_time"; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; location /1.gif { #伪装成gif文件 default_type image/gif; #本身关闭access_log,通过subrequest记录log access_log off; access_by_lua " -- 用户跟踪cookie名为__utrace local uid = ngx.var.cookie___utrace if not uid then -- 如果没有则生成一个跟踪cookie,算法为md5(时间戳+IP+客户端信息) uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent) end ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'} if ngx.var.arg_domain then -- 通过subrequest到/i-log记录日志,将参数和用户跟踪cookie带过去 ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime()) end "; #此请求不缓存 add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT"; add_header Pragma "no-cache"; add_header Cache-Control "no-cache, max-age=0, must-revalidate"; #返回一个1×1的空gif图片 empty_gif; } location /i-log { #内部location,不允许外部直接访问 internal; #设置变量,注意需要unescape set_unescape_uri $u_domain $arg_domain; set_unescape_uri $u_t $arg_t; set_unescape_uri $u_url $arg_url; set_unescape_uri $u_title $arg_title; set_unescape_uri $u_referrer $arg_referrer; set_unescape_uri $u_sh $arg_sh; set_unescape_uri $u_sw $arg_sw; set_unescape_uri $u_cd $arg_cd; set_unescape_uri $u_lang $arg_lang; set_unescape_uri $u_utrace $arg_utrace; set_unescape_uri $u_account $arg_account; set_unescape_uri $u_time $arg_time; #打开日志 log_subrequest on; #记录日志到ma.log,实际应用中最好加buffer,格式为tick access_log /usr/local/nginx/nginxlog/access.log tick; #输出空字符串 echo ''; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # remove the robots line if you want to use wordpress' virtual robots.txt location = /robots.txt { access_log off; log_not_found off; } location = /favicon.ico { access_log off; log_not_found off; } # this prevents hidden files (beginning with a period) from being served location ~ /\. { access_log off; log_not_found off; deny all; } } }
相关推荐
配置nginx日志 实验环境 kali系统 实验步骤 1.查看nginx版本情况 在kali中输入“nginx -t”来查看配置文件信息。 2.打开主配置文件“/etc/nginx/nginx.conf” 3.刚安装的nginx,配置文件里可能信息不齐导致无法启动...
Nginx日志描述 通过访问日志,你可以得到用户地域来源、跳转来源、使用终端、某个URL访问量等相关信息;通过错误日志,你可以得到系统某个服务或server的性能瓶颈等。因此,将日志好好利用,你可以得到很多有价值的...
可一键配置nginx日志生成格式,并进行图形化解析,充分了解访客数据与后台服务运行状况。 docker容器部署 将nginx与nginxWebUI集成到一个docker镜像中,完美的打造一个拥有图形界面的nginx运行包,不与服务器上...
Nginx配置负载均衡、配置网页数据动静分离、请求日志打印详细配置、日志自动切割、自动压缩、自动删除
本资源结合我的博客一并使用,用于解决filebeat收集nginx日志用的
将在 logs目录下生成类似 2014-01-21.access.log 的log文件,这个日期会根据服务器的时间变化) 配置可参照 nginx目录下的 conf/nginx.conf 文件进行log配置 源码留给有特殊需要的朋友自己编译。此源码就是修改后的...
配置nginx日志 打开/etc/nginx/nginx.conf文件,查看log_format与access_log的配置 cat /etc/nginx/nginx.conf 配置日志文件存放位置/var/log/nginx/access.log 使用浏览器访问nginx并查看日志记录 打开/etc/...
本篇文章主要介绍了详解Nginx日志配置及日志切割,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Nginx 日志分析 AWStats + JAWStats 安装配置 Awstats 是一款基于 Perl 的 WEB 日志分析工具,而 JAWStats 是一款基于 PHP 的统计套件,旨在为 Awstats 提供更为精美图形界面。
nginx针对syslog的补丁
: 为限制 Nginx日志文件大小增加,文件过大,导致无法收缩日志,在配置好的nginx的安装目录下,可以采用批处理文件对日志,根据实际需要的时间进行自动切割,本脚本以天为单位进行保存。对于日志备份功能,因存储...
Nginx日志主要分为两种:访问日志和错误日志。日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的。 访问日志主要记录客户端访问Nginx的每一个请求,格式可以...
windows安装nginx-1.14.0,附带有清理日志任务程序XML文件。安装服务后,设置服务自动启动。修改XML配置(对应文件路径),然后倒入到任务程序运行。
本文实现的功能是在吗每天凌晨00:00把前一天的Nginx日志access.log重命名为access-xxxx-xx-xx.log格式,例如:access-2016-10-01.log,下面话不多说了,来看看详细的实现方法吧。 实现方法 脚本 vim /opt/nginx/...
Web应用安全:Nginx日志配置.pptx
本文总结了nginx日志相关的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。 nginx有一个非常灵活的日志记录模式。每个级别的配置可以有各自独立的...
Web应用安全:Nginx日志配置文本.docx
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 error_log /var/log/...
今天在do的VPS配置Nginx虚拟主机时,修改配置文件后,重启Nginx后一直报告失败,但是不知道哪里错了,直觉觉得是配置文件配置错了,google了下解决方案。 解决方案 Nginx启动或重启失败,一般是因为配置文件出错了,...