一、首先查看nginx版本,我使用的是1.9.7的版本,安裝目錄在/application/nginx-1.9.7
[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx -Vnginx version: nginx/1.9.7built 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 -tnginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.9.7/conf/nginx.conf test is successful[root@AnSheng ~]# /application/nginx-1.9.7/sbin/nginx
三、把nginx配置文件內多余的注視行和空行刪掉
[root@AnSheng ~]# cd /application/nginx-1.9.7/conf/[root@AnSheng conf]# egrep -v "#|^$" nginx.conf.defaultworker_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標簽內加入以下標簽和內容
location /logs { alias /application/nginx-1.9.7/logs; #Nginx日志目錄 autoindex on; #打開目錄瀏覽功能 autoindex_exact_size off; #默認為on,顯示出文件的確切大小,單位是bytes #顯示出文件的大概大小,單位是kB或者MB或者GB autoindex_localtime on; #默認為off,顯示的文件時間為GMT時間。 #改為on后,顯示的文件時間為文件的服務器時間 add_header Cache-Control no-store; #讓瀏覽器不保存臨時文件}
五、開啟在瀏覽器打開log文件,如果不開啟再點擊文件的時候就下載而不是打開
[root@AnSheng conf]# vim mime.typestypes { 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 -tnginx: the configuration file /application/nginx-1.9.7/conf/nginx.conf syntax is oknginx: 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用戶認證。
七、安裝httpd-tools,用于帳號密碼生成
[root@AnSheng ~]# yum -y install httpd-tools
八、創建認證的賬號
[root@AnSheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguserNew password:Re-type new password:Adding password for user loguser#密碼需要輸入兩次
九、編輯nginx配置文件,在logs的location加入下面的內容
location /logs { ...... alias PATH; autoindex on; autoindex_exact_size off; autoindex_localtime on; add_header Cache-Control no-store; auth_basic "Restricted"; #Nginx認證 auth_basic_user_file /application/nginx-1.9.7/conf/loguser; #認證賬號密碼保存的文件}
十、然后再打開的時候就會提示輸入賬號和密碼,登陸之后才可以查看。
十一、總結
以上就是利用Nginx實現瀏覽器可實時查看訪問日志的全部步驟,希望對大家的學習或者工作有所幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答