Nginx實現(xiàn)瀏覽器可實時查看訪問日志的步驟詳解
一、首先查看nginx版本,我使用的是1.9.7的版本,安裝目錄在/application/nginx-1.9.7
[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx -V nginx version: nginx/1.9.7 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) configure arguments: --prefix=/application/nginx-1.9.7 --user=nginx --group=nginx --with-http_stub_status_module
二、檢查語法并啟動nginx
[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx -t nginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.9.7/conf/nginx.conf test is successful [root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx
三、把nginx配置文件內(nèi)多余的注視行和空行刪掉
[root@AnSheng ~]# cd /application/nginx-1.9.7/conf/
[root@AnSheng conf]# egrep -v "#|^$" nginx.conf.default
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@AnSheng conf]# egrep -v "#|^$" nginx.conf.default nginx.conf
四、在nginx配置文件的server標(biāo)簽內(nèi)加入以下標(biāo)簽和內(nèi)容
location /logs {
alias /application/nginx-1.9.7/logs;
#Nginx日志目錄
autoindex on;
#打開目錄瀏覽功能
autoindex_exact_size off;
#默認(rèn)為on,顯示出文件的確切大小,單位是bytes
#顯示出文件的大概大小,單位是kB或者M(jìn)B或者GB
autoindex_localtime on;
#默認(rèn)為off,顯示的文件時間為GMT時間。
#改為on后,顯示的文件時間為文件的服務(wù)器時間
add_header Cache-Control no-store;
#讓瀏覽器不保存臨時文件
}
五、開啟在瀏覽器打開log文件,如果不開啟再點擊文件的時候就下載而不是打開
[root@AnSheng conf]# vim mime.types
types {
text/html html htm shtml;
text/log log;
text/css css;
text/xml xml;
.............
六、檢測語法,然后讓nginx配置生效,在瀏覽器查看
[root@AnSheng conf]# /application/nginx-1.9.7/sbin/nginx -t nginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.9.7/conf/nginx.conf test is successful [root@AnSheng conf]# /application/nginx-1.9.7/sbin/nginx -s reload
打開瀏覽器輸入域名或者IP,后面加上logs,然后點擊文件就可以打開了,如果日志隨隨便便就可以被別人查看是不是很不安全,所以我們要在加一層nginx用戶認(rèn)證。


七、安裝httpd-tools,用于帳號密碼生成
[root@AnSheng ~]# yum -y install httpd-tools
八、創(chuàng)建認(rèn)證的賬號
[root@AnSheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguser New password: Re-type new password: Adding password for user loguser #密碼需要輸入兩次
九、編輯nginx配置文件,在logs的location加入下面的內(nèi)容
location /logs {
......
alias PATH;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
add_header Cache-Control no-store;
auth_basic "Restricted";
#Nginx認(rèn)證
auth_basic_user_file /application/nginx-1.9.7/conf/loguser;
#認(rèn)證賬號密碼保存的文件
}
十、然后再打開的時候就會提示輸入賬號和密碼,登陸之后才可以查看。

十一、總結(jié)
以上就是利用Nginx實現(xiàn)瀏覽器可實時查看訪問日志的全部步驟,希望對大家的學(xué)習(xí)或者工作有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
ngin配置301重定向設(shè)置方法和nginx子目錄301重定向
這篇文章主要介紹了ngin配置301重定向設(shè)置方法和nginx子目錄301重定向,需要的朋友可以參考下2014-04-04
nginx?proxy_pass轉(zhuǎn)發(fā)規(guī)則解讀
這篇文章主要介紹了nginx?proxy_pass轉(zhuǎn)發(fā)規(guī)則,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

