How does Nginx enable the browser to view access logs in real time?

WBOY
Release: 2023-05-15 12:31:22
forward
1456 people have browsed it

1. First check the nginx version. I am using version 1.9.7. The installation directory is /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
Copy after login

2. Check Grammar and start 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
Copy after login

3. Delete the redundant attention lines and blank lines in the nginx configuration file

[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
Copy after login

4. In nginx Add the following tags and content to the server tag of the configuration file

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;
 #让浏览器不保存临时文件
}
Copy after login

5. Enable the log file to be opened in the browser. If it is not enabled, it will be downloaded instead of opened when you click the file

[root@ansheng conf]# vim mime.types
types {
 text/html html htm shtml;
 text/log log;
 text/css css;
 text/xml xml;
 .............
Copy after login

6. Check the syntax, then let the nginx configuration take effect, view it in the browser

[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
Copy after login

Open the browser and enter the domain name or IP, add logs after it, and then click The file can be opened. Is it unsafe if the log can be viewed by others casually, so we need to add a layer of nginx user authentication.

How does Nginx enable the browser to view access logs in real time?

How does Nginx enable the browser to view access logs in real time?

##7. Install httpd-tools for account and password generation

[root@ansheng ~]# yum -y install httpd-tools
Copy after login

8. Create a certified account

[root@ansheng ~]# htpasswd -c /application/nginx-1.9.7/conf/loguser loguser
new password:
re-type new password:
adding password for user loguser
#密码需要输入两次
Copy after login

9. Edit the nginx configuration file and add the following content to the 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;
 #认证账号密码保存的文件
}
Copy after login

10 , and then when you open it again, you will be prompted to enter your account number and password, and you can view it only after logging in.

How does Nginx enable the browser to view access logs in real time?

The above is the detailed content of How does Nginx enable the browser to view access logs in real time?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template